Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit multi-interval timer - Greenhouse edition - version 1.0
- October 8, 2019
- Board: not specified
- Additional hardware: see below
- The purpose of this sketch is to run a teperature check at {interval #1} time limit.
- If the temperature is above a limit, then a cooler is turned on for {interval #2} amount of time.
- After it turns off, it will not allow activation for {interval #3} time limit.
- This sketch has very short time limits to allow viewing the serial.print statements in a reasonable amount of time.
- All the time limits must be stated in seconds, but this allows intervals up to 533 hours+.
- 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.
- You would need to take that code out and write in the code to check a real temperature sensor.
- Also there are no pins designated for operating the cooler, only a serial message when the cooler is turned on and turned off.
- */
- // Below is the amount of time in seconds that the temperature will be checked at, as long as the cooler has not run:
- int normal_temperature_check_interval_in_seconds = 2;
- // Below is the amount of time that the cooler will run, if it is turned on:
- int cooler_running_time_in_seconds = 5;
- // Below is the amount of time that the cooler will be disabled after it finishes running:
- int cooler_disable_time_in_seconds = 15;
- float maximum_temperature_allowed = 80; // This is the temperature target you would have, above which the cooler will activate.
- float measured_sensor_temperature = 0; // This is to use for putting the sensor reading in. The program sets this value.
- // Below is a variable that contains a value that tells what the program should currently be doing.
- int auto_cooler_state = 0; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
- unsigned long one_second_begin_time = 0; // This variable is used to time a single second.
- unsigned long one_second_current_time = 0; // This variable is used to time a single second.
- int seconds_delay_counter = 0; // This variable is used to count the total seconds that is compared to the three intervals above.
- int built_in_led = 13; // This is to use the built-in LED to blink to show that the timer is running.
- bool blink_state = false; // This is to change the pin on and off.
- void setup() {
- Serial.begin(9600);
- pinMode(built_in_led, OUTPUT);
- 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.
- // 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.
- Serial.println("Exiting setup()");
- Serial.print("Normal temperature checks are performed every ");
- Serial.print(normal_temperature_check_interval_in_seconds);
- Serial.println(" seconds.");
- }
- void loop() {
- switch (auto_cooler_state) {
- case 0:
- // Normal checking the temperature
- check_temperature_sensor_timer();
- break;
- case 1:
- // Running the cooler
- check_cooler_timer();
- break;
- case 2:
- // Wating for the after-cooler delay
- check_to_re_enable_cooler();
- break;
- }
- }
- void check_temperature_sensor_timer() {
- check_one_second_timer();
- if (seconds_delay_counter >= normal_temperature_check_interval_in_seconds) {
- seconds_delay_counter = 0; // Reset the seconds counter
- check_temperature_sensor();
- if (measured_sensor_temperature > maximum_temperature_allowed) {
- auto_cooler_state = 1; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
- turn_the_cooler_on();
- }
- }
- }
- void check_cooler_timer() {
- check_one_second_timer();
- if (seconds_delay_counter >= cooler_running_time_in_seconds) {
- seconds_delay_counter = 0; // Reset the seconds counter
- // Switch to the next state, and turn the cooler off.
- auto_cooler_state = 2; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
- turn_the_cooler_off();
- }
- }
- void check_to_re_enable_cooler() {
- check_one_second_timer();
- if (seconds_delay_counter >= cooler_disable_time_in_seconds) {
- seconds_delay_counter = 0; // Reset the seconds counter
- // Switch to the beginning state, and show the cooler re-enable message.
- auto_cooler_state = 0; // 0 = checking the temperature, 1 = running the cooler, 2 = waiting for the cooler disable time
- show_cooler_enable_message();
- }
- }
- void turn_the_cooler_on() {
- // This is the function that would turn the cooler system on.
- // For this example there is just a serial.print to show that it is working:
- Serial.print("Cooler turning on for ");
- Serial.print(cooler_running_time_in_seconds);
- Serial.println(" seconds.");
- }
- void turn_the_cooler_off() {
- // This is the function that would turn the cooler system off.
- // For this example there is just a serial.print to show that it is working:
- Serial.println("Cooler turning off.");
- Serial.print("Cooler disabled for ");
- Serial.print(cooler_disable_time_in_seconds);
- Serial.println(" seconds.");
- }
- void show_cooler_enable_message() {
- // This function doesn't need to do anything more, it is just to show that the cooler was re-enabled.
- Serial.println("Cooler delay completed.");
- Serial.print("Normal temperature checks resuming every ");
- Serial.print(normal_temperature_check_interval_in_seconds);
- Serial.println(" seconds.");
- }
- void check_temperature_sensor() {
- // This function just generates a random number for testing purposes.
- // You would need to change this function to actually read a temperature sensor that you had connected.
- Serial.print("Target temperature = ");
- Serial.println(maximum_temperature_allowed);
- measured_sensor_temperature = random(70, 83);
- Serial.print("Temperature check = ");
- Serial.println(measured_sensor_temperature);
- }
- void check_one_second_timer() {
- // All this function does is count passing seconds and increment the seconds_delay_counter value.
- one_second_current_time = millis();
- if (one_second_current_time >= one_second_begin_time) {
- if (one_second_current_time >= (one_second_begin_time + 1000)) {
- seconds_delay_counter++;
- blink_the_timer_led();
- one_second_begin_time = millis();
- }
- }
- else {
- one_second_begin_time = millis();
- }
- }
- void blink_the_timer_led() {
- if (blink_state == true) {
- blink_state = false;
- }
- else {
- blink_state = true;
- }
- digitalWrite(built_in_led, blink_state);
- }
- // ~~~~~~~~ the end ~~~~~~~~~~~
Add Comment
Please, Sign In to add comment