简体   繁体   中英

Clear data in Serial.read()

I am programming an Arduino to turn a LED on when it receives something in the serial port. I have done this and it has received the data but after the LED should have been turned off it keeps going. Here is my current code:

int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0) {
    Serial.print("RECEIVED_CALL");
    digitalWrite(ledPin, HIGH);
    delay(4500);
    digitalWrite(ledPin, LOW);
    Serial.clear();
  }
}

Please leave any suggestions.

Add some debugging statements (like Serial.print("LED_OFF") ) to be sure that your Arduino is not restarting upon receiving serial data as described in my post here .

This may account for the light staying on. It may be restarting over and over again if you are sending enough serial data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM