//bool sw = false;
//bool x = false;
//bool state = LOW;
//bool lastState = LOW;
//bool armed = true;
//uint8_t btn_prev;
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 25; // the debounce time; increase if the output flickers
void setup() {
Serial.begin (9600);
pinMode (9,OUTPUT); // Latch relay
pinMode (3,INPUT); //pushbutton latching
digitalWrite(9, ledState);
attachInterrupt(digitalPinToInterrupt(3), latchSW, RISING);
//state = digitalRead(3);
}
void loop() {
if(ledState != lastButtonState){
// set the LED:
if(ledState == LOW && lastButtonState == HIGH){
Serial.println("TimeDelay 2 sec");
delay(2000);
Serial.println("OFF");
digitalWrite(9, LOW);
//lastButtonState = LOW;
}else if(ledState == HIGH && lastButtonState == LOW){
Serial.println("ON");
digitalWrite(9, HIGH);
//lastButtonState = HIGH;
}
}
//delay(500);
}
void latchSW(){
// read the state of the switch into a local variable:
int reading = digitalRead(3);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
/*
if (btn == LOW && btn_prev == HIGH)
{
//if(latchstate == HIGH){
//LED on pin 9 is Push ON, Push OFF
digitalWrite(9,HIGH);
Serial.println("HIGH");
}
else{
digitalWrite(9,LOW);
Serial.println("LOW");
}
//}
btn_prev = digitalRead(3);
uint8_t state = digitalRead(3);
if(state && !lastState){
lastState = true;
x!=x;
if(x){
// code if turned on
digitalWrite(9,HIGH);
Serial.println("HIGH");
}else{
// code if turned off
delay(1000);
digitalWrite(9,LOW);
Serial.println("LOW");
}
}else if(!state){
lastState = false;
}
if (armed && digitalRead(3) == PRESSED){
armed = false;
...
}
buttonState = digitalRead(3);
if (buttonState == HIGH)
{
if(latchState == LOW){
//LED on pin 9 is Push ON, Push OFF
digitalWrite(9,HIGH);
Serial.println("HIGH");
latchState == HIGH;
}
else{
//delay(2000);
digitalWrite(9,LOW);
Serial.println("LOW");
latchState == LOW;
}
}
*/