So, i propose to traverse an array of structs to find out if today is a holiday, and then execute some code accordingly. I create an array of structs with the Month, Day and a pointer to a function for that day...
struct Date {
int month, day;
void(*holidayFunction)();
};
Date holidays[] = {
{1, 1, newYear},
{2, 14, valentine},
{7, 4, fourthOfJuly}
};
Date today = {13, 32, everyday};
void setup()
{
Serial.begin(9600);
today.month = 1;
today.day = 2;
bool holiday = false;
for (int i = 0; i < sizeof(holidays)/sizeof(holidays[0]); i++)
{
if (holidays[i].month == today.month && holidays[i].day == today.day)
{
holidays[i].holidayFunction();
holiday = true;
break;
}
}
if (!holiday)
{
today.holidayFunction();
}
Serial.println("finished setup");
}
void loop()
{
}
void newYear(void)
{
Serial.println("Happy New Year!!");
}
void valentine(void)
{
Serial.println("Happy St Valentines Day!!");
}
void fourthOfJuly(void)
{
Serial.println("Happy Birthday USA!!");
}
void everyday()
{
Serial.println("Just any old day");
}
So (and assume for the sake of argument we are dealing with a much larger array) it was proposed to me that it may be more efficient to use a look-up table. Searching the internet, I am unable to locate an example that would correspond to my specific example (a two dimensional array, essentially with a finite number of possible intersections of Month and Day. Further, if I could sequence the table such that the entire table does not have to be traversed in order to see that the day is not special...
Anyone have a recommendation on how to create what I request?