我有arduino UNO与蓝牙模块和振动马达附件。我可以打开振动器,但好像关不掉它。下面是代码
#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:
}在蓝牙终端中,当我输入'1‘时,它显示<1>可控震源打开,但当我输入'0’时,它也显示<0)可控震源打开,而它应该是可控震源关闭。
感谢所有的帮助
发布于 2016-11-12 12:43:54
if(BluetoothData='1'){
^单个=是赋值。使用==进行比较。
此外,在loop()中,可能应该将BluetoothData定义为局部变量。无论采用哪种方式,它都可以工作,但编译效率会稍微高一些(可读性也更好!)代码。
https://stackoverflow.com/questions/40559626
复制相似问题