yes that specific if was totally wrong in the original code...
by the way — I went for minutes since midnight assuming that the cut off time wouldn't always be a round hour. If it is then indeed there is no need to go beyond just checking the hours
yes that specific if was totally wrong in the original code...
by the way — I went for minutes since midnight assuming that the cut off time wouldn't always be a round hour. If it is then indeed there is no need to go beyond just checking the hours
handling crossing midnight might be more easily handled by separate the start and stop times rather than using a on-period
if (! onFlag && now.hour >= startHour) {
onFlag = true;
...
}
else if (onFlag && now.hour >= stopHour) {
onFlag = false;
...
}
Yes - if that were part of the requirements then minutes since midnight does not cut it
The logic does not work for starting at 2 AM and stopping at 1 AM. It does not handle crossing midnight, it seems, was it meant to or I am misreading it. Can't run it just now, the kn,y way I would be sure...
What is the flaw in the code I posted using minutes since midnight and a start time before midnight and an end time after?
a7
that may be true for a trange (e.g. 2am < time && time < 1am) but then i suggested start/stop times. you wouldn't start at 1am nor stop at 2 am
implement what meets your needs. i'm sure there there more complicated cases
Sry, I thought the code in #22 was supposed to better handle, or handle at all, a time span crossing midnight.
Why not?
Use something more plausible, then: start at 9 pm and stop at 6 am. Does your logic work for that?
I placed the logic in #22 in a context to test it and it is flawed even for start before stop time within a single day.
# include <stdio.h>
int hour;
int startHour = 3;
int stopHour = 9;
bool onFlag;
int main()
{
printf("Hello World\n");
hour = 22;
for (int ii = 0; ii < 26; ii++) {
if (! onFlag && hour >= startHour) {
onFlag = true;
}
else if (onFlag && hour >= stopHour) {
onFlag = false;
}
printf("%02d %s\n", hour, onFlag ? "ON" : "");
hour++; if (hour >= 24) hour = 0;
}
return 0;
}
Prints
Hello World
22 ON
23
00
01
02
03 ON
04 ON
05 ON
06 ON
07 ON
08 ON
09
10 ON
11
12 ON
13
14 ON
15
16 ON
17
18 ON
19
20 ON
21
22 ON
23
I can certainly believe I made an error testing those few lines. Please point it out if.
a7
sorry. change the ">=" to "=="
int startHour = 23;
int stopHour = 9;
Hello World
22
23 ON
00 ON
01 ON
02 ON
03 ON
04 ON
05 ON
06 ON
07 ON
08 ON
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23 ON
Thank you all so much for your feedback and advice: with the help of this forum I am very near completion of this project and really exited about it ![]()
Update I am attaching the new and modified code.
Everything in this code works just fine except the last few lines. The LED lights switch according to their correct day and times using IF , ELSE statements.
Using the LCD display i thought it would be useful to make a reminder that on Monday their are two medications required while on Friday only one medication is required.
When i run this code (modifying the day/times to equal now for testing) Monday's notification for two meds is overwritten by the ELSE statement "No Med's Required", however Friday's notification for one medication prints as it should when i modify that lines day/times to equal now.
???
I suspect an issue with the language using two if statements, so i initially wrote an ELSE after the first IF concerning the LCD message and i received the same results.
Any help understanding my mistake would be much appreciated
PS I am aware their are better components than the RTC and TEMP/HUMI sensors I've used for this project, these are the components I have on hand and saw no sense in throwing them away, LMAO
#include <Arduino.h>
#include <Ds1302.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "Arduino.h"
#include "DHT.h"
#define DHTPIN 10 // digital pin connected to the sensor module
#define DHTTYPE DHT11 // this is a DHT11 module
DHT dht(DHTPIN, DHTTYPE);
Ds1302 rtc(2,5,4);
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
const static char* WeekDays[7] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const static char* Months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT); // Red LED //
pinMode(8, OUTPUT); // Green LED //
dht.begin();
lcd.init();
lcd.backlight();
rtc.init();
}
void loop()
{
Ds1302::DateTime now;
rtc.getDateTime(&now);
static uint8_t last_second = 0;
if (last_second != now.second)
{
last_second = now.second;
}
float h = dht.readHumidity(); // Read temperature as Celsius (the default)
float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
lcd.setCursor(10,3);
lcd.print("T* ");
lcd.print(f);
lcd.setCursor(1,3);
lcd.print("H% ");
lcd.print(h);
lcd.setCursor(4,0);
lcd.print(Months[now.month - 1]); //01-12
lcd.setCursor(1,0);
if (now.day < 10) lcd.print('0');
lcd.print(now.day);
lcd.setCursor(14,0);
lcd.print("20");
lcd.print(now.year);
lcd.print(" ");
lcd.setCursor(1,1);
if (now.hour < 10) lcd.print('0');
lcd.print(now.hour);
lcd.print(",");
if (now.minute < 10) lcd.print('0');
lcd.print(now.minute);
// lcd.print("");
lcd.setCursor(10 , 1);
lcd.print(WeekDays[now.dow - 1]);
if ((now.dow == 1) && (now.hour >= 15 && now.hour <= 21) && (now.minute <= 59 ) ||
(now.dow == 5) && (now.hour >= 3 && now.hour <= 9) && (now.minute <= 59 ))
{
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}
else
{
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
}
if ((now.dow == 1) && (now.hour >= 15 && now.hour <= 21))
{
lcd.setCursor(0,2);
lcd.print(" Take Two Meds ");
}
if ((now.dow == 5) && (now.hour >= 3 && now.hour <= 9))
{
lcd.setCursor(0,2);
lcd.print(" Take only one Med ");
}
else
{
lcd.setCursor(0,2);
lcd.print(" No Med's Required");
}
delay(3000);
}
Just looking through the tiny window in transit. You may need an else in there, viz:
if ((now.dow == 1) && (now.hour >= 15 && now.hour <= 21))
{
}
else if ((now.dow == 5) && (now.hour >= 3 && now.hour <= 9))
{
}
else
{
}
there are 3 if/else statements in your code, the "No Med's Required" is an else condition for just the last one, not for all of them
Yes, that fixes it but points up the disadvantage of the start and stop times method… if the system wakes up between the start and stop hours, it will not start.
Whereas the interval test will be able to see that 2pm is between 10am and 5pm, for example.
a7
if this is a serious concern, it can be handled in some initialization code rather dealing with the issues using range, a condition, that need to handle when crossing midnight, for example
the more i look at this code, the more is seems that recognizing events would make such code easier