Calling wrong function

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 :slight_smile:

Use serial monitor and Serial.print in strategic places displaying strategic variables. This often becomes an iteration. After the first running of test print new, or changed, test prints are needed.
Dry reading the code and finding the bug is often very time consuming.

Tell us what these do ?

I had the print in my checkDate.... functions to print out the day, month, hour, color of LED and everything looked OK

This is from DS1307RTC -> SetTime.
Without this part of code the tm.Day, tm.Hour etc... returns 0 instead of the actual day or hour

Note this only works at compile time.

They give the date and time of the compilation?

Obviously the standard printouts are not enough to help You find the bug. Scratch Your head and install pure technical debugging printouts giving You help.

Correct

Haha. Thanks! I think I used that somewhere some 30 years ago.....

Often used in setup( ) for things like a serial number or version number for the running sketch, however, cannot see the use in loop () . :thinking:

What do you mean? It's the function I made as well as the checkForDateInAdvance() and I want to call it every second or how often the loop() is called...

I put printouts in the functions and when I open the serial monitor the Hour, Month, and Day print out correctly. The thing is that e.g. the checkForDateInAdvance() get called correctly, then LED turns OFF correctly past 7 p.m., but the next day the checkForDateInAdvance() is called again instead of the checkForDate(), but correctly at 6 a.m.

Honestly, I am totally lost, can someone help?

__DATE__
__TIME__

The compiler uses these to get date and time from the PC that was current when the compiler did her/his/its job.

@LarryD
Once it gets the date and time, it is stored in DS1307 isn't it? And Arduino should read from this module the time and date. I understood that the DS1307 is something like a clock

Sorry if I have stupid questions but I am noob in this kind of stuff, learning Arduino for a week

These will be locked to the date and time of compiling.


Yes, your RTC can be read to retrieve the current date and time.

There should be examples that came with your RTC library showing how to read from the DS1307RTC.

Try the examples and report back here how you make out with them.

Basically, half of the code is from the example of DS1307.

It's reading the time and date correctly but still it only calls checkForDateInAdvance() even though it's the desired day and should call checkForDate()

I can't see what's wrong it the condition or functions...

So everyone one is on the same page, please show the sketch you are currently working with.

So the update:

I've changed the library to RTClib which is much easier to follow and use and I've just reused the functions and arrays I've made. So now it looks like it's working more or less correctly. The LED breaths and stays solid on the correct days.

The only issue I'm having is that the LED should start breathing or stay solid at 6 a.m. and it starts at roughly 7 a.m.
But it turns OFF correctly at 7 p.m. spot on.

@pmagowan @LarryD I always forget...

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;


char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

#define ARRAY_SIZE(my_array) ((sizeof(my_array))/(sizeof(my_array[0])))

//LED colors
int blue = 6;
int white = 11;
int yellow = 9;
int green = 10;

int mainColor = 0;

int plastic[7][3] = {{19, 3, -1},         
                     {1, 29, -1},   
                     {26, -1, -1},  
                     {23, -1, -1},  
                     {21, -1, -1},  
                     {18, -1, -1},  
                     {16, -1, -1}}; 

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, 21, -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}}; 

int Hour = 0;
int Month = 0;
int Day = 0;


/////////////-LEDS-//////////////////////
void stayOn(int color){
  analogWrite(color, 5);
}

void turnOff(int color){
  analogWrite(color, LOW);
}


void breath (int color)
{
  for(int a = 0; a < 15; a++)
  {
    analogWrite(color, a);
     if(a<20)
     {
      delay(50);
     }
     else
     {
      delay(10);
     }
  }
  delay(800);

  for(int a = 15; a >= 0; a--)
  {
    analogWrite(color, a);
     if(a<20)
     {
      delay(50);
     }
     else
     {
      delay(10);
     }
  }
  delay(800);
}
/////////////-LEDS=ENDs-////////////////////////////////////////////////////



void setup() {

  pinMode(white, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);

  Serial.begin(9600);
  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (!rtc.isrunning()) {
    Serial.println("RTC lost power, lets set the time!");
  
  // Comment out below lines once you set the date & time.
    // Following line sets the RTC to the date & time this sketch was compiled
    
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));   ////************************************/////
  
    // Following line sets the RTC with an explicit date & time
    // for example to set January 27 2017 at 12:56 you would call:
    // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
  }
}

void loop() {

    DateTime now = rtc.now();
    
    Serial.println("Current Date & Time: ");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    //delay(1000);

    Hour = now.hour();
    Day = now.day();
    Month = now.month();

    Serial.println(Hour);
    Serial.println(Day);
    Serial.println(Month);

    ////////-CHECKINGforDate-////////////////////

    if((checkForDateInAdvance(plastic, yellow) || 
    checkForDateInAdvance(all, white) ||
    checkForDateInAdvance(paper, blue) ||
    checkForDateInAdvance(bio, green)) && (Hour > 6 && Hour < 19)) 
    {
      breath(mainColor);
    }
    else if((checkForDate(plastic, yellow) || 
            checkForDate(all, white) ||
            checkForDate(paper, blue) ||
            checkForDate(bio, green)) && (Hour > 6 && Hour < 19))
    {
      stayOn(mainColor);
    }
    else{
      Serial.println("fail");
      turnOff(mainColor);
    }
    
    ////////-CHECKINGforDate=ENDs-//////////////

}


/////////////-DATEcheck-////////////////////////////////////////////////////
bool checkForDate(int my_array[7][3], int color){
  for(int i = 0; i<7; i++){
    for(int j = 0; j<3; j++){
      if(Month == i+6 && Day == my_array[i][j]){
        mainColor = color;
        Serial.print("Curr day");
        Serial.print("Day");
        Serial.println(Day);
        Serial.print("Month");
        Serial.println(Month);
        Serial.print("Hour");
        Serial.println(Hour);
        return true;
        break;
      }
    }
  }
  return false;
}

//1_day in advance

bool checkForDateInAdvance(int my_array[7][3], int color){
  for(int i = 0; i<7; i++){
    for(int j = 0; j<3; j++){
      if(Month == i+6 && Day == my_array[i][j]-1){
        mainColor = color;
        Serial.println("In advance");
        Serial.print("Day");
        Serial.println(Day);
        Serial.print("Month");
        Serial.println(Month);
        Serial.print("Hour");
        Serial.println(Hour);
        return true;
        break;
      }
    }
  }
  return false;
}
/////////////-DATEcheck=ENDs-//////////////////////

I don’t see the updated code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.