Timing a Functions activity

Hello all,

I am looking to add a Time readout that counts up for the duration the output is active, when the output the timer would drop to nil.

My problem is most likely not knowing the right search terms.

Project is fairly simple. Hardware is:
Arduino Uno
BME280
20x4 LCD
LED's
buttons
Output to a solid state relay. This is what i want to know how long its been on.

I read the temp and report it to the LCD
I read the millis and report it to the LCD
I switch the relay output between the 2 temps i have set
2 temps are variable with the buttons.

My code:

#include <LiquidCrystal_I2C.h>
#include "cactus_io_BME280_I2C.h"

BME280_I2C bme(0x76); //BM280 address

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars 4 line display

const int r1_pin = 10; //Solid State Relay 1
const int led1 = 13; //Red Led used to indicate relay on (Heat on)
const int led2 = 12; //Yellow Led used to indicate standby, relay off (Heat Off)
const int led3 = 11; //Green Led used to indicate program is running through its loop

int r1state = LOW; // SSR state setting

const int B1pin = 2; //Button for templow incriment + 
const int B2pin = 3; //Button for templow incriment -
const int B3pin = 4; //Button for temphigh incriment +
const int B4pin = 5; //Button for temphigh incriment -

int B1State; // the current reading from the input pin
int B2State; // the current reading from the input pin
int B3State; // the current reading from the input pin
int B4State; // the current reading from the input pin

int lastB1State = LOW; // the previous reading from the input pin
int lastB2State = LOW; // the previous reading from the input pin
int lastB3State = LOW; // the previous reading from the input pin
int lastB4State = LOW; // the previous reading from the input pin

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers


float templow = 19; // Temperature realy will switch on
float temphigh = 20;// Temperature relay will switch off

unsigned long previousMillis = 0UL; //timing setup
unsigned long interval = 100UL; //timing delay setting



void setup(){

  pinMode(r1_pin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  pinMode(B1pin, INPUT_PULLUP);
  pinMode(B2pin, INPUT_PULLUP);
  pinMode(B3pin, INPUT_PULLUP);
  pinMode(B4pin, INPUT_PULLUP);

  lcd.init();    // initialize the lcd
  lcd.backlight(); // initialize the backlight for lcd
  Serial.begin(115200) ;

  if (!bme.begin()) {
  Serial.println("Could not find a valid BME280 sensor, check wiring!");
  while (1);}

  bme.setTempCal(-1); // Temp calibration setting in Cecius

  lcd.setCursor(0,0); lcd.print("Temp ");
  lcd.setCursor(11,0); lcd.print("Heat");
  lcd.setCursor(0,1); lcd.print("Low ");
  lcd.setCursor(0,2); lcd.print("High");
}

void loop()
{
  unsigned long currentMillis = millis();    
  bme.readSensor();

//======================== BUTTON INPUT SECTION ==================
//================================================================
{
  int reading = digitalRead(B1pin);// read the state of the switch into a local variable: 
  //check to see if you just pressed the button (i.e. the input went from LOW to HIGH),  and you've waited long enough since the last press to ignore any noise:
  // If the switch changed, due to noise or pressing:
  if (reading != lastB1State) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != B1State) {
      B1State = reading;
     
      // only toggle the LED if the new button state is HIGH
      if (B1State == HIGH) {
        templow = templow+0.1; 
      }
    }
    }
  lastB1State = reading;
}
{
  int reading = digitalRead(B2pin);// read the state of the switch into a local variable: 
  //check to see if you just pressed the button (i.e. the input went from LOW to HIGH),  and you've waited long enough since the last press to ignore any noise:
  // If the switch changed, due to noise or pressing:
  if (reading != lastB2State) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != B2State) {
      B2State = reading;
     
      // only toggle the LED if the new button state is HIGH
      if (B2State == HIGH) {
        templow = templow-0.1; 
      }
    }
    }
  lastB2State = reading;
}
{
  int reading = digitalRead(B3pin);// read the state of the switch into a local variable: 
  //check to see if you just pressed the button (i.e. the input went from LOW to HIGH),  and you've waited long enough since the last press to ignore any noise:
  // If the switch changed, due to noise or pressing:
  if (reading != lastB3State) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != B3State) {
      B3State = reading;
     
      // only toggle the LED if the new button state is HIGH
      if (B3State == HIGH) {
        temphigh = temphigh+0.1; 
      }
    }
    }
  lastB3State = reading;

}
{
  int reading = digitalRead(B4pin);// read the state of the switch into a local variable: 
  //check to see if you just pressed the button (i.e. the input went from LOW to HIGH),  and you've waited long enough since the last press to ignore any noise:
  // If the switch changed, due to noise or pressing:
  if (reading != lastB4State) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != B4State) {
      B4State = reading;
     
      // only toggle the LED if the new button state is HIGH
      if (B4State == HIGH) {
        temphigh = temphigh-0.1; 
      }
    }
    }
  lastB4State = reading;

}
//======================== HEAT CONTROL SECTION ==================
//================================================================

 if(currentMillis - previousMillis > interval)
 {  
  if (bme.getTemperature_C() < (templow)) {digitalWrite(r1_pin, LOW); digitalWrite(led1, HIGH); digitalWrite(led2, LOW); lcd.setCursor(16,0); lcd.print("ON.");}  
  else if (bme.getTemperature_C() >= (temphigh)){digitalWrite(r1_pin, HIGH); digitalWrite(led1, LOW); digitalWrite(led2, HIGH); lcd.setCursor(16,0); lcd.print("OFF");
  }
//======================== LCD OUTPUT SECTION ====================
//================================================================

{
  lcd.setCursor(5,0); lcd.print(bme.getTemperature_C());
  lcd.setCursor(0,3); lcd.print(millis() / 1000);
  lcd.setCursor(5,1); lcd.print(templow);
  lcd.setCursor(5,2); lcd.print(temphigh); }

  previousMillis = currentMillis;
 }

}

My code is not the best i imagine, its not my strong suit. I did notate most of it when i was doing debugging due to a LCD freezing issue, i changed all the hardware and the issue persisted. tried different library's and it persisted. Then i seen mention of EMF causing issues and i started looking at me relay setup. i was running a relay board to switch a 24VAC contactor, as soon as i swapped the contactor to a SSR it solved the issue.

Anyways hoping someone has a better search term i can use that will find something beyond "Blink Without Delay"

Cheers

A timer is just the current millis minus the millis when the timer started.

To start a timer when you do a thing (such as activate a relay) you simply set a variable eg startTimer to equal millis.

To find out how much time has elapsed you simply do the formula elapsedTime equals current millis minus startTimer.

Divide by 1000 if you want seconds and do with the variable whatever you like

If you are coding for lots of different things it is often best to do each thing separately and then make it into a function to give it some encapsulation. That way it doesn’t all jumble together.

Rewriting code from scratch over and over as you learn is both cathartic and often quicker than continued tweaking

This guy does a great job at showing how to create a buslist (I think he calls it multi-tasking) with millis() timing. A buslist is where you want one event to occur every 500ms, and another event to occur every 100 ms, and more events at 25ms... et c.

Thanks to Both of you it pointed me where i think i need to go to get this sorted, i will drop back with results once i sort it this weekend for others to view.

Much Appreciated, Cheers

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.