Go Down

Topic: Keep variable value until serial input is changed (Read 692 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

PaulS

So, every time you type something, the serial monitor sends that and a carriage return. A carriage return is not a '1', so the LED turns off.

There are other options...


UKHeliBob

Like selecting an option that does not add an extra character or 2
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lawm


Like selecting an option that does not add an extra character or 2


Can you please explain in detail?

PaulS

Quote
Can you please explain in detail?

Can't you look at the list of options and figure that "No line ending" adds nothing, that "CR only" and "Line feed only" add one character, and that "Both CR and LF" adds two characters?

If not, can't you try all 4 options, and see if one of them allows the LED to stay on?

lawm


Go Up