Hi, I had this condition and i want to stop after 5sec and does not wait a hole minute until stop.
The condition is :
if (weekDay=="Thu" && Hours==10 && Minutes==10 && Secs==00){
digitalWrite(relay_1, HIGH);
delay(5000);
digitalWrite(relay_1, LOW);
}
It's my first time i wrote at this forum so please forgive me if my post does not have the required protocol.
Actually, you did well! Welcome, Nick.
You write HIGH before and after the delay, I presume one of those should have been LOW.
My mistake. I change it.any suggest?
Well, if you changed it, what did you modify? No harm in re-posting your new code.
Hint - Your WHOLE code. Go to the IDE, find "copy for forum", click on that, then go back to the forum, open a new message in this thread, and ctrl-v to paste the code exactly as it was in the forum.
In future, don't modify the initial post, it's important to keep context for subsequent messages. Instead, just create a new message.
This is the whole code
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,20,4);
int relay_1=4;
int relay_2=5;
int relay_3=3;
int Day;
int Month;
int Year;
int Secs;
int Minutes;
int Hours;
String weekDay;
String myDate;
String myTime;
char daysOfTheWeek[7][12]={"San","Mon","Tue","Wed","Thu","Fri","Sat"};
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.init();
lcd.backlight();
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
RTC.begin();
if (!RTC.isrunning()) {
Serial.println("RTC is NOT running! - Adjusting Date & Time");
Serial.println();
RTC.adjust(DateTime(23, 9, 8, 00, 26, 0));
} else {
Serial.print("RTC adjusted & running");
Serial.println();
}
delay(1000);
}
void loop(){
DateTime now = RTC.now();
lcd.clear();
Day = now.day();
Month = now.month();
Year = now.year();
Secs = now.second();
Hours = now.hour();
Minutes = now.minute();
weekDay = daysOfTheWeek[now.dayOfTheWeek()];
myDate = myDate +weekDay+ " "+ Day + "/" + Month + "/" + Year ;
myTime = myTime + Hours +":"+ Minutes +":" + Secs ;
Serial.println(weekDay);
Serial.println(myDate);
Serial.println(myTime);
lcd.setCursor(0,0);
lcd.print(myDate);
lcd.setCursor(0,1);
lcd.print("Time:");
lcd.setCursor(6,1);
lcd.print(myTime);
myDate = "";
myTime = "";
delay(1000);
if(weekDay=="Thu" && Hours==10&& Minutes==10&& Secs==0){
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Mg=ON");
digitalWrite(relay_1, HIGH);
delay(5000);
digitalWrite(relay_1, LOW);
}
guix
September 21, 2023, 1:00pm
7
Welcome
Try something like this
static bool lock = false;
if ( lock == false && weekDay=="Thu" && Hours==10 && Minutes==10 && Secs==00){
lock = true;
digitalWrite(relay_1, HIGH);
delay(5000);
digitalWrite(relay_1, LOW);
}
Reset lock to false when needed
You have output on your Serial Monitor, showing this output would help us debug. Please cut and paste into a quote in your next message.
alto777
September 21, 2023, 1:19pm
9
The approach of looking for exactly Thursday at 10:10:00 will be compromised by that delay of one second.
If you aren't checking frequently enough, that exact time may pass by with your if statement never triggering.
10:09:59 - not time to
fail to check for 1000.001 milliseconds
10:10:01 - not time to
Here simply you could cut the delay and not use anything else that keeps it from checking more frequently.
If that delay were the only problem, cutting it to 500 ms would fix it.
As would
if (weekDay == "Thu" && Hours == 10 && Minutes == 10 && Secs < 4) {
a7
yes this works!! Thanks!! The other think that i want is to not stop the display time for 5sec. Is that possible?
guix
September 21, 2023, 1:49pm
11
Yes, look the Blink Without Delay example
A better way than comparing hours, minutes and seconds, is to convert the actual time as seconds since midnight. Here is a more advanced example : t940083.ino - Wokwi ESP32, STM32, Arduino Simulator
Here is the same example but with the time changed according to your need (you still have to add the weekday comparison) : t1170694.ino - Wokwi ESP32, STM32, Arduino Simulator