Hello, I am here again and need your help. I am using Arduino Nano ATmega 328P with RTC DS1307 module.
I am using a 2-dimensional array to represent month and day. I have 2 functions to check for date. One check if "today" is the actual day (checkForDate()) and the second check if "today" is the day before the desired day (checkForDateInAdvance()).
When the function return true, the LED is either breathing (day before desired) or stays solid ON (desired day). Also, there is a condition that the LED should breath or stay ON from 6 a.m. to 7 p.m.
The problem is that when I run it e.g. day before the desired day, the function checkForDateInAdvance() works, it also turns OFF past 7 p.m. but the next day (desired day) at 6 a.m. it starts to breath again instead of staying solid ON.
When I upload the code to Arduino it always recognizes the date correctly so I think the issue is somewhere in the loop()
Hope I explained it well... Feel free to ask for further info.
I have no idea what I am doing wrong TBH...
Here is the code:
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include "LowPower.h"
#define ARRAY_SIZE(my_array) ((sizeof(my_array))/(sizeof(my_array[0])))
tmElements_t tm;
//****************************************************//
//****************************************************//
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
int plastic[7][3] = {{19, 3, -1}, //Jun
{1, 29, -1}, //Jul
{26, -1, -1}, //Aug
{23, -1, -1}, //Sep
{21, -1, -1}, //Oct
{18, -1, -1}, //Nov
{16, -1, -1}}; //Dec
int bio[7][3] = {{13, 27, -1},
{11, 25, -1},
{8, 22, -1},
{5, 19, -1},
{3, 17, 31},
{14, 28, -1},
{12, -1, -1}};
int all[7][3] = {{7, -1, -1},
{5, 18, -1},
{2, 30, -1},
{27, -1, -1},
{25, -1, -1},
{22, -1, -1},
{20, -1, -1}};
int paper[7][3] = {{17, -1, -1},
{15, -1, -1},
{12, -1, -1},
{9, -1, -1},
{7, -1, -1},
{4, -1, -1},
{2, 30, -1}};
//LED colors
int blue = 6;
int white = 11;
int yellow = 9;
int green = 10;
int mainColor = 0;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 5;
void stayOn(int color){
analogWrite(color, 5);
}
void turnOff(int color){
analogWrite(color, LOW);
}
void breath (int color)
{
for(int a = 0; a < 35; a++)
{
analogWrite(color, a);
if(a<20)
{
delay(50);
}
else
{
delay(10);
}
}
for(int a = 35; a >= 0; a--)
{
analogWrite(color, a);
if(a<20)
{
delay(50);
}
else
{
delay(10);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
//RTC READ time///////////////////////////////////////////////////////////////////////
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}
void loop() {
tmElements_t tm;
//RTC READ time
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
if((checkForDateInAdvance(plastic, yellow) ||
checkForDateInAdvance(all, white) ||
checkForDateInAdvance(paper, blue) ||
checkForDateInAdvance(bio, green)) && (tm.Hour > 6 && tm.Hour < 19))
{
breath(mainColor);
}
else if((checkForDate(plastic, yellow) ||
checkForDate(all, white) ||
checkForDate(paper, blue) ||
checkForDate(bio, green)) && (tm.Hour > 6 && tm.Hour < 19))
{
stayOn(mainColor);
}
else{
Serial.println("fail");
turnOff(mainColor);
}
}//END LOOP
//RTC SET time
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
//RTC SET time END
//RTC READ time
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
//RTC READ time END
bool checkForDate(int my_array[7][3], int color){
getDate(__DATE__);
getTime(__TIME__);
for(int i = 0; i<7; i++){
for(int j = 0; j<3; j++){
if(tm.Month == i+6 && tm.Day == my_array[i][j]){
mainColor = color;
return true;
break;
}
}
}
return false;
}
//1_day in advance
bool checkForDateInAdvance(int my_array[7][3], int color){
getDate(__DATE__);
getTime(__TIME__);
for(int i = 0; i<7; i++){
for(int j = 0; j<3; j++){
if(tm.Month == i+6 && tm.Day == my_array[i][j]-1){
mainColor = color;
return true;
break;
}
}
}
return false;
}
Glad for any help ![]()