kensuaga

Arduino Cat Box Timer (Hero Multi Function Shield)

Jan 7th, 2021
613
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Cat Box Timer
  2.  
  3. #include <TimerOne.h>
  4. #include <Wire.h>
  5. #include <MultiFuncShield.h>
  6. #include <TM1637Display.h>
  7.  
  8. #define LED_1 13
  9.  
  10. enum countUpModeValues
  11. {
  12.   COUNTING_STOPPED,
  13.   COUNTING,
  14.   HELLOKITTY
  15. };
  16.  
  17. byte countUpMode = HELLOKITTY;
  18.  
  19. unsigned int seconds = 0;
  20. unsigned int hours = 0;
  21. unsigned int days = 0;
  22.  
  23. static char strBuffer[4] = {0};
  24.  
  25.  
  26.  
  27. void setup() {
  28.   // put your setup code here, to run once:
  29.   Timer1.initialize();
  30.   MFS.initialize(&Timer1); // initialize multifunction shield library
  31.  
  32.   pinMode(LED_1, OUTPUT);
  33.  
  34.   digitalWrite(LED_1, LOW);
  35.  
  36.   //Clears the display
  37.   MFS.write("    ");
  38.  
  39. }
  40.  
  41.  
  42. void loop() {
  43.   // put your main code here, to run repeatedly:
  44.  
  45.   byte btn = MFS.getButton();
  46.  
  47.   switch (countUpMode)
  48.   {
  49.     case COUNTING_STOPPED:
  50.       if (btn == BUTTON_1_SHORT_RELEASE)
  51.       {
  52.         // start the timer
  53.         countUpMode = COUNTING;
  54.       }
  55.       else if (btn == BUTTON_1_LONG_PRESSED)
  56.       {
  57.         // reset the timer
  58.         seconds = 0;
  59.         hours = 0;
  60.         days = 0;
  61.         countUpMode = HELLOKITTY;
  62.       }
  63.       else if (btn == BUTTON_2_PRESSED || btn == BUTTON_2_LONG_PRESSED)
  64.       {
  65.         days++;
  66.         //Stop days from going into the double digets it can't display
  67.         if (days == 10) {
  68.           days = 0;
  69.         }
  70.         MFS.write(buildStringForMFS(days, hours));
  71.  
  72.       }
  73.       else if (btn == BUTTON_3_PRESSED || btn == BUTTON_3_LONG_PRESSED)
  74.       {
  75.         hours ++;
  76.         if (hours >= 23)
  77.         {
  78.           hours = 0;
  79.           days++;
  80.           if (days == 10) {
  81.             days = 0;
  82.           }
  83.         }
  84.         MFS.write(buildStringForMFS(days, hours));
  85.       }
  86.       break;
  87.  
  88.  
  89.  
  90.  
  91.     case COUNTING:
  92.       if (btn == BUTTON_1_SHORT_RELEASE || btn == BUTTON_1_LONG_RELEASE)
  93.       {
  94.         // stop the timer
  95.         countUpMode = COUNTING_STOPPED;
  96.       }
  97.       else
  98.       {
  99.         // continue counting up
  100.         seconds++;
  101.  
  102.         if (seconds == 3600) //1 hour 3600
  103.         {
  104.           seconds = 0;
  105.           hours++;
  106.  
  107.         }
  108.         if (hours == 24) { //1 Day
  109.           hours = 0;
  110.           days++;
  111.         }
  112.         // Display not ment to show over 10 days so it must reset
  113.         if (days == 10) {
  114.           days = 0;
  115.         }
  116.         if (days > 4 && seconds == 0) {
  117.           MFS.beep(20, 5, 2); // (beep x times, xx milliseconds on / xx off)
  118.         }
  119.  
  120.         MFS.write(buildStringForMFS(days, hours));
  121.  
  122.  
  123.         //Blink the light to signal timer is running
  124.         if (digitalRead(LED_1) == LOW) {
  125.           digitalWrite(LED_1, HIGH);
  126.         }
  127.         else {
  128.           digitalWrite(LED_1, LOW);
  129.         }
  130.  
  131.         delay(1000); //Delay one second
  132.         //delay(10); // Speed up for testing
  133.       }
  134.  
  135.       break;
  136.  
  137.     //Used for boot up and testing all lights
  138.     case HELLOKITTY:
  139.       countUpMode = COUNTING_STOPPED;
  140.  
  141.       for (int i = 0; i <= 2; i++) {
  142.         MFS.write(" 00 ");
  143.         delay(1000);
  144.         MFS.write(" -- ");
  145.         delay(100);
  146.       }
  147.       MFS.write(" 00 ");
  148.       delay(2000);
  149.       MFS.write("cAt");
  150.  
  151.       break;
  152.   }
  153. }
  154.  
  155.  
  156. char *buildStringForMFS(int days, int hours) {
  157.  
  158.   if (hours > 9) {
  159.     sprintf(strBuffer, "%d %d", days, hours);
  160.   } else {
  161.     sprintf(strBuffer, "%d  %d", days, hours);
  162.   }
  163.   return strBuffer;
  164. }
  165.  
RAW Paste Data