I have arduino UNO with bluetooth module and vibration motor attached. I can turn on the vibrator but cant seem to turn it off. Here's the code
#include<SoftwareSerial.h>//import the serial library
int vib=8;
SoftwareSerial Genotronex(10,11);//RX,TX
int BluetoothData;//the data given
void setup() {
Genotronex.begin(9600);
Genotronex.println("Bluetooth on please press 1 to vibrate");
pinMode(vib,OUTPUT);
// put your setup code here, to run once:
}
void loop() {
if (Genotronex.available()){
BluetoothData=Genotronex.read();{
if(BluetoothData='1'){
digitalWrite(vib,'1');
Genotronex.println("Vibrator on");
delay(500);
}
else if (BluetoothData='0'){
digitalWrite(vib,'0');
Genotronex.println("Vibrator off");
delay(500);
}
}
}
delay(100);
// put your main code here, to run repeatedly:
}
In the bluetooth terminal, it stated <1> Vibrator on when i input '1' but also stated <0) Vibrator on when i input '0' when it should've been Vibrator off.
Appreciate all the help
if(BluetoothData='1'){
^
Single =
is assignment. Use ==
for comparisons.
Also, BluetoothData
should probably be defined as a local variable in loop()
. It'll work either way, but will compile to slightly more efficient (and more readable!) code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.