Arduino On/off button?

Hey so I had a question.

Can somebody post some code of how to make an LED come on when you press a button and stay on, then once you press the button again it goes off and stays off until you press the button again?


sort by: active | newest | oldest
Something like this

int mylight = LOW;
int whatever = 13;
int ledpin = 12;

void setup()
  pinMode (whatever,INPUT);
void loop()
while(digitalRead(whatever)==LOW);  //do nothing until button pressed
if (digitalRead(whatever)==HIGH) mylight=!mylight;
while (digitalRead(whatever)==HIGH);

HavocRC (author)  steveastrouk4 years ago
Two questions :D

What does "mylight=!mylight mean? I know that "!" means (not).

How did you past the code in there with color and a different font??


i test that last code and working, but how i change output pwm ? i think analogWrite pin A5 pwm,25

and how adding button / or bush second time pwm change to 255 and plink ?

Assign the new value of mylight to NOT the value currently in mylight.

If mylight = false, then mylight=!mylight leaves mylight with true in it - so it toggles from on to off.

Copy as HTML from Arduino, Rich editor, source view, and paste in Instructables.
HavocRC (author)  steveastrouk4 years ago
/* Thank you steve for all your help!! */

Brilliant ! What a great way of sending a reply ;-)
Its a really good way of posting code.

HavocRC (author)  steveastrouk4 years ago