Avoid Delay

void bomba()
{ sensors.requestTemperatures();
float TempE = sensors.getTempCByIndex(1);
float TempS = sensors.getTempCByIndex(0);

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

if (TempS > (4 + TempE))
{ int C = TempS;
int D = TempS;
if (D <= C)
{
void setup()
{
pinMode(12, OUTPUT);

Serial.begin(9600);

sensors.begin();

Blynk.begin(auth, ssid, pass);

timer.setInterval(1000L, myTimerEvent);
timer.setInterval(2500L, bomba);
}

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

Thank you very much for your information Alexis, but I need there to be 30 seconds between the two temperature readings. This is so that the ralay is executed when the temperature stops increasing.

1 Like

make 2 functions
Bomba1 and bomba2
and two timers
timer.setInterval(2500L, bomba1);
timer.setInterval(32500L, bomba2);

It does not work, int A = TempS is repeated every 2500ms and int B = TempS every 32500ms, therefore it compares the values ​​every 2500ms. I need you to compare the current temperature with that of 30sec behind.

Well, I fixed your post with proper formatting as required:

But not too many will take lots of time to diagnose and rewrite your code for you… I suggest you work WITH @Blynk_Coeur and try to learn from what he has provided, not just say "It does not work… I need you to…"

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