0

Imagine a system where you have two transient non-exclusive buttons and two LEDs.

When you press button 1, the LED 1 is HIGH and LED 2 is LOW (and stays that way).

When you press button 2, the LED 1 is LOW and LED 2 is HIGH (also stays that way).

Which C++ code allow me, to keep the LED 1 turned on indefinitely (until button 2 is pressed)?

Basically, my problem is, that the buttons are transient and the behaviour of LEDs shouldn't be.

Here is code, that would work for pernament exclusive buttons, but doenst work for transient non-exclusive buttons.

if (digitalRead(button1) == HIGH) 
{   
    led1 = HIGH; 
}
else 
{ 
    led2 = LOW; 
}
if (digitalRead(button2) == HIGH) 
{ 
    led2 = HIGH; 
}
else 
{ 
    led2 = LOW; 
}
4
  • AIqbalRaj Choices are in the infinite space of all possible programs.
    – GIA_96
    Nov 23, 2018 at 10:11
  • can you share the code that you tried and where did you get stuck?
    – AIqbalRaj
    Nov 23, 2018 at 10:55
  • I edited my original post.
    – GIA_96
    Nov 23, 2018 at 11:02
  • Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
    – KIIV
    Nov 23, 2018 at 12:12

2 Answers 2

0

Something like this:

if (digitalRead(button1) == HIGH) 
{   
    led1 = HIGH; 
    led2 = LOW; 
}
if (digitalRead(button2) == HIGH) 
{ 
    led2 = HIGH; 
    led1 = LOW; 
}
1
  • Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
    – GIA_96
    Nov 23, 2018 at 13:21
1

Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.

Update:

How do I prevent overwriting the state variable with different input ?

You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton saves the number of the most recent button:

void setup(){
   pinmode(LED1PIN, OUTPUT);
   pinmode(LED2PIN, OUTPUT);
   pinmode(BUTN1PIN, INPUT_PULLUP);  // you might not want the pullup
   pinmode(BUTN2PIN, INPUT_PULLUP);  //  depending on your wiring.
}


void loop(){
   static uint8_t lastButton = 0;

   // Save the number of the last button pressed.
   if (digitalRead(button1) == HIGH) 
      lastButton = 1;
   else if (digitalRead(button2) == HIGH) 
      lastButton = 2;

   // Set the LEDs according to most recent button
   // Note that at program-start, when no button has
   // been pressed yet, this code leaves both LED's off.
   if (lastButton == 1) 
   {   
      digitalWrite(LED1PIN, HIGH);
      digitalWrite(LED2PIN, LOW);
   }
   else if(lastButton == 2)
      digitalWrite(LED1PIN, LOW);
      digitalWrite(LED2PIN, HIGH);
   }
}
2
  • Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
    – GIA_96
    Nov 28, 2018 at 15:59
  • I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
    – GIA_96
    Nov 28, 2018 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.