Pages: [1]   Go Down
Author Topic: Keep variable value until serial input is changed  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)?
Logged

US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are other options...

Like...
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3526
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Can you please explain in detail?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

US
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I realized No Line Ending does it.
Logged

Pages: [1]   Go Up
Jump to: