The same button/trigger twice with Arduino, on/off?

So I'm having the hardest time trying to figure out how to make Arduino do this:

if statement == true
LED on
and if that statement if found to be true again,
LED off
then every time that statement == true toggle between LED on and off.

So how do I go about this??

Thanks!

sort by: active | newest | oldest
lewisb424 years ago
The key is you need a variable to track whether the LED is on or off:

if statement == true
{
if ledIsOn==true
{
turn LED off
set ledIsOn to false
}
else
{
turn LED on
set ledIsOn to true
}
}
HavocRC (author)  lewisb424 years ago
So I guess in the beginning of the code I would put,
int ledlsOn = true?
How would I go about that?
I'd probably make it boolean, i.e.,

boolean ledIsOn = true;

And yes, put it at the top of your code. Alternately, put the "boolean ledIsOn;" part at the top of your code and the "ledIsOn=true;" part in your setup() function.
HavocRC (author)  lewisb424 years ago
Hey lewisb thank you very much for all your help I was able to get my project to work. I'm still having a little trouble understanding exactly what's happening but I'll get it down eventually. Thank you!
You asked this a couple of weeks ago, and I gave you sample code. Did you try it ?
HavocRC (author)  steveastrouk4 years ago
Hahaha yes I know, but that was for a button. I'm trying to do this with some IR signals now, and it no matter what I tried, I couldn't get the code you posted to work :D