Checking for weekend date

Hi,

My alarm clock has a switch which enables/disables the alarm from tripping on weekends.

I just cannot improve the following code which is pretty messy.

Any help is appreciated.

TIA

#include <TimeLib.h>

bool weekend = false;  //set by switch on = true alarm will trip on weekends
bool alarmSet = false;  //set by switch

void setup() {
  //  ....
}

void loop() {

  //Sun=1, Mon=2, ... Sat=7
  if (alarmSet && (weekday() >= 2 && weekday() <= 6)) soundAlarm(); //weekday check
  if (alarmSet && (weekday() == 6 || weekday() == 7) && weekend == true) soundAlarm(); //weekend check

  //or...

  if ((alarmSet && (weekday() >= 2 && weekday() <= 6))
      || (alarmSet && (weekday() == 6 || weekday() == 7) && weekend == true) soundAlarm();
}

void soundAlarm() {
  //....
}
if (alarmSet && (weekday() == 6 || weekday() == 7) && weekend == true) soundAlarm();

Why are you checking for Friday and Saturday? (6 and 7)? How is the 'weekend' flag generated?

The weekend flag is generated by a switch. With your code, the alarm will trip only on weekends

typedef enum {
    dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday
} timeDayOfWeek_t;

If your weekend is like my weekend then it's Saturday and Sunday which are days 1 and 7 according to the Time library.

Once you know that the weekday is 1 or 7, you don't need to try to see if weekend is true, you already know that by the weekday. That would be a redundant check.

It's really hard to understand that code anyway. It calls the same function on every day but Sunday. So what's the point of checking at all if all roads lead to the same place?

Sorry it's a typo. It should be

if (alarmSet && (weekday() == 1 || weekday() == 7) && weekend == true) soundAlarm();

This line checks if it's a weekend and if the alarm clock is set to trip on weekends.

OK, what's the problem then. Is it not tripping?

I was looking for a way to simplify the code.

It looks pretty simple to me already. Those are all the things you need to check. I'm not sure what else you could do there. The way with two ifs seems a little more readable to me. But it's 6 of one or half a dozen of the other.

ok. Thank you