Avoid Delay

Look at using timeout timers instead of delay()

void DoSomethingNow() {
// do stuff now
timer.setTimeout(30000, DoSomethingLater);  // will run this function in 30 seconds
}

void DoSomethingLater() {
// do the later stuff
}

https://playground.arduino.cc/Code/SimpleTimer#Functions

perfect! thank you! I’m going to try that now!

32500-2500=30000 = 30 sec :stuck_out_tongue_winking_eye:
so I don’t understand what do you want

if you need 2500 ms between A and B
just change timer, or use lambda function as @Gunner suggested you :wink:

there is an other mistake

A and B are int
TempS and TempE are float :wink:

Alexis thank you for your reply!

I need that when 30 seconds pass and the temperature has not increased the relay will turn on.

Sorry, but I’m very inexperienced in this and I can do it here and I do not know how to continue … XD

if pump1 runs every 2500ms (loop type) … when pump2 run for first time (32500ms), the comparison will not be made between pump2 and pump1 of the 30000ms?

Part pseudo code… but should help stimulate the imagination and start the learning curve.

int variable1;  // or float if required
int variable2;  // or float if required

void DoSomethingNow() {
// check temperature, store value in variable1
timer.setTimeout(30000, DoSomethingLater);  // will run this function in 30 seconds
}

void DoSomethingLater() {
// check temperature, store in variable2
// if variable 2 < variable1 then activate relay until next check
// else deactivate relay
}

here we go!!

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <OneWire.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

float TempS;
float TempE;
float A;

BlynkTimer timer;
OneWire ourWire(14);
DallasTemperature sensors(&ourWire);

int RELAY = 13;

void myTimerEvent()
{
  Blynk.virtualWrite(V5, TempS);
  Blynk.virtualWrite(V6, TempE);
}
void lectura()
{
  float A = TempS;
  timer.setTimeout(30000, encendido);
}
void encendido()
{ 
  if (TempS <= A)
{
  digitalWrite (RELAY, HIGH);
  }
}

void setup()
{  pinMode(13, OUTPUT);
  // Debug console
  Serial.begin(9600);
  
  sensors.begin();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  sensors.requestTemperatures();
  TempE = sensors.getTempCByIndex(0);
  TempS = sensors.getTempCByIndex(1);
  if (TempS > (TempE + 4))
  {
    lectura();
  }
}

Now take all this OUT of the void loop() (no need to run it thousands of times a second) and put it in a timed function

@Gunner ,

I did this, is it ok?

the problem is that “A” changes value every time f “CONTROL” runs (1000ms)

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxxx";

BlynkTimer timer;
OneWire ourWire(14);
DallasTemperature sensors(&ourWire);

float A;
float B;

int RELAY = 13;

void myTimerEvent()
{
  sensors.requestTemperatures();
  float TempE = sensors.getTempCByIndex(0);
  float TempS = sensors.getTempCByIndex(1);
  Blynk.virtualWrite(V5, TempS);
  Blynk.virtualWrite(V6, TempE);
}

void control()
{
  sensors.requestTemperatures();
  float TempP = sensors.getTempCByIndex(0);
  float TempC = sensors.getTempCByIndex(1);
  if (TempC > (TempP + 4))
  {
    A = TempC;
    timer.setTimeout(30000, encendido);
  }
  if (TempC < (TempP + 3))
  {
    B = TempC;
    timer.setTimeout(20000, apagado);
  }
}

void encendido()
{
  sensors.requestTemperatures();
  float Temp2 = sensors.getTempCByIndex(1);
  if (Temp2 <= A)
  {
    digitalWrite (RELAY, HIGH);
  }
}

void apagado()
{
  sensors.requestTemperatures();
  float Temp4 = sensors.getTempCByIndex(1);
  if (Temp4 <= B)
  {
    digitalWrite (RELAY, LOW);
  }
}

void setup()
{
  pinMode(13, OUTPUT);

  Serial.begin(9600);

  sensors.begin();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, control);
}

void loop()
{
  Blynk.run();
  timer.run();
}

I am not actually following your code in any detail, I have my own projects I am working on :wink: I am just pointing out various notable things that I can see at a glance

Do you need these functions to run together? if so, merge them into ONE timed function… otherwise stagger them so they DON’T try to run at the exact same time (see the link to my timers post, above).

perfect, thank you very much!

The problem is I still can not compare temperatures with 30 seconds of difference

can you better explain what you are trying to accomplish? the logic in your latest code is hard to follow.

Also by looking at the drawing you provided it shows the comparison of two temperatures only 2500 ms apart

I did what @Gunner told me.

My oroginal code was this:

  if (TempS < (3 + TempE))
  { int A = TempS;
    delay (30000);
    if (TempS <= A)
    {
      digitalWrite (RELAY, LOW);
    }
  }

  if (TempS > (4 + TempE))
  { int C = TempS;
    delay (30000);
    if (TempS <= C)
    {
      digitalWrite (RELAY, HIGH);
    }

But I need to avoid DELAY function because it’s blocking the sending of data to Blynk app.
I need to compare the current temperature with the temperature of 30 seconds ago.

Thanks!!

@Gunner told you how to do

Sorry … But I do not understand that, where is the 30-second pause?

timer.setTimeout(30000, DoSomethingLater); // will run this function in 30 seconds
}

that’s what I did … but how do I make that loop repeat?

 if (TempC > (TempP + 4))
  {
    A = TempC;
    timer.setTimeout(30000, encendido);
  }
  if (TempC < (TempP + 3))
  {
    B = TempC;
    timer.setTimeout(20000, apagado);
  }
}

void encendido()
{
  sensors.requestTemperatures();
  float Temp2 = sensors.getTempCByIndex(1);
  if (Temp2 <= A)
  {
    digitalWrite (RELAY, HIGH);
  }
}

Your code snippets are still hard to decipher.

The concept of timers depends on the timer you use… you really need to sit down and read all about their different options, then experiment with simple concepts before plunging into something that affects real-world controls… checkout that link above… to my example sketches… it also has links to the SimpleTimer library that Blynk Timer is based on.

Then you can do lots of different “timing” things without the delay()

Another pseudo code example… you want to have an action, every 5 minutes to take a reading, then compare it to itself 60 seconds later (for whatever reason… just spitballin…)

Ideally you would have an interval timer in setup() repeatedly calling a function, say… every 5 minutes. Then that function calls another for the reading, then runs a different type of timer… the timeout timer, which only runs the one time, x seconds later, to call another function… that then calls your sensors again, compares them, and does whatever logic you want based on the results.

void setup() {
timer.setInterval(3000000, MyFirstreading);  // Interval timer will run this function every 5 minutes
}

void MyFirstreading() {
MySensors();    // run the sensor function
firstTemp = temp;  // transfer the value
timer.setTimeout(60000, MySecondReading);  // then run a timeout timer that runs once, 60 seconds later, each each time this containing function is called
}


void MySecondReading() { 
MySensors();  // run the sensor function
secondTemp = temp;
// compare firstTemp with secondTemp and do something based on result
}


void MySensors() {
temp = sensor_read_command;  // read a sensor, store it in a variable called temp
}

This example could be further refined with less functions… but I am trying to spread them out (the differing timers) for better viewing :wink:

1 Like

Opps :blush: I had the wrong type of timer showing in my above post, the one in setup() is supposed to be an interval, not a timeout. It is fixed now… and a few other syntax issues… happens when I make non-running pseudo code that is not verified in a compiler.

2 Likes