I made Arduino setup and codes so that if button is pressed when led is off, it will turned on, and keep the light on until next trigger.
Likewise, if button is pressed when led is on, it will turned off, and keep the light off until next trigger.
int state = HIGH;
int reading;
int previous = LOW;
void setup() {
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
reading = digitalRead(2);
if (reading == HIGH && previous == LOW) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
}
digitalWrite(13, state);
previous = reading;
}