Loop problem, please help

Hello, guys, here goes my problem (I think it is quite a simple one, but I am a beginner :frowning: )

I have to control one LED with two pushbuttons (buttonA and button B) and this is the routine of my problem:

1- pulse on buttonA --> LED on;
2- LED keeps on even when buttonA is not pushed;
3- pulse on buttonB --> LED off;
4- LED keeps off until buttonA is pushed again.

I can't find a way to keep the LED on after releasing the buttonA. When I use "if", the LED turns on only by the time that the buttonA is pushed. Right now, that is my code:

void setup() {

  • Serial.begin(9600); //start serial connection*

  • pinMode(2, INPUT_PULLUP); //configure pin2 as an input and enable the internal pull-up resistor*

  • pinMode(3, INPUT_PULLUP);*

  • pinMode(12, OUTPUT);*
    }
    void loop()
    {

  • int buttonA = digitalRead(2); //read the pushbutton value into a variable*

  • int buttonB = digitalRead(3);*

  • Serial.println(buttonA); //print out the value of the pushbutton*

  • Serial.println(buttonB);*

  • while (buttonA == LOW)*

  • {*

  • while (buttonB == HIGH)*

  • {*

  • digitalWrite(12, HIGH);*

  • }*

  • }*
    }

I thank for all the help you guys can give me and I hope we figure this out.

What did the version with the if statements look like? Should just be one if to turn it on, one to turn it off.

Please use code tags when you post more code.

I often see people try to describe the nature of their device using computer sounding terms. I think you should describe your device in plain English then move it to code. Your device seems to be push one button LED turns on. Push the other button LED turns off.

I see no need to entangle the two buttons the way you seem to. Just monitor the signal at each button pin for state changes. Button A can always turn on the LED which would have no effect if it is already on. Button B can similarly always turn it off.

Basically the logic would be

if button pin A state has changed to LOW turn LED on.
if button pin B state has changed to LOW turn LED off.
repeat above steps

Check out this page ButtonStateChange

ETA: That example uses external resistors. So HIGH is pressed.

almirfilho77:
1- pulse on buttonA --> LED on;
2- LED keeps on even when buttonA is not pushed;
3- pulse on buttonB --> LED off;
4- LED keeps off until buttonA is pushed again.

This is a simple logical problem.

Have a variable to record the led state - call it ledState.

Then when button A is pressed it sets ledState to HIGH
When button B is pressed it sets ledState to LOW

and the led is operated with

digitalWrite(ledPin, ledState);

...R