Switch LED On/OFF with the same command

Hy!
I'm trying to switch a LED On and Off with the same command.

Here is my code:

int LED_13 = 13;

void setup() {
pinMode(LED_13, OUTPUT);
Serial.begin(9600); // Porneste Serialul
}

void loop() {
int val;
if (Serial.available()) {
delay(10);
while (Serial.available() > 0) {
val = Serial.read();
if (val == '1') {

// I NEED HELP HERE

}
}
}
}

Where i wrote "I NEED HELP HERE" i need to put a code that turns the LED on/off. :smiley:

I think that i need to use a Boolean (or a 0 and 1 variable). And do something like this:

  1. When i receive the "1" value (and the Boolean is False), digitalWrite(LED_13, HIGH);
  2. When i receive the "1" value again (and the Boolean is True), digitalWrite(LED_13, LOW);

But i don't know how to write that in Arduino. :frowning:

I've done it! :slight_smile:
I tried this:

if (val == 'a') {
if (LED_1_V == 0) {
digitalWrite(LED_13, HIGH);
LED_1_V = 1;
}
else if (LED_1_V == 1) {
digitalWrite(LED_13, LOW);
LED_1_V = 0;
}
}

Works fine. :smiley: If you have any suggestions, let me know.

byte LED_1_V = 0;
void loop(){
       if (val == 'a') {
           LED_1_V = 1 - LED_1_V;  // results in 1,0,1,0 ...
             digitalWrite(LED_13, LED_1_V);  // I think this will work
// otherwise
          if (LED_1_V == 1){ digitalWrite (LED_13, HIGH);}
          else {digitalWrite (LED_13, LOW);}
       }
}

I'm fairly certain that you can do this:

int LED_13 = 13;

void setup() {
  pinMode(LED_13, OUTPUT);
  Serial.begin(9600);        //  Porneste Serialul
}

void loop() {
  int val;
  if (Serial.available()) {
    delay(10);
    while (Serial.available() > 0) {
      val = Serial.read();
          if (val == '1') {
            digitalWrite(LED_13, !digitalRead(LED_13));
          }
    }
  }
}

although the other solutions are WAY better

How about something more compact.

byte LEDpin = 13; //on-board LED
char key;

boolean latch = false;

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

void loop() {
  if(Serial.available()){

    key = Serial.read();
    if( key == 'H' || key == 'h'){
      latch = !latch;
      latch ? digitalWrite(LEDpin, HIGH) :  digitalWrite(LEDpin, LOW);
    }
  }
}

Or...

digitalWrite(LEDPin, !digitalRead(LEDPin));
latch ? digitalWrite(LEDpin, HIGH) :  digitalWrite(LEDpin, LOW);

Isn't that back-to-front? If I understand it correctly, the order of the ternary operator is condition, do-if-true, do-if-false, so that order switches it on when it is on, and off when it is off.

Shouldn't it be:

latch ? digitalWrite(LEDpin, LOW) :  digitalWrite(LEDpin, HIGH);

?

Doesn't matter, as long as it always does the same thing every time latch is true, and the opposite when it is false.

How about:

digitalWrite(LEDpin, latch ? HIGH : LOW);

Bugger the other suggestions.

Just enjoy your own success.

That’s what the Arduino is for :slight_smile:

…R

3AgL3_DeeJay:
I’ve done it! :slight_smile:

So many ways to do the same thing ...

const uint8_t   LED_13 = 13;

void loop()
{
    if ( Serial.available() )
    {
        delay(10UL);

        while ( Serial.available() > 0 )
        {
            if ( '1' == Serial.read() )
            {
                digitalWrite(LED_13, !digitalRead(LED_13));
            }
        }
    }
}


void setup()
{
    Serial.begin(9600);

    pinMode(LED_13, OUTPUT);
}

So many ways to do the same thing ...

Some better than others.

            if ( '1' == Serial.read() )

This is in the "others" category. It's real hard to add another case to code like this, if you want '2' to do something else, and '3' to do something else, too.

Baby steps Paul, baby steps!

Baby steps Paul, baby steps!

Agreed, as long as bad habits are not learned along the way. Personally, I see this as a bad habit in the making.

How about this:

  bitSet(PINB, 5);    // Toggle output on PB5, which is pin 13 on the Uno

From the Atmega328 spec sheet:

The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register.

This actually works. Who knew?

This actually works. Who knew?

Many know. Keep in mind this is a feature that some older AVR chips don't support at all and It's also not portable across the many different arduino boards that use different AVR chips.

Lefty

Well, it's amazing what you learn when you RTFM, eh?

Thank you for pointing out the non-portability of it. I still think it's kinda fun, but I'll be careful how I use it.