by

Timed Reminders with the Alpha Clock Five (continued)

It turns out my last post wasn’t quite as accurate as I had hoped. Shoutout to Tyler for calling me out.

Since our publish schedule is bi-weekly it turns out my code would have actually reminded me on certain Thursdays that do not precede scheduled publish Tuesdays. My previous code would have just reminded me every single Thursday. So, again, thanks be to Tyler.

It is somewhat complicated to get the reminders to come out on the correct Thursdays. Our publish schedule is on the 2nd and 4th Tuesday of the month, so there are some months where you are reminded on the 1st and 3rd Thursday and others where you are reminded on the 2nd and 4th Thursday.

Here is some revised code that will remind me on the correct days:

//Put this at the beginning of the void UpdateDisplay (byte forceUpdate) method

int dayOfWeek = weekday();

int DaysInWeek = 7;

int firstDayOfMonth;

int daysFromFirstDay = (day() % DaysInWeek);
// if 0 daysFromFirstDay it's the next day
if (daysFromFirstDay == 0)
{
    firstDayOfMonth = (weekday() + 1) % DaysInWeek;

    firstDayOfMonth = (firstDayOfMonth == 0) ? 7 : firstDayOfMonth;
} else
{
    int factor = (weekday() - (daysFromFirstDay - 1)) < 0 ? -1 : 1;
    firstDayOfMonth = (factor * (weekday() - (daysFromFirstDay - 1))) % DaysInWeek;

    firstDayOfMonth = (firstDayOfMonth == 0) ? 7 : firstDayOfMonth;
}

int daysInFirstWeek = DaysInWeek - (firstDayOfMonth - 1);

int firstWeek = 1;
int secondWeek = daysInFirstWeek;
int thirdWeek = daysInFirstWeek + DaysInWeek;
int fourthWeek = (daysInFirstWeek + (DaysInWeek * 2));
int fifthWeek = (daysInFirstWeek + (DaysInWeek * 3));

boolean isFirstWeek = false;
boolean isSecondWeek = false;
boolean isThirdWeek = false;
boolean isFourthWeek = false;
boolean isFifthWeek = false;

if (day() >= fifthWeek){
    isFifthWeek = true;
} else if ( day() >= fourthWeek ) {
    isFourthWeek = true;
} else if ( day() >= thirdWeek ) {
    isThirdWeek = true;
} else if ( day() >= secondWeek ) {
    isSecondWeek = true;
} else if ( day() >= firstWeek ) {
    isFirstWeek = true;
}

boolean firstAndThird = (firstDayOfMonth == 1) ||
            (firstDayOfMonth == 2) ||
            (firstDayOfMonth == 3) ||
            (firstDayOfMonth == 4);

boolean secondAndFourth = (firstDayOfMonth == 5) ||
            (firstDayOfMonth == 6) ||
            (firstDayOfMonth == 7);

boolean isThursday = dayOfWeek == 5;
//Put this at the end of the if-else chain in the UpdateDisplay method

else if ((isFirstWeek || isThirdWeek) && firstAndThird && isThursday && second() % 15 == 0) {
    DisplayWordSequence(10);
} else if ((isSecondWeek || isFourthWeek) && secondAndFourth && isThursday && second() % 15 == 0) {
    DisplayWordSequence(10);
}

Write a Comment

Comment