Author Options:

Arduino On/off button? Answered

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?



Something like this
int mylight = LOW;
int whatever = 13;
int ledpin = 12;

  pinMode (whatever,INPUT);
while(digitalRead(whatever)==LOW);  //do nothing until button pressedif (digitalRead(whatever)==HIGH) mylight=!mylight;
while (digitalRead(whatever)==HIGH);

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.

/* Thank you steve for all your help!! */

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