How to compare string from Serial.read()?

后端 未结 6 2347
遇见更好的自我
遇见更好的自我 2021-02-09 11:07

I have this code below where I got from this forum that I followed through. It did not work for me but they claim that the code is fine. I already tried several string compariso

6条回答
  •  面向向阳花
    2021-02-09 11:27

    Why not use Serial.readString();??

    Try this..

         void setup() {
          pinMode(13, OUTPUT);
          Serial.begin(9600);
        }
    
        void loop(){
          if(Serial.available()){
            String ch;
            ch = Serial.readString();
            ch.trim();
            if(ch=="on"||ch=="ON"){
              digitalWrite(13, HIGH);  
            }
            else if(ch=="off"||ch=="OFF"){
              digitalWrite(13, LOW);
            }
          }
        }
    

提交回复
热议问题