int pbuttonPin = 2; //connect output to push button
int relayPin = 10; //connected to relay (LED)
int val = 0; //push value from pin 2
int lightON = 0; //light status
int pushed = 0; //push status
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(pbuttonPin); //read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
Serial.println(pushed);
lightON = val;
if (pushed == HIGH){
Serial.println("Light OFF");
digitalWrite(relayPin, LOW);
}
else{
Serial.println("Light ON");
digitalWrite(relayPin, HIGH);
}
delay(100);
}