首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino在输入0后没有关闭振动

Arduino在输入0后没有关闭振动
EN

Stack Overflow用户
提问于 2016-11-12 12:37:27
回答 1查看 42关注 0票数 0

我有arduino UNO与蓝牙模块和振动马达附件。我可以打开振动器,但好像关不掉它。下面是代码

代码语言:javascript
复制
#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)可控震源打开,而它应该是可控震源关闭。

感谢所有的帮助

EN

回答 1

Stack Overflow用户

发布于 2016-11-12 12:43:54

代码语言:javascript
复制
if(BluetoothData='1'){
                ^

单个=是赋值。使用==进行比较。

此外,在loop()中,可能应该将BluetoothData定义为局部变量。无论采用哪种方式,它都可以工作,但编译效率会稍微高一些(可读性也更好!)代码。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40559626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档