SimpleTimer Implementation

Yes I see that, just preparing a simulation sketch for you.

Ah okay. Thanks!

Serial Monitor is:

Started
Timer activated in MS_1
MT_1 will start in 10s
Reached MT_1
Reached MT_1
timer has now been disabled

For this:

// SimpleTimerExampleV2.ino by Costas 3/10/2016

#include <SimpleTimer.h>

SimpleTimer timer;
long DurationRequired = 10000;
int functionCall = 1; // SimpleTimer used to stop and start a function // COSTAS
bool timerHasStarted = false;
int Setpoint1 = 74;
int tempF_1;
bool printed = false;

void MS_1(){
    if ((tempF_1 >= Setpoint1) && (printed == false)){
      Serial.println("Timer activated in MS_1");
      TimerStart();
      printed = true;
    }
}

void TimerStart() {
  printed = false;
  if ((timerHasStarted == false) && (printed == false)){   
    functionCall = timer.setInterval(DurationRequired, MT_1);
    Serial.println("MT_1 will start in 10s");
  }
  if (timerHasStarted == true) {
    timer.disable(functionCall);
    Serial.println("timer has now been disabled");                                     
    timerHasStarted = false;
  }
}

void MT_1(){
  Serial.println("Reached MT_1"); 
  timerHasStarted = true;
  if((millis() / 1000) > 23){  // 23 is just a simulated number
    TimerStart(); 
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("\nStarted");
}

void loop() {
  timer.run();
  if(((millis() / 1000) > 7) && ((millis() / 1000) < 12)){  // 7 & 12 are just a simulated numbers 
    tempF_1 = 75;  // above SetPoint1 temperature
    MS_1();
  }
}

Does this make it clearer how SimpleTimer works and how it can be used for your project?

Is your project another Blynk Brewery as we have code on this site for such projects?
If it is for a Brewery I believe the critical factors are time AND temperature, correct?

millis() is only used for simulation purposes in my sketch it doesn’t have anything to do with SimpleTimer.

It is for a brewery - but a more complicated system than is on the this website. You’re correct in saying that both time AND temp are critical factors. However, I’ve already got my temperature control coded and I’m good on that front. It’s just the timing that I can’t figure out how to code.

I’ll take a look at your simulation and see if I can figure it out. Thanks again, I’ll keep you updated.

1 Like

More complicated because of PID?

More complicated because of PID as well as pumps, heating elements, valves, LCD, physical btns, inputs (temp, time).

For the durationRequired variable, how do I get that to only decide how long MT_1() runs for? It seems in your code that the variable also dictates how long until MT_1() starts.

You can have another SimpleTimer for that. Maximum of 10 timers for each instance of SimpleTimer.

So how do I set that? I made another variable (long DurationRequired1) but I don’t know where I’d put it. In your code, millis() seems to dictate how long MT_1() runs for.

In reality millis() would be another factor such as temperature i.e. if temperature is above or below X then stop function y but for simulation I am using millis(). Does that make sense?

I will try to add durationRequired1 to the simulation now and send it to you.

Yes, that does make sense. Still confused on adding a second duration though. Hopefully the example will be helpful. Thanks.

Removed all the confusing millis() and this should cover what you need but there are lots more functions available in SimpleTimer.

    Started
    Timer activated in MS_1
    MT_1 will repeat every 3s until StopFunction timer kicks in
    MT_1 function running
    MT_1 function running
    MT_1 function running
    MT_1 function running
    MT_1 function has now been stopped by SimpleTimer
    StopFunction timer has also been disabled
// SimpleTimerExampleV3.ino by Costas 3/10/2016
#include <SimpleTimer.h>

SimpleTimer timer;
long StartupRequired = 3000;
long DurationRequired = 13000;
int StartFunction = 1; // SimpleTimer used to start a function
int StopFunction = 2; // SimpleTimer used to stop a function
int Setpoint1 = 74;
int tempF_1  = 76;  // simulation sets temperature over Setpoint1
bool buttonwaspressed = true;

void MS_1(){
    if (tempF_1 >= Setpoint1){
      Serial.println("Timer activated in MS_1");
      TimerStart();
      buttonwaspressed = false;  // simulation
    }
}

void TimerStart() {   
    StartFunction = timer.setInterval(StartupRequired, MT_1);
    Serial.println("MT_1 will repeat every 3s until StopFunction timer kicks in");
    StopFunction = timer.setInterval(DurationRequired, stopMT_1);
}

void MT_1(){
  Serial.println("MT_1 function running");
}

void stopMT_1(){
  timer.disable(StartFunction);
  Serial.println("MT_1 function has now been stopped by SimpleTimer");
  timer.disable(StopFunction);
  Serial.println("StopFunction timer has also been disabled");  
}

void setup() {
  Serial.begin(115200);
  Serial.println("\nStarted");
}

void loop() {
  timer.run();
  if(buttonwaspressed == true){ // simulation
    MS_1();
  }
}

Does that look better?

Well it’s after 4.30 am here so I’m off to bed for a few hours before hitting the beach.

Okay. Thanks so much for the help @Costas. Sorry if I kept you up! Was not my intention.