Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Cat Box Timer
- #include <TimerOne.h>
- #include <Wire.h>
- #include <MultiFuncShield.h>
- #include <TM1637Display.h>
- #define LED_1 13
- enum countUpModeValues
- {
- COUNTING_STOPPED,
- COUNTING,
- HELLOKITTY
- };
- byte countUpMode = HELLOKITTY;
- unsigned int seconds = 0;
- unsigned int hours = 0;
- unsigned int days = 0;
- static char strBuffer[4] = {0};
- void setup() {
- // put your setup code here, to run once:
- Timer1.initialize();
- MFS.initialize(&Timer1); // initialize multifunction shield library
- pinMode(LED_1, OUTPUT);
- digitalWrite(LED_1, LOW);
- //Clears the display
- MFS.write(" ");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- byte btn = MFS.getButton();
- switch (countUpMode)
- {
- case COUNTING_STOPPED:
- if (btn == BUTTON_1_SHORT_RELEASE)
- {
- // start the timer
- countUpMode = COUNTING;
- }
- else if (btn == BUTTON_1_LONG_PRESSED)
- {
- // reset the timer
- seconds = 0;
- hours = 0;
- days = 0;
- countUpMode = HELLOKITTY;
- }
- else if (btn == BUTTON_2_PRESSED || btn == BUTTON_2_LONG_PRESSED)
- {
- days++;
- //Stop days from going into the double digets it can't display
- if (days == 10) {
- days = 0;
- }
- MFS.write(buildStringForMFS(days, hours));
- }
- else if (btn == BUTTON_3_PRESSED || btn == BUTTON_3_LONG_PRESSED)
- {
- hours ++;
- if (hours >= 23)
- {
- hours = 0;
- days++;
- if (days == 10) {
- days = 0;
- }
- }
- MFS.write(buildStringForMFS(days, hours));
- }
- break;
- case COUNTING:
- if (btn == BUTTON_1_SHORT_RELEASE || btn == BUTTON_1_LONG_RELEASE)
- {
- // stop the timer
- countUpMode = COUNTING_STOPPED;
- }
- else
- {
- // continue counting up
- seconds++;
- if (seconds == 3600) //1 hour 3600
- {
- seconds = 0;
- hours++;
- }
- if (hours == 24) { //1 Day
- hours = 0;
- days++;
- }
- // Display not ment to show over 10 days so it must reset
- if (days == 10) {
- days = 0;
- }
- if (days > 4 && seconds == 0) {
- MFS.beep(20, 5, 2); // (beep x times, xx milliseconds on / xx off)
- }
- MFS.write(buildStringForMFS(days, hours));
- //Blink the light to signal timer is running
- if (digitalRead(LED_1) == LOW) {
- digitalWrite(LED_1, HIGH);
- }
- else {
- digitalWrite(LED_1, LOW);
- }
- delay(1000); //Delay one second
- //delay(10); // Speed up for testing
- }
- break;
- //Used for boot up and testing all lights
- case HELLOKITTY:
- countUpMode = COUNTING_STOPPED;
- for (int i = 0; i <= 2; i++) {
- MFS.write(" 00 ");
- delay(1000);
- MFS.write(" -- ");
- delay(100);
- }
- MFS.write(" 00 ");
- delay(2000);
- MFS.write("cAt");
- break;
- }
- }
- char *buildStringForMFS(int days, int hours) {
- if (hours > 9) {
- sprintf(strBuffer, "%d %d", days, hours);
- } else {
- sprintf(strBuffer, "%d %d", days, hours);
- }
- return strBuffer;
- }
RAW Paste Data