skizziks_53

Reddit 12-hour blinker timer v 1.0

Oct 26th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. /*
  2.   October 25, 2018
  3.   This is a 12-hour repeating timer sketch for Arduino. It begins counting 12 hours from whenever it is powered on, or the [reset] button is pressed.
  4.   When the time limit has been reached, the timer resets and calls the function time_event_function()
  5.   (the last function in the code below)
  6.   It also blinks the pin-13 LED on every 5 seconds, for one second.
  7.  
  8.   This code doesn't really do anything other than slowly blink the pin-13 LED, since there is not anything in the time_event_function().
  9. */
  10.  
  11. unsigned long oneSecond_beginTime = 0;
  12. unsigned long oneSecond_currentTime = 0;
  13.  
  14. int seconds_counter = 0;
  15. int minutes_counter = 0;
  16. int hours_counter = 0;
  17. int target_hours = 12; // This is the value in hours that you wanted the timer to wait for.
  18.  
  19. int ledCycle = 5; // This value means that the pin-13 LED will blink on every 5 seconds, and stay on for one second.
  20. // If you have a device that is set to wait very long times between acting,
  21. // it is wise to have it blink an indicator LED regularly so that you can tell that it is still working.
  22.  
  23.  
  24. void setup() {
  25.  
  26.   pinMode(13, OUTPUT); // The pin# 13 LED will be used as our blinker indicator.
  27.  
  28. }
  29.  
  30.  
  31. void loop() {
  32.   check_for_one_second();
  33.   check_seconds();
  34.   check_minutes();
  35.   check_hours_target();
  36. }
  37.  
  38. void check_for_one_second() {
  39.   // The code below is only for timing one second.
  40.   oneSecond_currentTime = millis();
  41.   if (oneSecond_currentTime >= oneSecond_beginTime) {
  42.     if (oneSecond_currentTime >= (oneSecond_beginTime + 1000)) { // ------> if 1 second has passed since the time was set in oneSecond_beginTime
  43.       seconds_counter += 1; // -------------------------------------------> then add 1 to the value seconds_counter here.
  44.       oneSecond_beginTime = millis(); // ---------------------------------> and then reset the oneSecond_beginTime to the current time.
  45.       cycle_blinkerLED();
  46.     }
  47.   }
  48.   else {
  49.     // This line is to reset the beginTime if the millis() value has rolled over.
  50.     oneSecond_beginTime = millis();
  51.   }
  52. }
  53.  
  54. void cycle_blinkerLED() {
  55.   // Below is what blinks the pin-13 LED on and off:
  56.   int x = seconds_counter % ledCycle;
  57.   if (x == 0) {
  58.     // ledCycle is set to 5.
  59.     // So every time that the seconds_counter value is evenly divisible by the ledCycle value,
  60.     // The ledBlinkPin will get turned on.
  61.     digitalWrite(13, 1);
  62.   }
  63.   else {
  64.     // Every time that the seconds_counter value is NOT evenly divisible by the ledCycle value, the ledBlinkPin will get turned off.
  65.     // And this check gets run every second, so the LED will only stay on for 1 second at a time.
  66.     // This part means it will get turned 'off' 80% of all the 1-second intervals,
  67.     // but repeatedly turning it 'on' or 'off' when it is already set {that way} doesn't harm it.
  68.     digitalWrite(13, 0);
  69.   }
  70. }
  71.  
  72. void check_seconds() {
  73.   if (seconds_counter >= 60) {
  74.     seconds_counter = 0;
  75.     minutes_counter += 1;
  76.   }
  77. }
  78.  
  79. void check_minutes() {
  80.   if (minutes_counter >= 60) {
  81.     minutes_counter = 0;
  82.     hours_counter += 1;
  83.   }
  84. }
  85.  
  86. void check_hours_target() {
  87.   if (hours_counter >= target_hours) {
  88.     hours_counter = 0;
  89.     target_reached();
  90.   }
  91. }
  92.  
  93. void target_reached() {
  94.   time_event_function();
  95.   reset_timer();
  96. }
  97.  
  98. void reset_timer() {
  99.   seconds_counter = 0;
  100.   minutes_counter = 0;
  101.   hours_counter = 0;
  102. }
  103.  
  104. void time_event_function() {
  105.   // This is the place where you put your own commands, for whatever you want to happen every 12 hours.
  106.  
  107. }
  108.  
  109.  
  110. // [the end]
Add Comment
Please, Sign In to add comment