Hi,
I am making a UV disinfectant BOX using OSRAM 254nm wavelength light.
Intended operation,
-
When the start button is pressed and the reed switch ON.
-
2 relays should turn ON for 120sec I.e 2min.
-
If the start button is pressed again during the count down, another 60sec should be added to the down count. (if the start button is pressed twice 120sec should be added)
-
And if the reed switch goes OFF in between the down count, both the relay should be turned off.
I have made an effort to put up a basic code, I need the forums to help to complete the code part
as I have a good understanding of hardware.
This is initial code I have written. It works but i know it can be written well and it does not have No 3) function.
#include <stdio.h>
#include <Wire.h> // Library for I2C
#include <LiquidCrystal_I2C.h> // Library for LCD
const long interval = 500; // Interval at which buzzer beeps
// Input and output Pins
int ButtonState = 2; // Interrupt input Pin D2 on Nano for Button switch
int ReedSwitch = 3; // Interrupt input Pin D3 on Nano for magnetic reed switch
int FanRelay = 7; // Output Pin D7 on Nano for fan relay
int LightRelay = 8; // Output pin D8 on Nano for light relay
int BuzzerPin = 6; // Output pin D6 on Nano for Buzzer
int Seconds;
int logic1 = 0;
int logic2 = 0;
int BuzzerState = LOW;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
pinMode(FanRelay, OUTPUT);
pinMode(LightRelay, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
pinMode(ButtonState, INPUT);
pinMode(ReedSwitch, INPUT);
digitalWrite(FanRelay, HIGH);
digitalWrite(LightRelay, HIGH);
digitalWrite(BuzzerPin, LOW);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("UV SANITIZER BOX");
delay(3000);
}
void splashscreen()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("2 Min SANITATION");
lcd.setCursor(0, 1);
lcd.print("* Press Start *");
delay(100);
}
void DisplayTime()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Time Remaining");
lcd.setCursor(0, 1);
lcd.print("Seconds - ");
lcd.setCursor(10, 1);
lcd.print(Seconds);
}
void DisplayInterrupt()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("UV SANITATION");
lcd.setCursor(0, 1);
lcd.print("Interrupted!!!!!");
delay(3000);
}
void DisplayFinish()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("UV SANITATION");
lcd.setCursor(0, 1);
lcd.print("****COMPLETE****");
delay(5000);
}
void loop()
{
splashscreen();
logic1 = digitalRead(ButtonState);
logic2 = digitalRead(ReedSwitch);
if (logic1 == 1 && logic2 == 1 )
{
for (int Countdown = 240; Countdown >= 0; Countdown --)
{
digitalWrite(LightRelay, LOW);
digitalWrite(FanRelay, LOW);
tone(BuzzerPin, 500);
delay(500);
Seconds = (Countdown / 2);
DisplayTime();
logic2 = digitalRead(ReedSwitch);
if (logic2 == 0 )
{
digitalWrite(LightRelay, HIGH);
digitalWrite(FanRelay, HIGH);
DisplayInterrupt();
break;
}
}
DisplayFinish();
}
else {
digitalWrite(LightRelay, HIGH);
digitalWrite(FanRelay, HIGH);
}
}
I want to use MILLIS() for the delay.
In the below code “if ((logic1 == 1) && (logic2 == 1))” works fine only when Both logic1 & logic2 are HIGH. when logic2 is LOW and logic1 is HIGH the relay turns on for the duration logic1 is HIGH.
void loop()
{
logic1 = digitalRead(ButtonState);
logic2 = digitalRead(ReedSwitch);
if ((logic1 == 1) && (logic2 == 1))
{
digitalWrite(FanRelay, HIGH);
offat = millis()+5000;
Serial.print(offat);
}
if (digitalRead(FanRelay) == HIGH)
{
logic2 = digitalRead(ReedSwitch);
if((millis() >= offat) || (logic2 == LOW))
{
digitalWrite(FanRelay, LOW);
}
}
}
Thank you