Advertisement
skizziks_53

Easy 24hr timer v1.0

Jul 20th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. /*
  2.   July 20, 2019
  3.   This is a sketch to trigger a pin to go high once every 24 hours.
  4.  
  5.   There is one button to shut off the alarm if it is going off.
  6.   There is a second button to reset the timer to zero, so that the alarm triggers immediately and then every 24 hours afterward.
  7.   There are also some serial messages to verify that most events are occurring properly.
  8. */
  9.  
  10. // Note: you cannot use pins zero and 1 if you use Serial messages!
  11.  
  12. int alarm_output_pin = 2; // This pin is the one that goes high every 24 hours.
  13. int alarm_shut_off_button_pin = 3; // This is to shut off whatever the alarm pin does.
  14. int reset_timer_button_pin = 4; // This is the button to reset the timer at the current time.
  15.  
  16. int LED_pin = 13; // This is the usual I/O pin that has the LED connected.
  17.  
  18. int seconds_counter = 0;
  19. int minutes_counter = 0;
  20. int hours_counter = 0;
  21.  
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   pinMode(alarm_output_pin, OUTPUT);
  26.   pinMode(LED_pin, OUTPUT);
  27.   pinMode(alarm_shut_off_button_pin, INPUT_PULLUP);
  28.   pinMode(reset_timer_button_pin, INPUT_PULLUP);
  29.   digitalWrite(LED_pin, LOW); // Some clone boards need you to write this pin low or else it will stay high.
  30.   digitalWrite(alarm_output_pin, LOW);
  31.   Serial.println("Exiting setup");
  32. }
  33.  
  34. void loop() {
  35.  
  36.   check_button_inputs();
  37.   check_alarm();
  38.   delay_one_second();
  39.   check_seconds_counter();
  40.   check_minutes_counter();
  41.   check_hours_counter();
  42.  
  43. }
  44.  
  45.  
  46. void check_button_inputs() {
  47.   // The part below is for shutting off the alarm.
  48.   if (digitalRead(alarm_shut_off_button_pin) == LOW) {
  49.     digitalWrite(alarm_output_pin, LOW);
  50.     Serial.println("Alarm shut-off button pressed");
  51.   }
  52.   // The part below resets the timer to all zero values, and the alarm should immediately trigger.
  53.   if (digitalRead(reset_timer_button_pin) == LOW) {
  54.     seconds_counter = 0;
  55.     minutes_counter = 0;
  56.     hours_counter = 0;
  57.     Serial.println("Alarm reset button pressed");
  58.   }
  59. }
  60.  
  61. void check_alarm() {
  62.   // This alarm is set to go off when all three countr values are zero.
  63.   // This is done so that when you press the timer-reset button (that resets all three values to zero) the alarm should go off immediately and you don't need to wait 24 hours to see if it works.
  64.   if (hours_counter == 0) {
  65.     if (minutes_counter == 0) {
  66.       if (seconds_counter == 0) {
  67.         // Anything that should happen when the alarm goes off, goes here.
  68.         digitalWrite(alarm_output_pin, HIGH);
  69.         Serial.println("Alarm is activated");
  70.       }
  71.     }
  72.   }
  73. }
  74.  
  75. void check_hours_counter() {
  76.   if (hours_counter == 24) {
  77.     hours_counter = 0;
  78.   }
  79. }
  80.  
  81. void check_minutes_counter() {
  82.   if (minutes_counter == 60) {
  83.     minutes_counter = 0;
  84.     hours_counter++;
  85.   }
  86. }
  87.  
  88. void check_seconds_counter() {
  89.   if (seconds_counter == 60) {
  90.     seconds_counter = 0;
  91.     minutes_counter++;
  92.   }
  93. }
  94.  
  95. void delay_one_second() {
  96.   delay(1000);
  97.   seconds_counter++;
  98.   check_to_blink_led();
  99. }
  100.  
  101. void check_to_blink_led() {
  102.   // This function is for blinking the pin-13 LED on and off once every ten seconds.
  103.   int blinkCheck = seconds_counter % 10; // Modulo: If seconds_counter is evenly divisible by ten, then turn the LED on. Otherwise, turn the LED off.
  104.   if (blinkCheck == 0) {
  105.     digitalWrite(LED_pin, HIGH);
  106.   }
  107.   else {
  108.     digitalWrite(LED_pin, LOW);
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement