#include <LiquidCrystal_I2C.h>
const uint8_t redPin = 4 ;
const uint8_t ledPin = 7 ;
bool lastRedState = HIGH ;
uint16_t counter = 0 ;
uint32_t lastSecs = 0 ;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
pinMode(redPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT );
lcd.init( );
lcd.backlight();
}
void loop() {
bool redState = digitalRead( redPin ) ;
uint32_t secs = millis() / 1000 ;
if ( redState == LOW ) {
digitalWrite(ledPin, HIGH);
if ( secs != lastSecs ) {
lastSecs = secs ;
if ( counter > 0 ) counter-- ;
}
if ( lastRedState == HIGH ) {
lastRedState = redState ;
counter = 105 ; // should be 130
}
lcd.clear() ;
if( counter < 100 ) lcd.print( counter );
delay(500) ;
}
else {
// clear up
lcd.clear() ;
digitalWrite(ledPin, LOW);
lastRedState = HIGH ;
}
}Keep pressing Red button to simulate red light