Go Down

Topic: Keep variable value until serial input is changed (Read 684 times) previous topic - next topic

lawm

I found this code from http://www.suffix.be/blog/send-data-to-arduino

Code: [Select]
int ledPin = 13;

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

void loop() {
  if (Serial.available() > 0) {
    char value = Serial.read();
    if (value == '1') {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
    Serial.println(value);
  }
  delay(1000);
}


It turns on the onboard LED when you type '1' in the serial monitor, and off when you type anything else.

However, the LED turns off on its own since the serial monitor returns to a default value immediately.

Does anybody know a way to make the LED stay on until you type something else in?

AWOL

Quote
However, the LED turns off on its own since the serial monitor returns to a default value immediately.

More likely, you're sending carriage-return/line-feed.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lawm


Quote
However, the LED turns off on its own since the serial monitor returns to a default value immediately.

More likely, you're sending carriage-return/line-feed.



Is this an answer?

PaulS

Quote
Is this an answer?

It's a hint for you to tell us what is sending the serial data to the Arduino, and how. If you are using the Serial Monitor, what option have you selected (at the bottom)?

lawm


It's a hint for you to tell us what is sending the serial data to the Arduino, and how. If you are using the Serial Monitor, what option have you selected (at the bottom)?


'Carriage Return' is selected

Go Up