Turn on LEDs at a specific days

Hello guys, I am trying to make a project where specific LEDs turn on at a specific date. I am using Arduino Nano with RTC module DS1307.
I would like to ask you if there is a better and cleaner way than a bunch of else if statements like I use:

 if(tm.Month == 6){
    if(tm.Day == 19){
      breath(white);
      delay(10);
    }
  }
  else if (tm.Month == 7){
    if(tm.Day == 1){
      breath(yellow);
      delay(10);
    }
    else if (tm.Day == 5){
      breath(white);
      delay(10);
    }
    else if (tm.Day == 11){
      breath(green);
      delay(10);
    }
    else if (tm.Day == 15){
      breath(blue);
      delay(10);
    }
    else if (tm.Day == 25){
      breath(green);
      delay(10);
    }
    else if (tm.Day == 29){
      breath(yellow);
      delay(10);
    }     
  }
.
.
.

Glad for any help! :slight_smile:

Maybe you can use an and (&&) in the else if statement, so you can avoid the if...

You could have a table of times/dates, and a simple loop to examine.it.

@anon73444976 I made 4x 2D arrays and a function that goes through the arrays, but there is another problem that arose... I call the function four times always with different arguments in loop(). How do I avoid calling the functions every second (it seems quite demanding even tho the arrays are size of [7][3]? I am using a blinking LED so when I add delay, the LED also stops blinking :frowning: I would like to call the function every day or every two days because I am checking only day and month so I don't need to check every second

I would discard the DS1307 RTC module and use the DS3132. The latter has programmable alarms that could wake up your nano once a day for a single comparison, keeping the nano in deep sleep and ultra-low power the rest of the time.

Why do you care?

You could have a variable called prevDay and use it like this:

static byte prevDay = 99;
...
if(tm.Day != prevDay){
  //The day has changed, do stuff...
  ...
  prevDay = tm.Day;
}

but as already pointed out, there is no point avoiding execution of complex code unless you are saving CPU% or battery power or whatever for something else to use.

I don't know, it just felt wrong :smiley:
But I am glad to hear it's OK, thank you guys, and sorry for my noob questions, just starting to learn Arduino :smiley:

Another option:

int date = tm.Month * 100 + tm.Day;

  switch (date)
  {
case 619: // May 19th
case 705: // June 5th
      breath(white);
      break;

case 701: // June 1st
case 729: // June 29th
      breath(yellow);
      break;

case 711: // June 11th
case 725: // June 25th
      breath(green);
      break;

case 715: // June 15th
      breath(blue);
      break;
    }     

That's actually a pretty interesting and clever way of representing the day and month. Thank you for the suggestion :+1:

Why would "6" be May and "7" June...??

case 619: // May 19th
case 705: // June 5th

Off-by-one error.

Off-by-one error. I should have said June and July.

1 Like

hehehe....made me think for a minute or so though before I asked...... :thinking:

years ago had a teacher that used to do that just to see if we were awake..... :sweat_smile:

There is a TimeAlarms library which goes with (I think) the Time library. It can do all sorts of stuff and might be useful here.

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