Arduino Serial.read()串口接收数据,输入open点灯,输入其他关闭
作者:互联网
String comdata="";
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
Serial.begin(9600);
while(Serial.read() >= 0){}
Serial.println(“Begin”);
}
void loop() {
// put your main code here, to run repeatedly:
ReceiveWifiMessage();
if(comdata.length()>0){
Serial.println(comdata);
if(comdata.compareTo(“open”)>0){
Serial.println(“Light”);
digitalWrite(13, HIGH); // 将LED点亮(HIGH代表高电压)
}else{
Serial.println(“Dark”);
digitalWrite(13, LOW); // 将LED熄灭(LOW代表低压)
}
}
}
/**
- 读取串口缓冲区里面的数据
*/
void ReceiveWifiMessage(){
comdata="";
while (Serial.available() > 0 )
{
comdata+=char(Serial.read());
delay(2);
}
}
标签:13,read,void,println,串口,comdata,Serial,接收数据,输入 来源: https://blog.csdn.net/weixin_43132322/article/details/122596816