skizziks_53

Reddit multi-interval timer v1.0

Oct 8th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. /*
  2.   Reddit multi-interval timer - Greenhouse edition - version 1.0
  3.   October 8, 2019
  4.  
  5.   Board: not specified
  6.   Additional hardware: see below
  7.  
  8.   The purpose of this sketch is to run a teperature check at {interval #1} time limit.
  9.   If the temperature is above a limit, then a cooler is turned on for {interval #2} amount of time.
  10.   After it turns off, it will not allow activation for {interval #3} time limit.
  11.  
  12.   This sketch has very short time limits to allow viewing the serial.print statements in a reasonable amount of time.
  13.   All the time limits must be stated in seconds, but this allows intervals up to 533 hours+.
  14.  
  15.   Note that this sketch doesn't actually work, as it has a function to generate a random number to provide a temperature to compare to.
  16.   You would need to take that code out and write in the code to check a real temperature sensor.
  17.  
  18.   Also there are no pins designated for operating the cooler, only a serial message when the cooler is turned on and turned off.
  19.  
  20. */
  21.  
  22. // Below is the amount of time in seconds that the temperature will be checked at, as long as the cooler has not run:
  23. int normal_temperature_check_interval_in_seconds = 2;
  24.  
  25. // Below is the amount of time that the cooler will run, if it is turned on:
  26. int cooler_running_time_in_seconds = 5;
  27.  
  28. // Below is the amount of time that the cooler will be disabled after it finishes running:
  29. int cooler_disable_time_in_seconds = 15;
  30.  
  31. float maximum_temperature_allowed = 80; // This is the temperature target you would have, above which the cooler will activate.
  32.  
  33.  
  34. float measured_sensor_temperature = 0; // This is to use for putting the sensor reading in. The program sets this value.
  35.  
  36. // Below is a variable that contains a value that tells what the program should currently be doing.
  37. int auto_cooler_state = 0; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
  38.  
  39. unsigned long one_second_begin_time = 0; // This variable is used to time a single second.
  40. unsigned long one_second_current_time = 0; // This variable is used to time a single second.
  41. int seconds_delay_counter = 0; // This variable is used to count the total seconds that is compared to the three intervals above.
  42.  
  43. int built_in_led = 13; // This is to use the built-in LED to blink to show that the timer is running.
  44. bool blink_state = false; // This is to change the pin on and off.
  45.  
  46.  
  47.  
  48. void setup() {
  49.   Serial.begin(9600);
  50.   pinMode(built_in_led, OUTPUT);
  51.   digitalWrite(built_in_led, LOW); // With some of the China clones, the pin-13 LED goes high unless you declare it as output and write it LOW.
  52.   // I didn't declare any sensor or cooler pins here because there is just place-holder functions for reading the sensor and operating the cooler.
  53.   Serial.println("Exiting setup()");
  54.   Serial.print("Normal temperature checks are performed every ");
  55.   Serial.print(normal_temperature_check_interval_in_seconds);
  56.   Serial.println(" seconds.");
  57. }
  58.  
  59. void loop() {
  60.   switch (auto_cooler_state) {
  61.     case 0:
  62.       // Normal checking the temperature
  63.       check_temperature_sensor_timer();
  64.       break;
  65.     case 1:
  66.       // Running the cooler
  67.       check_cooler_timer();
  68.       break;
  69.     case 2:
  70.       // Wating for the after-cooler delay
  71.       check_to_re_enable_cooler();
  72.       break;
  73.   }
  74. }
  75.  
  76.  
  77. void check_temperature_sensor_timer() {
  78.   check_one_second_timer();
  79.   if (seconds_delay_counter >= normal_temperature_check_interval_in_seconds) {
  80.     seconds_delay_counter = 0; // Reset the seconds counter
  81.     check_temperature_sensor();
  82.     if (measured_sensor_temperature > maximum_temperature_allowed) {
  83.       auto_cooler_state = 1; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
  84.       turn_the_cooler_on();
  85.     }
  86.   }
  87. }
  88.  
  89.  
  90. void check_cooler_timer() {
  91.   check_one_second_timer();
  92.   if (seconds_delay_counter >= cooler_running_time_in_seconds) {
  93.     seconds_delay_counter = 0; // Reset the seconds counter
  94.     // Switch to the next state, and turn the cooler off.
  95.     auto_cooler_state = 2; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
  96.     turn_the_cooler_off();
  97.   }
  98. }
  99.  
  100.  
  101. void check_to_re_enable_cooler() {
  102.   check_one_second_timer();
  103.   if (seconds_delay_counter >= cooler_disable_time_in_seconds) {
  104.     seconds_delay_counter = 0; // Reset the seconds counter
  105.     // Switch to the beginning state, and show the cooler re-enable message.
  106.     auto_cooler_state = 0; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
  107.     show_cooler_enable_message();
  108.   }
  109. }
  110.  
  111.  
  112. void turn_the_cooler_on() {
  113.   // This is the function that would turn the cooler system on.
  114.   // For this example there is just a serial.print to show that it is working:
  115.   Serial.print("Cooler turning on for ");
  116.   Serial.print(cooler_running_time_in_seconds);
  117.   Serial.println(" seconds.");
  118. }
  119.  
  120. void turn_the_cooler_off() {
  121.   // This is the function that would turn the cooler system off.
  122.   // For this example there is just a serial.print to show that it is working:
  123.   Serial.println("Cooler turning off.");
  124.   Serial.print("Cooler disabled for ");
  125.   Serial.print(cooler_disable_time_in_seconds);
  126.   Serial.println(" seconds.");
  127. }
  128.  
  129. void show_cooler_enable_message() {
  130.   // This function doesn't need to do anything more, it is just to show that the cooler was re-enabled.
  131.   Serial.println("Cooler delay completed.");
  132.   Serial.print("Normal temperature checks resuming every ");
  133.   Serial.print(normal_temperature_check_interval_in_seconds);
  134.   Serial.println(" seconds.");
  135. }
  136.  
  137. void check_temperature_sensor() {
  138.   // This function just generates a random number for testing purposes.
  139.   // You would need to change this function to actually read a temperature sensor that you had connected.
  140.   Serial.print("Target temperature = ");
  141.   Serial.println(maximum_temperature_allowed);
  142.   measured_sensor_temperature = random(70, 83);
  143.   Serial.print("Temperature check = ");
  144.   Serial.println(measured_sensor_temperature);
  145. }
  146.  
  147. void check_one_second_timer() {
  148.   // All this function does is count passing seconds and increment the seconds_delay_counter value.
  149.   one_second_current_time = millis();
  150.   if (one_second_current_time >= one_second_begin_time) {
  151.     if (one_second_current_time >= (one_second_begin_time + 1000)) {
  152.       seconds_delay_counter++;
  153.       blink_the_timer_led();
  154.       one_second_begin_time = millis();
  155.     }
  156.   }
  157.   else {
  158.     one_second_begin_time = millis();
  159.   }
  160. }
  161.  
  162. void blink_the_timer_led() {
  163.   if (blink_state == true) {
  164.     blink_state = false;
  165.   }
  166.   else {
  167.     blink_state = true;
  168.   }
  169.   digitalWrite(built_in_led, blink_state);
  170. }
  171.  
  172.  
  173.  
  174. // ~~~~~~~~ the end ~~~~~~~~~~~
Add Comment
Please, Sign In to add comment