📜  como comparar serial.read con har - 任何代码示例

📅  最后修改于: 2022-03-11 15:00:33.484000             🧑  作者: Mango

代码示例1
int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }
  readString.trim();
  if (readString.length() >0) {
    if (readString == "on"){
      Serial.println("switching on");
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      Serial.println("switching off");
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}