其他分享
首页 > 其他分享> > arduino串口

arduino串口

作者:互联网

 

 

 

 

int ledPin =  2;  
String comdata = "";
char terminator = ';';
void setup() {
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);
  Serial.begin(9600);
  while(Serial.read()>= 0){} //clear serialbuffer
}
 
void loop() {
  // read data from serial port
  if(Serial.available()>0){
      comdata =Serial.readStringUntil(terminator);
      //Serial.print("REC: ");
      //Serial.println(comdata);

      if(comdata=="open"){
      digitalWrite(ledPin, LOW);}
      else{
      digitalWrite(ledPin, HIGH);
        }
       

      
    }
    while(Serial.read()>= 0){}
}

  

标签:arduino,read,digitalWrite,terminator,串口,comdata,Serial,ledPin
来源: https://www.cnblogs.com/kekeoutlook/p/14171091.html