This timed relay is for a UV-C disinfection chamber that uses 20 light tubes (30W 36in TUV T8 Germicidal Fluorescent) with interlocking doors to prevent entry while the system is running. The Arduino only controls the start/stop functions. It works as it should but I would like to improve it.
I got great help on this forum to accomplish this project, then added a few things here and there and so far works as it should.
The blinking LED is an indicator that the relay is on during the countdown, also the LCD states "RUNNING"
Now, what I have been unable to do is to have a countdown for the 20 minutes, and to print it here instead of the word running.
lcd.setCursor(7,1);
lcd.print("RUNNING ");
I have looked all over and came up with a few half working solutions but the closest I got I was able to display the countdown at the press of the start button but such countdown was already started, it would start once the device was powered on, for example if I waited one minute to press the button the count will start at 19:00
#include <LiquidCrystal_I2C.h>
#include <OneButton.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte buttonPin = 2; // SIGNAL FROM MOMENTARY START BUTTON
const byte resetPin = 4; // SIGNAL TO RESET COUNTDOWN, MOMENTARY PUSHBUTTON
const byte relayPin = 12; // 5 VOLT RELAY TO SWITCH LOAD ON
const byte ledPin = 13; // BLINKING LED TO INDICATE PROCESS
OneButton startButton(buttonPin);
OneButton resetButton(resetPin);
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 350; // INTERVAL TO TOGGLE THE LED ON AND OFF
const unsigned long duration = 1200000ul; // 20 MINUTES IS THE SET TIME TO TURN THE LOAD OFF AFTER HITTING START BUTTON
unsigned long chrono; // RECORD TIME TO COMPARE MILLIS FUNCTION
bool relayIsOn = false;
void relayOff() {
lcd.setCursor(7,1);
lcd.print("STAND BY");
digitalWrite(ledPin, LOW);
digitalWrite(relayPin, LOW);
relayIsOn = false;
}
void relayOn() {
digitalWrite(relayPin, HIGH);
chrono = millis();
relayIsOn = true;
}
void start() {
if (!relayIsOn) relayOn();
}
void stop() {
relayOff();
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
relayOff();
lcd.setCursor(1,0);
lcd.print("RIVER BEND UV ROOM");
lcd.setCursor(7,1);
lcd.print("STAND BY");
startButton.attachClick(start);
resetButton.attachClick(stop);
}
void loop() {
startButton.tick();
resetButton.tick();
if (relayIsOn && (millis() - chrono >= duration)) relayOff();
unsigned long currentMillis = millis();
if (relayIsOn && (currentMillis - previousMillis >= interval)) {
lcd.setCursor(7,1);
lcd.print("RUNNING ");
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}