Tri*_*tan 4 c python arduino pyserial
我有一个arduino连接到我的电脑(COM9),我有3个python脚本.第一个通过串行发送"1".第二个在串行上发送"0".第三个发送一个你给它的词.
ser.write("1")
Run Code Online (Sandbox Code Playgroud)
然后在我的arduino上我有一些代码.如果启动了python脚本1,它将打开一个led.如果启动2秒脚本,它将关闭一个led.如果启动了python脚本3,它会将该字打印到lcd.
所有硬件配置正确.问题是,当我运行脚本1时,不仅led会打开,而且lcd上也会有1.其他2个脚本按预期工作.
这是我的arduino代码的一部分.
if (Serial.available())
{
wordoftheday = Serial.readString();
if (wordoftheday == "1"){email = true;}
if (wordoftheday == "0"){email = false;}
else {
lcd.clear();
lcd.print(wordoftheday);
}
}
if (email == true){digitalWrite(9, HIGH);}
if (email == false){digitalWrite(9, LOW);}
Run Code Online (Sandbox Code Playgroud)
您无法使用比较字符串 ==
if (wordoftheday == "1"){email = true;}
Run Code Online (Sandbox Code Playgroud)
应该
if (strcmp(wordoftheday, "1") == 0){email = true;}
Run Code Online (Sandbox Code Playgroud)
而且(正如@chux指出的那样),你似乎忘记了else:
if (strcmp(wordoftheday, "1") == 0)
email = true;
else
if (strcmp(wordoftheday, "0") == 0)
email = false;
else {
lcd.clear();
lcd.print(wordoftheday);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |