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);
    }
    }
上一篇:ESP32-C3利用AsyncWebServer和HTTPClient实现双机通信


下一篇:图灵《JVM》