#include <stdint.h>
#define KEY A0
uint8_t key=1,previous=1;
uint16_t keyCount;
uint32_t tickKey;
void setup(){
Serial.begin(115200);
Serial.println("Begin.");
pinMode(KEY,INPUT_PULLUP);
}
void loop(){
if((millis()-tickKey)>20){ //do every 20mS
tickKey=millis(); //reset counter
key=digitalRead(KEY); //read key
if(previous==1 && key==0){ //is it a new key press
Serial.println("Key Pressed, light LED.");
keyCount=2000/20; //2 seconds delay started
}else if(previous==0 && key==1){ //has key been released?
Serial.println("Key released, turn LED off.");
}
previous=key; //keep copy of key
if(key==0 && keyCount>0){ //key pressed and counter running
if(--keyCount==0){ //count down and if zero, 2 seconds are up
Serial.println("2 seconds up, flash LED");
}
}else{
keyCount=0;
}
}
//any code here will run continuously.
}