Hello Friends,
I am trying to make a simple project that acts as a medicine reminder. This isn't my first project, however i am struggling to learn where my error is in my code. I have designed a circuit including red lights, green lights, DHT22 temp/humidity, along with a DS1302 RTC and LCD 20x4 display.
Both the time and date print correctly on the lcd screen and although the temp/ humidity print correctly the do flash every second or so; and by flash i mean that the characters disappear for a millisecond and then reappear as if the code is refreshing just those two lines. I would prefer if it wouldn't do that.
The first two lines of time and date do not flash like the temp and humidity do. However that is only a minor issue compared to the fact that my project doesn't technically work with the existing code I have.
Everything prints on the LCD fine (minus the flashing exception) and I have tested that the LEDs both red and green will in fact light up and switch on and off with different code. So I am trying to write the code so that the red LEDs would be on and the green off EXCEPT on Monday between 3 pm to 9pm and Friday 3 am to 9am when the green LEDs would come on and the red would be off.
When I run my existing code the green LEDs remain lit up and it simply never changes... I have done a fair amount of research but remain perplexed. I would appreciate any advice to help complete this project -
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);
pinMode(8, OUTPUT);
dht.begin();
rtc.init();
if (rtc.isHalted())
{
Serial.println("RTC is halted. Setting time...");
Ds1302::DateTime dt = {
.year = 24,
.month = Ds1302::MONTH_JUL,
.day = 6,
.hour = 8,
.minute = 30,
.second = 00,
.dow = Ds1302::DOW_TUE
};
rtc.setDateTime(&dt);
}
}
void loop()
{
// get the current time
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);
// digitalWrite(9, HIGH); // Turn on red lights **it is not time to take your medicine**//
lcd.init();
lcd.backlight();
lcd.setCursor(4,0);
lcd.print(Months[now.month - 1]); //01-12
lcd.print(" ");
if (now.day < 10) lcd.print('0');
lcd.print(now.day);
lcd.print(" ");
lcd.print("20");
lcd.print(now.year);
lcd.print(" ");
//lcd.print(".");
//lcd.print(now.second);
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(" ");
if(WeekDays[now.dow - 1 == 1 && now.hour >= 300 && now.hour < 900])
{
digitalWrite(8, HIGH); // Turn on Green lights **it is time to take your medicine**//
digitalWrite(9, LOW); // RED //
}else{
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
}
// if(WeekDays[now.dow - 1 == 6 && now.hour >= 15 && now.hour < 21])
// {
// digitalWrite(8, HIGH);
// digitalWrite(9, LOW);
// }else{
// digitalWrite(8, LOW);
// digitalWrite(9, HIGH);
// }
lcd.print(WeekDays[now.dow - 1]);
lcd.setCursor(0,2);
lcd.print("Temperature: ");
lcd.print(f);
lcd.setCursor(0,3);
lcd.print("Humidity: ");
lcd.print(h);
type or paste code here