Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- October 25, 2018
- 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.
- When the time limit has been reached, the timer resets and calls the function time_event_function()
- (the last function in the code below)
- It also blinks the pin-13 LED on every 5 seconds, for one second.
- 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().
- */
- unsigned long oneSecond_beginTime = 0;
- unsigned long oneSecond_currentTime = 0;
- int seconds_counter = 0;
- int minutes_counter = 0;
- int hours_counter = 0;
- int target_hours = 12; // This is the value in hours that you wanted the timer to wait for.
- int ledCycle = 5; // This value means that the pin-13 LED will blink on every 5 seconds, and stay on for one second.
- // If you have a device that is set to wait very long times between acting,
- // it is wise to have it blink an indicator LED regularly so that you can tell that it is still working.
- void setup() {
- pinMode(13, OUTPUT); // The pin# 13 LED will be used as our blinker indicator.
- }
- void loop() {
- check_for_one_second();
- check_seconds();
- check_minutes();
- check_hours_target();
- }
- void check_for_one_second() {
- // The code below is only for timing one second.
- oneSecond_currentTime = millis();
- if (oneSecond_currentTime >= oneSecond_beginTime) {
- if (oneSecond_currentTime >= (oneSecond_beginTime + 1000)) { // ------> if 1 second has passed since the time was set in oneSecond_beginTime
- seconds_counter += 1; // -------------------------------------------> then add 1 to the value seconds_counter here.
- oneSecond_beginTime = millis(); // ---------------------------------> and then reset the oneSecond_beginTime to the current time.
- cycle_blinkerLED();
- }
- }
- else {
- // This line is to reset the beginTime if the millis() value has rolled over.
- oneSecond_beginTime = millis();
- }
- }
- void cycle_blinkerLED() {
- // Below is what blinks the pin-13 LED on and off:
- int x = seconds_counter % ledCycle;
- if (x == 0) {
- // ledCycle is set to 5.
- // So every time that the seconds_counter value is evenly divisible by the ledCycle value,
- // The ledBlinkPin will get turned on.
- digitalWrite(13, 1);
- }
- else {
- // Every time that the seconds_counter value is NOT evenly divisible by the ledCycle value, the ledBlinkPin will get turned off.
- // And this check gets run every second, so the LED will only stay on for 1 second at a time.
- // This part means it will get turned 'off' 80% of all the 1-second intervals,
- // but repeatedly turning it 'on' or 'off' when it is already set {that way} doesn't harm it.
- digitalWrite(13, 0);
- }
- }
- void check_seconds() {
- if (seconds_counter >= 60) {
- seconds_counter = 0;
- minutes_counter += 1;
- }
- }
- void check_minutes() {
- if (minutes_counter >= 60) {
- minutes_counter = 0;
- hours_counter += 1;
- }
- }
- void check_hours_target() {
- if (hours_counter >= target_hours) {
- hours_counter = 0;
- target_reached();
- }
- }
- void target_reached() {
- time_event_function();
- reset_timer();
- }
- void reset_timer() {
- seconds_counter = 0;
- minutes_counter = 0;
- hours_counter = 0;
- }
- void time_event_function() {
- // This is the place where you put your own commands, for whatever you want to happen every 12 hours.
- }
- // [the end]
Add Comment
Please, Sign In to add comment