N00B Question

I am wondering how I would make an LED turn on with the push of a button and stay on after I release the button, but turn off when I push it again and stay off when I release the button?

Sincerely,
Nicholas Leffler
(Nick_12)

if (button == TRUE && led == FALSE) 
    led = ON; 
if (button == TRUE && led == TRUE)
    led = OFF;

This is the easy part. Good luck with the debouncing.

could you explain this please

if your button is pressed and the led is off, then the led should come on.
if your button is pressed and the led is on, then the led should come off.

The problem is that you need to make sure that when you push your key, you get a clean 0 to 1 and 1 to 0 transitions. That is hardly the case, and it is called key bounicng. Hence the name debouncing for the technique used to deal with this.

I have noticed though, that you still need to add another variable to know if there was a transition of the button or not.

button = digitalRead(); 

if (button !=lastButton && led == FALSE && button == TRUE)  [
    led = ON; 
   lastButton = button; //otherwise the next statement would reverse this
   }
if (button !=lastButton && button == TRUE && led == TRUE)
    led = OFF;

lastButton = button;

button = digitalRead();

if (button !=lastButton && led == FALSE && button == TRUE) [
led = ON;
lastButton = button; //otherwise the next statement would reverse this
}

Perhaps a typo? :slight_smile:

Perhaps a typo?

His shift key works like mine - sometimes when you want it, sometimes when you don't.