const unsigned long Minutes = 60ul * 1000ul;
const unsigned long SevenMinutes = 3000;
const unsigned long EightMinutes = Minutes * 8;
#define pin3 3
static unsigned long startTime = millis();
//static unsigned long startTime = 0;
unsigned long elapsedInterval;
unsigned long now;
static unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
static int latch = 0;
const long interval = 4000;
int led2 = 2;
static int lastButtonState = 1;
int buttonState = 1;
void setup() {
Serial.begin(9600);
Serial.println();
static int dingo;
pinMode(pin3,INPUT_PULLUP);
pinMode(led2,OUTPUT);
}
void loop() {
//WORKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIING
//delay(500);
buttonState = digitalRead(pin3);
unsigned long elapsedInterval = millis() - startTime;
startTime=millis(); //Updates startTime.
if(digitalRead(pin3)==LOW && buttonState != lastButtonState){ //this is your trigger
now=millis(); //starts the Clock because now will not update with startime/Millis till button state change.
latch=1; //Captures moments of truth
}
// latch is important because it maintains state even as lasbutton state changes after initial press
Serial.println(now);
if (latch==1){
if (millis() - now >= interval ) {//&& latch ==1
digitalWrite(led2, HIGH);
delay(800);
digitalWrite(led2, LOW);
latch = 0;
}
}
lastButtonState = buttonState;
/* else if(digitalRead(pin3)==HIGH) {
latch = 0; // Prevents any accidental switch press from triggering led.
}*/
}