I came upon a problem with timers.
If the pump is off and the temperature difference is <2 degrees, it needs to be active for 20 seconds (depends on PumpOnTime),.
After that, if the pump is on and the temperature difference is <2 degrees, it needs to be shut off for 10 seconds (depends on PumpOffTime),
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
// Addresses of 3 DS18B20s
uint8_t hot[8] = { 0x28, 0x69, 0x7A, 0x25, 0x51, 0x20, 0x01, 0x25 }; //Hot water sensor
uint8_t cold[8] = { 0x28, 0x84, 0x5D, 0x21, 0x51, 0x20, 0x01, 0xE1 }; //Returning cold water
uint8_t boiler[8] = { 0x28, 0xDD, 0xC4, 0x0C, 0x51, 0x20, 0x01, 0xF0 }; //Boiler
// You should get Auth Token in the Blynk App.
char auth[] = "zdn5X3XAlwFU69sOppwOezhjgz6gX0sc";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Teo-4CA03F-Greitasis";
char pass[] = "7035EB8038";
BlynkTimer timer;
WidgetLED Relay_LED(V9);
int RelayPin = 5;
int RelayState = LOW;
long Interval = 1000;
float Hot_s;
float Cold_s;
float Boiler_s;
float Difference;
long previousMillis = 0;
long PumpOnTime = 20000;
long PumpOffTime = 10000;
long OffTime = 0;
long OnTime = 0;
void TemperatureMeasurement()
{
sensors.requestTemperatures();
Hot_s = sensors.getTempC(hot);
Cold_s = sensors.getTempC(cold);
Boiler_s = sensors.getTempC(boiler);
Difference = Hot_s-Cold_s;
}
void ResultsToSerial()
{
Serial.print("Hot water: ");
Serial.print(Hot_s);
Serial.print(" C ");
Serial.print("Cold water: ");
Serial.print(Cold_s);
Serial.print(" C ");
Serial.print("Boiler: ");
Serial.print(Boiler_s);
Serial.print(" C ");
Serial.print ("Difference:");
Serial.print(Difference);
Serial.println(" C ");
unsigned long OffStart = millis();
unsigned long OnStart = millis();
if ((RelayState == LOW) &&(Difference >= 2)) {
Relay_LED.on();
RelayState = HIGH;
digitalWrite (RelayPin, RelayState);
Serial.println("Pump ON");
} else if ((RelayState == LOW) && (Difference < 2) && (OffStart - OffTime >= PumpOffTime)) {
Relay_LED.on();
RelayState = HIGH;
digitalWrite (RelayPin, RelayState);
Serial.println("Pump On");
OffTime = OffStart;
} else if ((RelayState == HIGH) && (Difference < 2) && (OnStart - OnTime >= PumpOnTime)){
Relay_LED.off();
RelayState = LOW;
digitalWrite (RelayPin, RelayState);
Serial.println("Pump Off");
OnTime = OnStart;
}
}
void ResultsToBlynk()
{
Blynk.virtualWrite(V5, Hot_s);
Blynk.virtualWrite(V6, Cold_s);
Blynk.virtualWrite(V8, Boiler_s);
Blynk.virtualWrite(V7, Difference);
}
void setup()
{
Serial.begin(9600);
sensors.begin();
Blynk.begin(auth, ssid, pass);
}
void loop()
{
unsigned long currentMillis = millis();
unsigned long EsamosMillis = millis();
Blynk.run();
if (currentMillis - previousMillis > Interval) {
previousMillis = currentMillis;
TemperatureMeasurement();
ResultsToSerial();
ResultsToBlynk();
}
}
However, there is a problem somewhere in the code. Please take a look at the provided picture for code. The pump turns on for 30 seconds, but after that time period it shuts off for only 1 second. Can somebody help me to correct the code. Thank you.
