Spa pool controller - How can I add a timer to a buttons function?

Hi everyone, last time I tried using this forum I received quite a few rude replies, but I would appreciate some guidance for a program I am writing. To begin with - I’m a registered elect-chicken and 250VAC is a part of life, so as far as the mains side of things, all is safe and well. If you aren’t aware of the code of conduct for dealing with mains power, please don’t use this to do it yourself and jerry rig a dodgy home made spa pool.

Anyway, here is the code I have sorted out so far:

#include <LiquidCrystal.h>   //Libraries
#include "RTClib.h"
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd
//LCD 1602
//RS to 2pin, EN to 3pin, D4 to 4pin, D5 to 5pin, D6 to 6pin, D7 to 7pin
//Contrast=  10K potentiometer: 1 to Vss, 2(center)to VO, 3 to GND
//Backlight= K to GND(with 1K resistor), A to Vcc
//Vss to GND , Vdd to +5v

RTC_DS1307 RTC;   // define the Real Time Clock object
                  //SDA to analog 4,  SCL to analog 5

const int TEMP_up = 8;        //Temperature up button
const int TEMP_down = 9;      //Temperature down button
const int FAST_pump_button = 10;
const int BLOWER_pump_button = 11;
#define TEMP_sensor 12 //DS18B20
//const int LIGHTS = 0;      //NOT IN USE YET
//const int LIGHTS_button = 1;      //NOT IN USE YET
const int HEAT = 14;        //Heater relay
//const int PUMP = 13;        //Pump relay
const int FAST_pump = 15;     //Fast pump relay
const int BLOWER_pump = 16;     //Blower relay
int TARGET_temp = 25;

int JETS_button_val = 0;        // variable integers for debouncing buttons
int JETS_button_old_val = 0;  
int JETS_button_state = 0;
int BLOWER_button_state = 0;
int BLOWER_button_old_val = 0;
int BLOWER_button_val = 0;

unsigned long timerCount = 0; // variable to hold our timer info

OneWire oneWire(TEMP_sensor);
DallasTemperature sensors(&oneWire);  // Pass the oneWire reference to Dallas Temperature

void setup() {
sensors.begin(); //Starts the sensor Library
RTC.begin();     //Start Real Time Clock
     lcd.begin(16, 2);  //States LCD field
     lcd.setCursor(0,0);  //Starting point on LCD field
     lcd.print("Hello");   //Prints welcome screen
     lcd.print("Fine Spa Day");  //Second line on the lcd

pinMode(TEMP_up, INPUT);
pinMode(TEMP_down, INPUT);
pinMode(FAST_pump_button, INPUT);
pinMode(BLOWER_pump_button, INPUT);
pinMode(TEMP_sensor, INPUT);
//pinMode(LIGHTS_button, INPUT);
//pinMode(LIGHTS, OUTPUT);
pinMode(HEAT, OUTPUT);
pinMode(FAST_pump, OUTPUT);
pinMode(BLOWER_pump, OUTPUT);


void loop() {

  LCD_show_time_temp();  //Shows the time and temp as home screen
  HEAT_regulated(); //Starts the button function and temperature control
  JETS_button();    //Starts the jets function
  BLOWER_button();  //Starts the blower function  


void LCD_show_time_temp()  {

DateTime now =;        // Clock call
now =;

  if(now.hour() < 10)
  lcd.print(now.hour(), DEC); //Print hour
  if(now.minute() < 10)
  lcd.print(now.minute(), DEC); //Print min
  if(now.second() < 10)
  lcd.print(now.second(), DEC); //Print sec

Serial.print(" Requesting temperatures...");  // call sensors.requestTemperatures() to issue temperature
  sensors.requestTemperatures();              // Send the command to get temperatures
Serial.print("Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));     //You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  lcd.setCursor(0,1);                         //Set start point on the LCD
  lcd.print(sensors.getTempCByIndex(0));      //Print Temperature Value

void HEAT_regulated() {
    TARGET_temp = constrain(TARGET_temp, 0, 41);
  if (digitalRead(TEMP_up) == HIGH)  {     // If button is pushed, move Target Temp up by 1
    } else {
  if (digitalRead(TEMP_down) == HIGH)  {   // If button is pushed, move Target Temp down by 1
    } else {
   if (sensors.getTempCByIndex(0) <= TARGET_temp - 0.5) {   
    digitalWrite(HEAT, LOW);
  } else {
    if (sensors.getTempCByIndex(0) >= TARGET_temp + 0.5) {
    digitalWrite(HEAT, HIGH);


void JETS_button()  {

  JETS_button_val = digitalRead(FAST_pump_button);

    if ((JETS_button_val == HIGH) && (JETS_button_old_val == LOW))  {
      JETS_button_state = 1 - JETS_button_state;                  //Debounce avoidance for mech switch

  JETS_button_old_val = JETS_button_val;        //Stores new value

    if (JETS_button_state == 1) {
      digitalWrite(FAST_pump, LOW);
      Serial.println("JETS_pump turned ON");
      lcd.print("JETS ON");
    if (JETS_button_state == 0) {
      digitalWrite(FAST_pump, HIGH);
      Serial.println("JETS_pump turned OFF");
      lcd.print("JETS OFF");


void BLOWER_button()  {

  BLOWER_button_val = digitalRead(BLOWER_pump_button);
    if ((BLOWER_button_val == HIGH) && (BLOWER_button_old_val == LOW))  {
      BLOWER_button_state = 1 - BLOWER_button_state;                  //Debounce avoidance for mech switch
  BLOWER_button_old_val = BLOWER_button_val;
    if (BLOWER_button_state == 1) {
      digitalWrite(BLOWER_pump, LOW);
      Serial.println("BLOWER_pump turned ON");
    }  else  {
      digitalWrite(BLOWER_pump, HIGH);
      Serial.println("BLOWER_pump turned OFF");

What I am trying to figure out is a minor problem really - how would I program a timer for either of the blower or jets pump. I know it will be something along the lines of millis() since you only want the jets to run for 5-7 minutes after pressing the button, then turn off. How can I get it in there though?

Anyone else who just wants to use this as a framework for their own thermostat or such, go hard.

Add a global unsigned long variable called something like JetsStartTime. When you turn the jets on, set it to millis().

Add another function that you call from loop called something like CheckTimers. In there, check whether millis()-JetsStartTime is greater than the number of milliseconds (an unsigned long constant) you want to run the jets for. If it is and the jets are still on, turn them off and adjust the jets state variable to reflect this.

Copy & paste to do the same for the blower. Make sure it has its own start time variable though.

Thanks for the reply, I'll have a crack at it today and see if she chooches. I haven't really done much millis() or unsigned long integer programming, and I hope to expand on this relatively simple program.
Basically once it is sorted and the screen and buttons are water tight I've made a $700 part that is no longer made, and it is free to anyone who wants to mimic.

To segregate mains voltage (low voltage) from my controller voltage (super low voltage), I have 5v aliexpress relays controlled by arduino, controlling 12VDC Omron 10A relays, which will be holding the 240VAC pixies at bay.