I have below code for my esp8266. when I set 15min time, the esp8266 turn of relay before 15min but it does work with smaller time such as 2 min.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//----------PIN ASSIGNEMENTS-----------//
//#define LEDs D7 //LED Transistor Trigger Pin
#define buzzer D6 //Buzzer and LED Pin
#define button D5 //Select Button Pin
#define durationPot A0 //Duration Knob Pin
//#define brightnessPot D8 //Brightness Knob Pin
//-------CONFIGURABLE PARAMETERS-------//
int
maxTime = 1200, //Maximum countdown timer time (in secs)
buzzerFreq = 5000; //Buzzer frequency (Hz, Change to customize tone)
//glowDelay = 10; //Change this to increase LED glow rate (0-200)
//---SYSTEM VARIABLES (DO NOT TOUCH)---//
float
timeLeftBarPercent = 0.00,
durationSecFloat = 0.00,
durationSecSelect = 0.00;
int
//whitePWM = 0,
duration = 0,
durationSec = 0,
//brightness = 0,
//brightnessPercent = 0,
//brightnessPWM = 0,
seconds = 0,
minutes = 0,
exposureLoop = 0,
startupLoop = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
char timeMinSec[16];
int relayInput = 13;
//-VOID SETUP (Runs codes before entering loop)-//
void setup() {
lcd.begin();
lcd.backlight();
// pinMode(LEDs, OUTPUT);
pinMode(buzzer, OUTPUT);
// initialize the LCD
digitalWrite(relayInput, LOW);
pinMode(relayInput, OUTPUT);
lcd.clear();
while (digitalRead(button) == LOW) {
lcd.setCursor(0, 0);
lcd.print(" PCB UV ");
lcd.setCursor(0, 1);
lcd.print(" Exposure Box ");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Firmware Version");
// delay(2000);
lcd.setCursor(6, 1);
lcd.print("v1.0");
delay(2000);
break;
}
lcd.clear();
}
//-VOID LOOP (Code that runs on repeat)-//
void loop() {
//--Gather Duration Knob Readings--//
duration = analogRead(durationPot);
durationSec = map(duration, 0, 1023, 0, maxTime);
/*
//--Gather Brightness Knob Readings--//
brightness = analogRead(brightnessPot);
brightnessPercent = map(brightness,0,1023,0,100);
brightnessPWM = map(brightness,0,1023,0,255);
//--Control LED brightness in real time--//
analogWrite(LEDs,brightnessPWM);
*/
minutes = durationSec / 60;
seconds = durationSec % 60;
lcd.setCursor(0, 0); lcd.print("Duration: ");
sprintf(timeMinSec, "%0.2d:%0.2d", minutes, seconds);
lcd.setCursor(11, 0); lcd.print(timeMinSec);
/*
if(brightnessPercent<100){ //Used this instead of "lcd.clear();" function. Done to reduce LCD flicker.
lcd.setCursor(14, 1);
lcd.print(" ");
}
lcd.setCursor(0, 1);
lcd.print("Brightness: ");
lcd.setCursor(12, 1);
lcd.print(brightnessPercent);
lcd.setCursor(15, 1);
lcd.print("%");
*/
if (digitalRead(button) == HIGH) {
durationSecSelect = durationSec;
durationSecFloat = durationSec;
digitalWrite(relayInput, HIGH);
lcd.clear();
/*
lcd.setCursor(0, 0);lcd.print(" Initializing ");delay(300);
lcd.setCursor(0, 0);lcd.print(" Initializing. ");tone(buzzer, buzzerFreq*3, 100);delay(300);
lcd.setCursor(0, 0);lcd.print(" Initializing.. ");tone(buzzer, buzzerFreq*3, 100);delay(300);
lcd.setCursor(0, 0);lcd.print(" Initializing...");tone(buzzer, buzzerFreq*3, 100);delay(500);
*/
lcd.setCursor(0, 0); lcd.print(" STARTING ");
lcd.setCursor(0, 1);
for (int i = 0; i <= 15; i++) {
lcd.print((char) 255); delay(35);
tone(buzzer, buzzerFreq * 2, 50);
}
lcd.print((char) 255);
delay(300);
// analogWrite(LEDs,brightnessPWM);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time Left: ");
while (exposureLoop == 0) {
timeLeftBarPercent = 100 * (durationSecFloat / durationSecSelect);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
minutes = durationSec / 60;
seconds = durationSec % 60;
sprintf(timeMinSec, "%0.2d:%0.2d", minutes, seconds);
lcd.setCursor(11, 0);
lcd.print(timeMinSec);
if (timeLeftBarPercent < 2) {
for (int v = 0; v <= 15; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 6.25) {
for (int v = 0; v <= 14; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 12.5) {
for (int v = 0; v <= 13; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 18.75) {
for (int v = 0; v <= 12; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 25) {
for (int v = 0; v <= 11; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 31.25) {
for (int v = 0; v <= 10; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 37.5) {
for (int v = 0; v <= 9; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 43.75) {
for (int v = 0; v <= 8; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 50) {
for (int v = 0; v <= 7; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 56.25) {
for (int v = 0; v <= 6; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 62.5) {
for (int v = 0; v <= 5; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 68.75) {
for (int v = 0; v <= 4; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 75) {
for (int v = 0; v <= 3; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 81.25) {
for (int v = 0; v <= 2; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 87.5) {
for (int v = 0; v <= 1; v++) {
lcd.setCursor(v, 1);
lcd.print((char) 255);
}
}
else if (timeLeftBarPercent < 93.75) {
lcd.setCursor(0, 1);
lcd.print((char) 255);
}
durationSec--;
durationSecFloat--;
}
if (durationSec <= 0) {
// digitalWrite(LEDs,LOW); //Turn off LEDs when countdown hits 0
digitalWrite(relayInput, LOW); // turn relay off
tone(buzzer, 3000, 500); //If you want to keep LED and buzzer for 500 ms for task start and stop
// tone(buzzer,3000); //If you want to keep LED and buzzer to run long after task completed
lcd.setCursor(0, 0); lcd.print(" Exposure ");
lcd.setCursor(0, 1); lcd.print(" Finished ");
delay(500); lcd.clear();
lcd.setCursor(0, 0); lcd.print(" Press Button ");
lcd.setCursor(0, 1); lcd.print(" To Start ");
delay(500);
while (digitalRead(button) == LOW) {
yield(); //Press button to proceed with system restart
}
//always make sure that you reset the wdt in what could be an infinite loop.
/*
for (int i=0; i <= 255; i++){ //Glow-up Illumination LED
analogWrite(LEDs,i);
delay(glowDelay);
}
*/
lcd.clear();
exposureLoop = 1;
}
else if (digitalRead(button) == HIGH) {
// digitalWrite(LEDs,LOW);
tone(buzzer, 3000, 500);
lcd.setCursor(0, 0); lcd.print(" Cancelling ");
lcd.setCursor(0, 1); lcd.print(" Process! ");
delay(750);
lcd.setCursor(0, 0); lcd.print(" Exposure ");
lcd.setCursor(0, 1); lcd.print(" Finished ");
delay(500); lcd.clear();
lcd.setCursor(0, 0); lcd.print(" Press Button ");
lcd.setCursor(0, 1); lcd.print(" To Start ");
/* while(digitalRead(button)==LOW){}
for (int i=0;i<=brightnessPWM;i++){
analogWrite(LEDs,i);
delay(glowDelay);
}
*/
lcd.clear();
exposureLoop = 1;
}
yield(); // and put one before the end of the while loop
} //End of While
exposureLoop = 0;
} //End Of Startup Button
delay(20);
}//End of loop