Advertisement
Guest User

keypad matrix problem

a guest
Sep 4th, 2011
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.23 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <EEPROM.h>
  3. #include "EEPROMAnything.h"
  4.  
  5. const byte ROWS = 4; //four rows
  6. const byte COLS = 3; //three columns
  7. char keys[ROWS][COLS] = {
  8.  {'1','2','3'},
  9.  {'4','5','6'},
  10.  {'7','8','9'},
  11.  {'*','0','#'}
  12. };
  13. byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
  14. byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad
  15.  
  16. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  17.  
  18. //Define all the bomb states  
  19. #define READY 0
  20. #define ARMED 1
  21. #define DISARMED 2
  22. #define DETONATED 3
  23.  
  24. int second=30, minute=10, hour=0; // declare time variables
  25. int bombState=0; //0 = Ready, 1 = Armed, 2 = Disarmed, 3 = detonated
  26. int configMenuFlag = 0; //prevents configMenu from launching after a key is pressed
  27. int speakerPin = 5;//pin that the piezo buzzer is connected to.
  28.  
  29. //variables for buzzer chirp function
  30. static unsigned long currentMillis = 0;
  31. long previousTime = 0;//holds previous time in ms
  32. long previousDelay = 0;//holds previous time in ms since buzzer was active
  33.  
  34. int stop = 0;//function kill flag
  35. long code = 0;//keeps a running tally of the digits entered into the keypad
  36. int count = 0;//number of correct digits
  37.  
  38. char ArmCode[] = "8507840";//code to arm the bomb
  39. char disarmCode[] = "5248040";//code to disarm the bomb
  40. char adminCode[] = "4725226";//code to change timer
  41.  
  42. //configuration
  43. //structure for storing the arm code, disarm code, admin code, and time in the eeprom memory
  44. struct config_t
  45. {
  46.    long arm;
  47.    long disarm;
  48.    long admin;
  49.    int c_hour;
  50.    int c_minute;
  51.    int c_second;
  52. } configuration;
  53.  
  54. void setup()
  55. {
  56.  Serial.begin(9600);
  57.  backlightOn();
  58.  pinMode(speakerPin, OUTPUT);//sets the output pin for the piezo buzzer
  59.  
  60.  readConfig();//read in the arm, disarm, admin codes and timer
  61.  //saveConfig();
  62.  //display the default timer when prop boots up
  63.  clearLCD();
  64.  delay(1000);
  65.    selectLineTwo();
  66.    Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  67.    Serial.print(":"); // a colon between the hour and the minute
  68.    Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  69.    Serial.print(":"); // a colon between the minute and the second
  70.    Serial.println(second, DEC); // the second, sent to the screen in decimal format
  71.    delay(1000);
  72.    clearLCD();
  73. }
  74.  
  75. void loop(){
  76.    
  77.  switch (bombState) {
  78.  
  79. /***********************************************************
  80. *Ready state prepares bomb, waits for arm code to be input *
  81. *                                                          *
  82. ************************************************************/
  83.  case READY:
  84.    while (count < (sizeof(ArmCode) - 1)) {//loop until the full and correct arm code is entered
  85.      
  86.      selectLineTwo();
  87.      Serial.print("Enter ArmCode");
  88.      
  89.    selectLineOne();
  90.    Serial.print("Code"); //prompt for arm code
  91.   Serial.print(": ");
  92.  
  93.        
  94.    char key = '\0';
  95.      do{
  96.        key = keypad.getKey();
  97.        }
  98.     while(key == '\0');
  99.  
  100.   code = (code * 10) + (key - 48); //stores the current correct digits for display later
  101.    
  102.   if(key == '#' && configMenuFlag == 0) {//check if user wants to enter menu
  103.               key = '\0';
  104.               configMenu();// enter menu
  105.              count = 0;
  106.              code = 0;
  107.              clearLCD();
  108.              
  109.   }
  110.  
  111.  
  112.   else if(key == ArmCode[count]) {//if the key matches the code
  113.              count++;
  114.              configMenuFlag = 1;//once the code is started, the menu cannot be entered  
  115.          }
  116.  
  117.  // else { //if the key does not match the code, reset
  118.              //count = 0;
  119.              //code = 0;
  120.              //configMenuFlag = 1;//once the code is started, the menu cannot be entered
  121.              //clearLCD();
  122.    //      }
  123.    
  124. selectLineOne();
  125.    Serial.print("Code"); //prompt for arm code
  126.   Serial.print(": ");
  127.    Serial.print(code);//display correct digits that have been entered
  128.    
  129.  }//end while
  130.  
  131.  clearLCD();
  132.  bombState = ARMED;//when the correct code has been entered, move to armed state
  133.  count = 0;
  134.  code = 0;
  135.  
  136.  break;
  137.  
  138.  
  139. /***********************************************************
  140. *Armed *
  141. *                                                          *
  142. ************************************************************/
  143.  case ARMED:
  144.    
  145.    selectLineOne();
  146.    Serial.print("Code"); //prompt for disarm code
  147.   Serial.print(": ");
  148.  
  149.        
  150.    char key;
  151.      do{
  152.        key = keypad.getKey();
  153.        countdown();//displays countdown while simultaneously checking for disarm code
  154.      
  155.      if (second == 0 && minute == 0 && hour == 0) {
  156.      code = 0;
  157.      clearLCD();
  158.      bombState = DETONATED; //clear LCD and set bomb state to "detonated" when timer runs out  
  159.    break;  
  160.  }  
  161.        //if the correct code is entered, move to disarmed state
  162.    else if(count == (sizeof(disarmCode) - 1)){
  163.      code = 0;  
  164.      clearLCD();
  165.        bombState = DISARMED;
  166.    }
  167.      
  168.    }//end do
  169.     while(key == '\0' && bombState == ARMED);
  170.  
  171.   code = (code * 10) + (key - 48); //store correct digits for display later
  172.  
  173.   //if the key entered matches the disarm code, increase count.
  174.   if(key == disarmCode[count]) {
  175.              count++;
  176.            }
  177.            
  178.  
  179.  
  180.   else {
  181.              count = 0;
  182.              code = 0;
  183.              clearLCD();
  184.          }
  185.    
  186. selectLineOne();
  187.    Serial.print("Code"); //prompt for arm code
  188.   Serial.print(": ");
  189.    Serial.print(code);//display the current correct digits
  190.    
  191. break;
  192.  
  193. /*******************************************************
  194. *Detonated. activate buzzer for 8 beeps, then 1 long.
  195. *Print bomb detonated message to LCD.
  196. ********************************************************/
  197.  case DETONATED:
  198.  clearLCD();
  199.  
  200. digitalWrite(speakerPin, HIGH);//turn on buzzer
  201. delay(5000);//wait 5 seconds
  202. digitalWrite(speakerPin, LOW);//turn off buzzer
  203.  
  204.  selectLineOne();
  205.  Serial.print(">Bomb Detonated<"); //loop message informing user of bomb detonation.
  206.  
  207.  
  208.  do{
  209.     stop = 1;
  210.  }
  211.  while(stop == 1);//endless loop stops program
  212.  
  213. /**************************************************************
  214. *DISARMED. Counter stopped, displays "bomb disarmed"
  215. *
  216. **************************************************************/
  217.  case DISARMED:
  218.  
  219.  selectLineOne();
  220.    Serial.print("Bomb Disarmed"); //bomb has been disarmed, inform user.
  221.  
  222.  do{
  223.    int stop = 1;
  224.  }
  225.  while(stop == 1);//endless loop stops program
  226.  
  227.  
  228.  }//end case statements    
  229. }
  230.  
  231. /***********************************************************
  232. * Main countdown timer                                     *
  233. *                 countdown()                              *
  234. ************************************************************/
  235. void countdown(){
  236.  
  237.  static unsigned long lastTick = 0; // set up a local variable to hold the last time we decremented one second
  238.  
  239. // (static variables are initialized once and keep their values between function calls)
  240.  
  241. // decrement one second every 1000 milliseconds
  242.  if (second > 0) {
  243.      if (millis() - lastTick >= 1000) {
  244.          lastTick = millis();
  245.          second--;
  246.      }
  247.  }
  248.  
  249. // decrement one minute every 60 seconds
  250.  if (minute > 0) {
  251.      if (second <= 0) {
  252.          minute--;
  253.          second = 60; // reset seconds to 60
  254.          
  255.      }
  256.  }
  257.  
  258. // decrement one hour every 60 minutes
  259.  if (hour > 0) {
  260.      if (minute <= 0) {
  261.          hour--;
  262.          minute = 60; // reset minutes to 60
  263.      }//closes if
  264.  }//closes if
  265.  
  266. beepSecondaryBuzzer();
  267.  
  268.  //the code below beeps the siren once every minute.
  269.  /*
  270.  currentMillis = millis();
  271.  if (currentMillis - previousTime > interval)
  272.  {
  273.    previousTime = currentMillis;
  274.    previousDelay = currentMillis;
  275.    
  276.   digitalWrite(speakerPin, HIGH);//turn on buzzer
  277.  }
  278.  
  279.  if (currentMillis - previousDelay > 100) {//100ms chirp duration
  280.    digitalWrite(speakerPin, LOW);//turn off buzzer
  281.  }
  282.  */
  283.  
  284.  selectLineTwo();
  285.  
  286.  Serial.print("ARMED: ");
  287.  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  288.  Serial.print(":"); // a colon between the hour and the minute
  289.  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  290.  Serial.print(":"); // a colon between the minute and the second
  291.  Serial.println(second, DEC); // the second, sent to the screen in decimal format
  292. } //close countdown();
  293.  
  294.  
  295. /*********************************************************************
  296. *       Serial LCD disagnostic and general use tools                 *
  297. *   selectLineOne(); | selectLineTwo(); | goTo(); | clearLCD();      *
  298. *      backlightOn(); | backlightOff(); | serCommand();             *
  299. **********************************************************************/
  300. void selectLineOne(){  //puts the cursor at line 0 char 0.
  301.   Serial.print(0xFE, BYTE);   //command flag
  302.   Serial.print(128, BYTE);    //position
  303. }
  304. void selectLineTwo(){  //puts the cursor at line 0 char 0.
  305.   Serial.print(0xFE, BYTE);   //command flag
  306.   Serial.print(192, BYTE);    //position
  307. }
  308. void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
  309. if (position<16){ Serial.print(0xFE, BYTE);   //command flag
  310.              Serial.print((position+128), BYTE);    //position
  311. }else if (position<32){Serial.print(0xFE, BYTE);   //command flag
  312.              Serial.print((position+48+128), BYTE);    //position
  313. } else { goTo(0); }
  314. }
  315.  
  316. void clearLCD(){
  317.   Serial.print(0xFE, BYTE);   //command flag
  318.   Serial.print(0x01, BYTE);   //clear command.
  319. }
  320. void backlightOn(){  //turns on the backlight
  321.    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
  322.    Serial.print(157, BYTE);    //light level.
  323. }
  324. void backlightOff(){  //turns off the backlight
  325.    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
  326.    Serial.print(128, BYTE);     //light level for off.
  327. }
  328. void serCommand(){   //a general function to call the command flag for issuing all other commands  
  329.  Serial.print(0xFE, BYTE);
  330. }
  331.  
  332. /***********************************************************
  333. * admin code, arm code, disarm code, and timer editing     *
  334. * function.                                                *
  335. *                 configMenu();                            *
  336. ************************************************************/
  337. void configMenu(){
  338. int exitMenu = 0;
  339. int back = 0; //prevent user from exiting without typing in full code
  340. char menuPage; //prompt for Menu page
  341.  
  342. clearLCD();
  343. selectLineOne();
  344. Serial.print("Menu");//inform the user that they are entered the menu
  345. delay(2000);
  346. clearLCD();
  347.  
  348.  
  349.  code = 0;
  350.  count = 0;
  351.  while (count < (sizeof(adminCode) - 1)) {
  352.    selectLineTwo();
  353.   Serial.print("Enter AdminCode");  
  354.    
  355.    selectLineOne();
  356.    Serial.print("Code"); //prompt for admin code
  357.   Serial.print(": ");
  358.  
  359.        
  360.    char adminKey;
  361.      do{
  362.        adminKey = keypad.getKey(); //loop and wait for keypress
  363.        }
  364.     while(adminKey == '\0');
  365.  
  366.   code = (code * 10) + (adminKey - 48);//store correct digits for later use
  367.    
  368.   if(adminKey == '#') {
  369.              return;//return to the arm code state
  370.              count = 0;
  371.              code = 0;
  372.              clearLCD();
  373.   }
  374.  
  375.   //if the correct key is pressed
  376.   else if(adminKey == adminCode[count]) {
  377.              count++;
  378.            }
  379.  
  380.   else {
  381.              count = 0;
  382.              code = 0;
  383.              clearLCD();//if the key is incorrect, reset
  384.          }
  385.    
  386. selectLineOne();
  387.    Serial.print("Code"); //prompt for admin code
  388.   Serial.print(": ");
  389.    Serial.print(code);//display correct digits
  390.    
  391.  }//end while
  392.  
  393.  clearLCD();
  394. do { //display menu
  395. selectLineOne();
  396. Serial.print("1.Admin 2.Time");
  397. selectLineTwo();
  398. Serial.print("3.Arm 4.Disarm");
  399.  
  400.      do{
  401.        menuPage = keypad.getKey();  
  402.        } //keep reading if anything but 1 - 4 is entered
  403.     while(menuPage == '\0' || menuPage == '*' || menuPage ==  '0' || menuPage > 52);
  404.  
  405.  
  406. switch(menuPage) {
  407.  
  408.  case '1': // Edit admin code
  409.  
  410.  clearLCD();
  411.  selectLineTwo();
  412.  Serial.print("# To Exit");
  413.  delay(2000);
  414.  clearLCD();
  415.  
  416.   selectLineTwo();
  417.   Serial.print("AdminCode");
  418.  
  419.   selectLineOne();
  420.   Serial.print("Code"); //prompt for admin code
  421.   Serial.print(": ");
  422.    
  423.     count = 0;
  424.     code = 0;//
  425.    while(count < (sizeof(adminCode) - 1)) {
  426.    char adminCodeKey;
  427.    do{
  428.        adminCodeKey = keypad.getKey();//wait for a keypress
  429.        }
  430.     while(adminCodeKey == '\0');
  431.  
  432.   code = (code * 10) + (adminCodeKey - 48); //store correct digits for later use
  433.  
  434.  
  435.  if(adminCodeKey == '#' && back == 0) {
  436.              count = sizeof(adminCode);
  437.              code = 0;
  438.              exitMenu = 1;//bail out if # is pressed
  439.              clearLCD();
  440.   }
  441.  
  442.   else {
  443.     //if key matches code
  444.         adminCode[count] = adminCodeKey;
  445.              count++;
  446.              back = 1;
  447.            }
  448.    
  449.  
  450.  selectLineOne();
  451.    Serial.print("Code"); //prompt for arm code
  452.   Serial.print(": ");
  453.  Serial.print(code);
  454.    }//end admincode while
  455.    clearLCD();
  456.    
  457.    selectLineOne();
  458.    Serial.print(code);
  459.    selectLineTwo();
  460.    Serial.print("* to exit");//display new code and give user time to see it
  461.    char key1;
  462.      do{
  463.        key1 = keypad.getKey();  
  464.        }
  465.     while(key1 == '\0' || key1 != '*');//keep looping until user presses *
  466.  
  467.    break;
  468.    
  469.    case '2': //Timer
  470.    
  471.  clearLCD();
  472.  selectLineTwo();
  473.  Serial.print("# To Exit");
  474.  delay(2000);
  475.  clearLCD();
  476.    
  477.  char timeInt;
  478.  selectLineOne();
  479.  Serial.print("Enter Hours");
  480.  do{
  481.    timeInt = keypad.getKey();
  482.      }
  483.  while(timeInt == '\0' || timeInt < '0' || timeInt > '6');
  484.  
  485.  hour = (timeInt - '0') * 10;//get first digit and convert to int
  486.  do{
  487.    timeInt = keypad.getKey();
  488.      }
  489.  while(timeInt == '\0');
  490.  hour = (timeInt - '0') + hour;//get second digit.
  491.  clearLCD();
  492.  timeInt = 'L';
  493.  
  494.  Serial.print("Enter Minutes");
  495.  do{
  496.    timeInt = keypad.getKey();
  497.      }
  498.  while(timeInt == '\0' || timeInt < '0' || timeInt > '6');
  499.  while (timeInt < '0' || timeInt > '6'){
  500.    timeInt = keypad.getKey();
  501.  }
  502.  minute = (timeInt - '0') * 10;//get first digit and convert to int
  503.  do{
  504.    timeInt = keypad.getKey();
  505.      }
  506.  while(timeInt == '\0');
  507.  minute = (timeInt - '0') + minute;//get second digit.
  508.  clearLCD();
  509.  timeInt = 'L';
  510.  
  511.  Serial.print("Enter Seconds");
  512.  do{
  513.    timeInt = keypad.getKey();
  514.      }
  515.  while(timeInt == '\0' || timeInt < '0' || timeInt > '6');
  516.  while (timeInt < '0' || timeInt > '6'){
  517.    timeInt = keypad.getKey();
  518.  }
  519.  second = (timeInt - '0') * 10;//get first digit and convert to int
  520.  do{
  521.    timeInt = keypad.getKey();
  522.      }
  523.  while(timeInt == '\0');
  524.  second = (timeInt - '0') + second;//get second digit.
  525.  clearLCD();
  526.  timeInt = 'L';
  527.  
  528.  clearLCD();
  529.    
  530.    selectLineOne();
  531.    Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  532.    Serial.print(":"); // a colon between the hour and the minute
  533.    Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  534.    Serial.print(":"); // a colon between the minute and the second
  535.    Serial.println(second, DEC); // the second, sent to the screen in decimal format
  536.  
  537.    selectLineTwo();
  538.    Serial.print("* to exit");
  539.    char key2;
  540.      do{
  541.        key2 = keypad.getKey();  
  542.        }
  543.     while(key2 == '\0' || key2!= '*');//display new code and give user time to see it
  544.  
  545.    break;
  546.  
  547.  case '3': //Arm code
  548.  clearLCD();
  549.  selectLineTwo();
  550.  Serial.print("# To Exit");
  551.  delay(2000);
  552.  clearLCD();
  553.  
  554.   selectLineTwo();
  555.   Serial.print("ArmCode");
  556.  
  557.   selectLineOne();
  558.   Serial.print("Code"); //prompt for admin code
  559.   Serial.print(": ");
  560.    
  561.     count = 0;
  562.     code = 0;
  563.     back = 0;
  564.    
  565.    while(count < (sizeof(ArmCode) - 1)) {
  566.    char armCodeKey;
  567.    do{
  568.       armCodeKey = keypad.getKey();
  569.        }
  570.     while(armCodeKey == '\0');
  571.  
  572.   code = (code * 10) + (armCodeKey - 48);//store correct digits for later use
  573.  
  574.  
  575.  if(armCodeKey == '#' && back == 0) {
  576.              count = sizeof(ArmCode);
  577.              code = 0;
  578.              exitMenu = 1;
  579.              clearLCD();//bail out if exit key is pressed
  580.     }
  581.  
  582.   else {
  583.     //if key matches code
  584.         ArmCode[count] = armCodeKey;
  585.              count++;
  586.              back = 1;
  587.            }
  588.    
  589.  
  590.  selectLineOne();
  591.    Serial.print("Code"); //prompt for arm code
  592.   Serial.print(": ");
  593.  Serial.print(code);
  594.    }//end armcode while
  595.    clearLCD();
  596.    
  597.    selectLineOne();
  598.    Serial.print(code);
  599.    selectLineTwo();
  600.    Serial.print("* to exit");
  601.    char key3;
  602.      do{
  603.        key3 = keypad.getKey(); //display new code and give user time to see it
  604.        }
  605.     while(key3 == '\0' || key3!= '*');
  606.  
  607.    break;
  608.    
  609.  case '4': //disarm
  610.   clearLCD();
  611.  selectLineTwo();
  612.  Serial.print("# To Exit");
  613.  delay(2000);
  614.  clearLCD();
  615.  
  616.   selectLineTwo();
  617.   Serial.print("disarmCode");
  618.  
  619.   selectLineOne();
  620.   Serial.print("Code"); //prompt for admin code
  621.   Serial.print(": ");
  622.    
  623.     count = 0;
  624.     code = 0;
  625.     back = 0;
  626.    
  627.    while(count < (sizeof(disarmCode) - 1)) {
  628.    char disarmCodeKey;
  629.    do{
  630.       disarmCodeKey = keypad.getKey();
  631.        }
  632.     while(disarmCodeKey == '\0');
  633.  
  634.   code = (code * 10) + (disarmCodeKey - 48);
  635.  
  636.  
  637.  if(disarmCodeKey == '#' && back == 0) {
  638.              count = sizeof(disarmCode);
  639.              code = 0;
  640.              exitMenu = 1;
  641.              clearLCD();
  642.     }
  643.  
  644.   else {
  645.    
  646.         disarmCode[count] = disarmCodeKey;
  647.              count++;
  648.              back = 1;
  649.            }
  650.    
  651.  
  652.  selectLineOne();
  653.    Serial.print("Code"); //prompt for arm code
  654.   Serial.print(": ");
  655.  Serial.print(code);
  656.    }//end disarmcode while
  657.    clearLCD();
  658.    
  659.    selectLineOne();
  660.    Serial.print(code);
  661.    selectLineTwo();
  662.    Serial.print("* to exit");
  663.    char key4;
  664.      do{
  665.        key4 = keypad.getKey();//display new code and give user time to see it  
  666.        }
  667.     while(key4 == '\0' || key4 != '*');
  668.  
  669.    break;
  670.    
  671.   case '#'://exit case
  672.     exitMenu = 1;//exit menu if # is pressed
  673.     break;
  674.    
  675.     case 'L'://exit case no admin code
  676.     exitMenu = 1;
  677.     break;
  678.    
  679. }//end switch
  680.  
  681. }while(exitMenu == 0); //end menu while loop
  682.  
  683. configMenuFlag = 1;//set flag so menu cannot be opened later
  684. saveConfig();//write new configuration into memory
  685.  
  686. }//end configMenu
  687.  
  688.  
  689. /***********************************************************
  690. * saves the admin code, arm code, disarm code, and timer   *
  691. *  into EEPROM memory where they will survive a power down *
  692. *                 saveConfig();                              *
  693. ************************************************************/
  694.  
  695. void saveConfig() {
  696.  
  697.  configuration.arm = atol(ArmCode);
  698.  configuration.disarm = atol(disarmCode);
  699.  configuration.admin = atol(adminCode);
  700.  configuration.c_hour = hour;
  701.  configuration.c_minute = minute;
  702.  configuration.c_second = second;
  703.  
  704.  EEPROM_writeAnything(0, configuration);
  705.  
  706. }
  707.  
  708. /***********************************************************
  709. * Reads the admin code, arm code, disarm code, and timer   *
  710. *  from EEPROM memory and into local variables             *
  711. *                 readConfig();                              *
  712. ************************************************************/
  713.  
  714. void readConfig() {
  715.  
  716.  EEPROM_readAnything(0, configuration);
  717.  
  718.  ltoa(configuration.arm, ArmCode, 10);
  719.  ltoa(configuration.disarm,disarmCode,10);
  720.  ltoa(configuration.admin,adminCode,10);
  721.  hour = configuration.c_hour;
  722.  minute = configuration.c_minute;
  723.  second = configuration.c_second;
  724. }
  725.  
  726. /***********************************************************
  727. * chirps a buzzer at 3 different intervals  > 30 seconds   *
  728. *  < 30 seconds, and < 10 seconds                          *
  729. *                 beepSecondaryBuzzer()                    *
  730. ************************************************************/
  731.  
  732. void beepSecondaryBuzzer() {
  733.  
  734. int timeLeft = 0;//default, seconds > 30
  735.  
  736. if(hour == 0 && minute == 0 && second <= 30 && second > 10){
  737.  timeLeft = 1; //if less than 30 seconds but more than 10 seconds left
  738. }
  739.  
  740. else if (hour == 0 && minute == 0 && second <= 10 && second > 0){
  741.  timeLeft = 2; //if less than 10 seconds left
  742. }
  743.  
  744. switch(timeLeft){
  745.  
  746.  case 0:
  747.    currentMillis = millis();
  748.  if (currentMillis - previousTime > 60000)//1 minute interval
  749.  {
  750.    previousTime = currentMillis;
  751.    previousDelay = currentMillis;
  752.    
  753.   digitalWrite(speakerPin, HIGH);//turn on buzzer
  754.  }
  755.  
  756.  if (currentMillis - previousDelay > 100) {//100ms chirp duration
  757.    digitalWrite(speakerPin, LOW);//turn off buzzer
  758.  }
  759.  break;
  760.  
  761.  case 1:
  762.  
  763.  currentMillis = millis();
  764.  if (currentMillis - previousTime > 2000)// 2 second interval
  765.  {
  766.    previousTime = currentMillis;
  767.    previousDelay = currentMillis;
  768.    
  769.   digitalWrite(speakerPin, HIGH);//turn on buzzer
  770.  }
  771.  
  772.  if (currentMillis - previousDelay > 100) {//100ms chirp duration
  773.    digitalWrite(speakerPin, LOW);//turn off buzzer
  774.  }
  775.  
  776.  break;
  777.  
  778.  case 2:
  779.  
  780.  currentMillis = millis();
  781.  if (currentMillis - previousTime > 500)// 1/2 second interval
  782.  {
  783.    previousTime = currentMillis;
  784.    previousDelay = currentMillis;
  785.    
  786.   digitalWrite(speakerPin, HIGH);//turn on buzzer
  787.  }
  788.  
  789.  if (currentMillis - previousDelay > 100) {//100ms chirp duration
  790.    digitalWrite(speakerPin, LOW);//turn off buzzer
  791.  }
  792.  
  793.  break;
  794.  
  795. }//end case statement
  796.  
  797. }//end beepSecondaryBuzzer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement