int buttonPin = 12;
int ledPin = 9;
boolean isLighting = false;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(buttonPin) == LOW){ //if LED is off
delay(10); // skip the phenomenon of 'bounce'
if(digitalRead(buttonPin) == LOW){ // if LED is off
reverseLED(); // call funcipn that turns on LED
while(digitalRead(buttonPin) == LOW){
delay(10); // keep delaying 10ms to avoid 'bounce' while LED is off
}
}
}
}
void reverseLED(){
if(isLighting){ // if LED is on
digitalWrite(ledPin, LOW);
isLighting = false; // store state of led
}
else{
digitalWrite(ledPin, HIGH);
isLighting = true; // store state of LED
}
}