Not sure if anyone is interested but someone may find it useful… A fairly simple menu on the serial port to setup an array to be used for a weekly access schedule. the array can then be checked with the checkSchedule function.
i have a stuct that holds username data, one element is: Username.Schedule that is a byte array of 21. i chose this because its really easy to store on an SD card with the arduino EDB library.
This is added to your code to call the menu function, i have it in the add user and update user menu’s:
while (!addSchedule()) SPARK_WLAN_Loop();
it will continue looping until the function is finished and returns a 1 for done or -1 for canceled.
and to check the schedule i load the Username,Schedule into RAM (this is done when the fingerprint reader has checked the print and determined whos it is)
boolean a = checkSchedule();
if its true the the bit was set and the user has access if not then they are not allowed in
the functions are below
void printSchedule(uint8_t days) {
Serial.println("Day Time 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00");
int a;
uint8_t day = 0;
while (day <= 6) {
day++;
a = days;
if (bitRead(a, day - 1)) {
if (day == 1) Serial.print(" Sunday : ");
if (day == 2) Serial.print(" Monday : ");
if (day == 3) Serial.print(" Tuesday : ");
if (day == 4) Serial.print("Wednesday : ");
if (day == 5) Serial.print(" Thursday : ");
if (day == 6) Serial.print(" Friday : ");
if (day == 7) Serial.print(" Saturday : ");
int j;
int k;
for (j = 0; j <= 7; j++) {
k = Username.Schedule[day - 1];
Serial.print(bitRead(k, j));
Serial.print(" ");
}
for (j = 0; j <= 7; j++) {
k = Username.Schedule[day + 6];
Serial.print(bitRead(k, j));
Serial.print(" ");
}
for (j = 0; j <= 7; j++) {
k = Username.Schedule[day + 13];
Serial.print(bitRead(k, j));
Serial.print(" ");
}
Serial.println(" ");
}
}
return;
}
boolean checkSchedule(void) {
if (Time.now() <= 1400000000) return true; //current time not set, so we let them in
uint8_t B; //stores byte the pointer
uint8_t t = Time.hour();
//Check the hour, see if its in the 1st 2nd or 3rd byte, then change it to set the bit
if (t >= 0 && t < 8) B = 0; // its in the 1st byte for the day
if (t >= 8 && t < 16) {
B = 7;
t -= 8;
} // its in the 2nd byte for the day, t sets back to bit 0
if (t >= 16 && t < 24) {
B = 14;
t -= 16;
}// its in the 3rd byte for the day, t sets back to bit 0
B += (Time.weekday() - 1); // B = B + Weekday number - 1 so it starts a 0 instead of 1
uint8_t b = Username.Schedule[B]; //holds the byte to check so we don't mess up the stored schedule
if (bitRead(b, t)) return true;
return false;
}
int addSchedule() {
uint8_t weekday;
uint8_t daystoset = 0;
char s[2];
char numascii[3];
int starttime;
int finishtime;
Serial.println("Choose a Weekday: Or Group:");
Serial.println(" 1. Sunday a. Weekdays");
Serial.println(" 2. Monday b. Weekends");
Serial.println(" 3. Tuesday c. Full Week");
Serial.println(" 4. Wednesday");
Serial.println(" 5. Thursday Press d when your Done");
Serial.println(" 6. Friday");
Serial.println(" 7. Saturday Or press q to Quit");
readString(s, 2);
if (s[0] == 'd') return 1;
if (s[0] == 'q') return -1;
if (s[0] == 'a') daystoset = 0b00111110;
if (s[0] == 'b') daystoset = 0b01000001;
if (s[0] == 'c') daystoset = 0b01111111;
weekday = atoi(s);
if (weekday >= 1 && weekday <= 7) bitSet(daystoset, weekday - 1);
printSchedule(daystoset);
Serial.println("Choose a Method");
Serial.println(" 1. Add Access");
Serial.println(" 2. Remove Access");
Serial.println(" 3. Back");
readString(s, 2);
uint8_t access = atoi(s);
if (access < 1 || access > 2) return 0;
Serial.print("Enter start time ie 21 for 9pm : ");
readString(numascii, 3);
starttime = atoi(numascii);
Serial.println(starttime);
if (starttime >= 24 || starttime < 0) {
Serial.println("Start time must be between 0 and 23");
return 0;
}
Serial.print("Enter End time ie 22 for 10pm (24 for Midnight): ");
readString(numascii, 3);
finishtime = atoi(numascii);
Serial.println(finishtime);
if (starttime >= finishtime) {
Serial.println("Start time must be before End time");
return 0;
}
if (finishtime > 24 || finishtime < 0) {
Serial.println("End time must be between 1 and 24");
return 0;
}
Serial.print("Set time from ");
Serial.print(starttime);
Serial.print(":00 to ");
Serial.print(finishtime);
Serial.print(":00 as ");
if (access == 1) Serial.println("Access Allowed");
if (access == 2) Serial.println("Access Denied");
Serial.print(" Press y to continue");
char c = readSelection();
if (c != 'y') return 0;
Serial.print("Saving schedule...");
int a;
uint8_t day = 0;
uint8_t starttime1;
while (day <= 6) {
day++;
starttime1 = starttime; //because we need to reset it after incrementing.
a = daystoset;
if (bitRead(a, day - 1)) {
while (starttime1 <= 7 && starttime1 < finishtime) {
if (access == 1) bitSet(Username.Schedule[day - 1], starttime1);
if (access == 2) bitClear(Username.Schedule[day - 1], starttime1);
starttime1++;
}
while (starttime1 <= 15 && starttime1 < finishtime) {
if (access == 1) bitSet(Username.Schedule[day + 6], starttime1 - 8);
if (access == 2) bitClear(Username.Schedule[day + 6], starttime1 - 8);
starttime1++;
}
while (starttime1 <= 23 && starttime1 < finishtime) {
if (access == 1) bitSet(Username.Schedule[day + 13], starttime1 - 16);
if (access == 2) bitClear(Username.Schedule[day + 13], starttime1 - 16);
starttime1++;
}
}
}
Serial.println("Done!");
return 0;
}