Timer mit Zahlenvergleich

/*********************************************************************

  • Serial LCD disagnostic and general use tools *
  • selectLineOne(); | selectLineTwo(); | goTo(); | lcd.clear(); *
  • backlightOn(); | backlightOff(); | serCommand(); *
    **********************************************************************/
    void selectLineOne(){ //puts the cursor at line 0 char 0.
    lcd.write(0xFE); //command flag
    lcd.write(128); //position
    }
    void selectLineTwo(){ //puts the cursor at line 0 char 0.
    lcd.write(0xFE); //command flag
    lcd.write(192); //position
    }
    void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
    if (position<16){ lcd.write(0xFE); //command flag
    lcd.write((position+128)); //position
    }else if (position<32){Serial.write(0xFE); //command flag
    lcd.write((position+48+128)); //position
    } else { goTo(0); }
    }

void clearLCD(){
lcd.write(0xFE); //command flag
lcd.write(0x01); //clear command.
}
void backlightOn(){ //turns on the backlight
lcd.write(0x7C); //command flag for backlight stuff
lcd.write(157); //light level.
}
void backlightOff(){ //turns off the backlight
lcd.write(0x7C); //command flag for backlight stuff
lcd.write(128); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
lcd.write(0xFE);
}

/***********************************************************

  • admin code, arm code, disarm code, and timer editing *
  • function. *
  • configMenu(); *
    ************************************************************/
    void configMenu(){
    int exitMenu = 0;
    int back = 0; //prevent user from exiting without typing in full code
    char menuPage; //prompt for Menu page

lcd.clear();
selectLineOne();
lcd.write("Menu");//inform the user that they are entered the menu
delay(2000);
lcd.clear();

code = 0;
count = 0;
while (count < (sizeof(adminCode) - 1)) {
selectLineTwo();
lcd.write("Enter AdminCode");

selectLineOne();
lcd.write("Code"); //prompt for admin code
lcd.write(": ");

char adminKey;
do{
adminKey = keypad.getKey(); //loop and wait for keypress
}
while(adminKey == '\0');

code = (code * 10) + (adminKey - 48);//store correct digits for later use

if(adminKey == '#') {
return;//return to the arm code state
count = 0;
code = 0;
lcd.clear();
}

//if the correct key is pressed
else if(adminKey == adminCode[count]) {
count++;
}

else {
count = 0;
code = 0;
lcd.clear();//if the key is incorrect, reset
}

selectLineOne();
lcd.write("Code"); //prompt for admin code
lcd.write(": ");
lcd.write(code);//display correct digits

}//end while

lcd.clear();
do { //display menu
selectLineOne();
lcd.write("1.Admin 2.Time");
selectLineTwo();
lcd.write("3.Arm 4.Disarm");

do{
menuPage = keypad.getKey();
} //keep reading if anything but 1 - 4 is entered
while(menuPage == '\0' || menuPage == '*' || menuPage == '0' || menuPage > 52);

switch(menuPage) {

case '1': // Edit admin code

lcd.clear();
selectLineTwo();
lcd.write("# To Exit");
delay(2000);
lcd.clear();

selectLineTwo();
lcd.write("AdminCode");

selectLineOne();
lcd.write("Code"); //prompt for admin code
lcd.write(": ");

count = 0;
code = 0;//
while(count < (sizeof(adminCode) - 1)) {
char adminCodeKey;
do{
adminCodeKey = keypad.getKey();//wait for a keypress
}
while(adminCodeKey == '\0');

code = (code * 10) + (adminCodeKey - 48); //store correct digits for later use

if(adminCodeKey == '#' && back == 0) {
count = sizeof(adminCode);
code = 0;
exitMenu = 1;//bail out if # is pressed
lcd.clear();
}

else {
//if key matches code
adminCode[count] = adminCodeKey;
count++;
back = 1;
}

selectLineOne();
lcd.write("Code"); //prompt for arm code
lcd.write(": ");
lcd.write(code);
}//end admincode while
lcd.clear();

selectLineOne();
lcd.write(code);
selectLineTwo();
lcd.write("* to exit");//display new code and give user time to see it
char key1;
do{
key1 = keypad.getKey();
}
while(key1 == '\0' || key1 != '*');//keep looping until user presses *

break;

case '2': //Timer

lcd.clear();
selectLineTwo();
lcd.write("# To Exit");
delay(2000);
lcd.clear();

char timeInt;
selectLineOne();
lcd.write("Enter Hours");
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0' || timeInt < '0' || timeInt > '6');

hour = (timeInt - '0') * 10;//get first digit and convert to int
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0');
hour = (timeInt - '0') + hour;//get second digit.
lcd.clear();
timeInt = 'L';

lcd.write("Enter Minutes");
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0' || timeInt < '0' || timeInt > '6');
while (timeInt < '0' || timeInt > '6'){
timeInt = keypad.getKey();
}
minute = (timeInt - '0') * 10;//get first digit and convert to int
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0');
minute = (timeInt - '0') + minute;//get second digit.
lcd.clear();
timeInt = 'L';

lcd.write("Enter Seconds");
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0' || timeInt < '0' || timeInt > '6');
while (timeInt < '0' || timeInt > '6'){
timeInt = keypad.getKey();
}
second = (timeInt - '0') * 10;//get first digit and convert to int
do{
timeInt = keypad.getKey();
}
while(timeInt == '\0');
second = (timeInt - '0') + second;//get second digit.
lcd.clear();
timeInt = 'L';

lcd.clear();

selectLineOne();
lcd.print(hour, DEC); // the hour, sent to the screen in decimal format
lcd.print(":"); // a colon between the hour and the minute
lcd.print(minute, DEC); // the minute, sent to the screen in decimal format
lcd.print(":"); // a colon between the minute and the second
lcd.println(second, DEC); // the second, sent to the screen in decimal format

selectLineTwo();
lcd.print("* to exit");
char key2;
do{
key2 = keypad.getKey();
}
while(key2 == '\0' || key2!= '*');//display new code and give user time to see it

break;

case '3': //Arm code
lcd.clear();
selectLineTwo();
lcd.print("# To Exit");
delay(2000);
lcd.clear();

selectLineTwo();
lcd.print("ArmCode");

selectLineOne();
lcd.print("Code"); //prompt for admin code
lcd.print(": ");

count = 0;
code = 0;
back = 0;

while(count < (sizeof(ArmCode) - 1)) {
char armCodeKey;
do{
armCodeKey = keypad.getKey();
}
while(armCodeKey == '\0');

code = (code * 10) + (armCodeKey - 48);//store correct digits for later use

if(armCodeKey == '#' && back == 0) {
count = sizeof(ArmCode);
code = 0;
exitMenu = 1;
lcd.clear();//bail out if exit key is pressed
}

else {
//if key matches code
ArmCode[count] = armCodeKey;
count++;
back = 1;
}

selectLineOne();
lcd.write("Code"); //prompt for arm code
lcd.write(": ");
lcd.write(code);
}//end armcode while
lcd.clear();

selectLineOne();
lcd.write(code);
selectLineTwo();
lcd.write("* to exit");
char key3;
do{
key3 = keypad.getKey(); //display new code and give user time to see it
}
while(key3 == '\0' || key3!= '*');

break;

case '4': //disarm
lcd.clear();
selectLineTwo();
lcd.write("# To Exit");
delay(2000);
lcd.clear();

selectLineTwo();
lcd.write("disarmCode");

selectLineOne();
lcd.write("Code"); //prompt for admin code
lcd.write(": ");

count = 0;
code = 0;
back = 0;

while(count < (sizeof(disarmCode) - 1)) {
char disarmCodeKey;
do{
disarmCodeKey = keypad.getKey();
}
while(disarmCodeKey == '\0');

code = (code * 10) + (disarmCodeKey - 48);

if(disarmCodeKey == '#' && back == 0) {
count = sizeof(disarmCode);
code = 0;
exitMenu = 1;
lcd.clear();
}

else {

disarmCode[count] = disarmCodeKey;
count++;
back = 1;
}

selectLineOne();
lcd.write("Code"); //prompt for arm code
lcd.write(": ");
lcd.write(code);
}//end disarmcode while
lcd.clear();

selectLineOne();
lcd.write(code);
selectLineTwo();
lcd.write("* to exit");
char key4;
do{
key4 = keypad.getKey();//display new code and give user time to see it
}
while(key4 == '\0' || key4 != '*');

break;

case '#'://exit case
exitMenu = 1;//exit menu if # is pressed
break;

case 'L'://exit case no admin code
exitMenu = 1;
break;

}//end switch

}while(exitMenu == 0); //end menu while loop

configMenuFlag = 1;//set flag so menu cannot be opened later

saveConfig();//write new configuration into memory

}//end configMenu

/***********************************************************

  • saves the admin code, arm code, disarm code, and timer *
  • into EEPROM memory where they will survive a power down *
  • saveConfig(); *
    ************************************************************/

void saveConfig() {

configuration.arm = atol(ArmCode);
configuration.disarm = atol(disarmCode);
configuration.admin = atol(adminCode);
configuration.c_hour = hour;
configuration.c_minute = minute;
configuration.c_second = second;

// EEPROM_writeAnything(0, configuration);

}

/***********************************************************

  • Reads the admin code, arm code, disarm code, and timer *
  • from EEPROM memory and into local variables *
  • readConfig(); *
    ************************************************************/

void readConfig() {

//EEPROM_readAnything(0, configuration);

ltoa(configuration.arm, ArmCode, 10);
ltoa(configuration.disarm,disarmCode,10);
ltoa(configuration.admin,adminCode,10);
hour = configuration.c_hour;
minute = configuration.c_minute;
second = configuration.c_second;
}

/***********************************************************

  • chirps a buzzer at 3 different intervals > 30 seconds *
  • < 30 seconds, and < 10 seconds *
  • beepSecondaryBuzzer() *
    ************************************************************/

void beepSecondaryBuzzer() {

int timeLeft = 0;//default, seconds > 30

if(hour == 0 && minute == 0 && second <= 30 && second > 10){
timeLeft = 1; //if less than 30 seconds but more than 10 seconds left
}

else if (hour == 0 && minute == 0 && second <= 10 && second > 0){
timeLeft = 2; //if less than 10 seconds left
}

switch(timeLeft){

case 0:
currentMillis = millis();
if (currentMillis - previousTime > 60000)//1 minute interval
{
previousTime = currentMillis;
previousDelay = currentMillis;

digitalWrite(speakerPin, HIGH);//turn on buzzer
}

if (currentMillis - previousDelay > 100) {//100ms chirp duration
digitalWrite(speakerPin, LOW);//turn off buzzer
}
break;

case 1:

currentMillis = millis();
if (currentMillis - previousTime > 2000)// 2 second interval
{
previousTime = currentMillis;
previousDelay = currentMillis;

digitalWrite(speakerPin, HIGH);//turn on buzzer
}

if (currentMillis - previousDelay > 100) {//100ms chirp duration
digitalWrite(speakerPin, LOW);//turn off buzzer
}

break;

case 2:

currentMillis = millis();
if (currentMillis - previousTime > 500)// 1/2 second interval
{
previousTime = currentMillis;
previousDelay = currentMillis;

digitalWrite(speakerPin, HIGH);//turn on buzzer
}

if (currentMillis - previousDelay > 100) {//100ms chirp duration
digitalWrite(speakerPin, LOW);//turn off buzzer
}

break;

}//end case statement

}//end beepSecondaryBuzzer();

Ich danke euch schon einmal im Voraus.

Gruß
Bastian