BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How often and for how long turn the relay on

Basicly ı want to control my motor via a relay I want to be able to control it wiith a StepH widget the relay how often it triggers on for example every 1 hour or every 2 hours and see it on the Labele Value widget at he same time with another slider for how long it stays on for examle 10 seconds or 20 seconds. I dont want to use timer widget or eventor widget

adjust how often the relay is on slider V9 labeled value widget V22 led widget V27

adjust for how long it stays on slider V10 labeled value widget V24 led widgetV27

manually turn on the relay push button widget V4 led widget V27

Sorry I got lost in my code

this is my code

#define BLYNK_PRINT Serial

#include <DHT.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SPI.h>

#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetLED motLed(V27);
BlynkTimer timer;

#include <Wire.h>
#define DHTPIN 2
#define DHTTYPE DHT22

 char auth[] = "4a408ebae6c4404abc1682cbef7ad2fe";
 char ssid[] = "moonlight";
char pass[] = "baho1985";
#define EspSerial Serial1
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
const int relayHum =  5;
const int relayTem = 6;
const int relayMot = 7;

float temLowTrigger;
int humLowTrigger;
int motSecTrigger = 0;              // Viyollerin Kac Saniye Dondurulecegini tutan degisken                         (EEPROM)       (1 - 15 Sn)
int motHourTrigger = 0;
int nextmotHourTrigger = -1;    // Bir sonraki dondurme zamanını hesaplayıp tutar. Saat olarak (14:45:55 ise 14 tutar )
int lastmotHourTrigger = -1;         // Aynı saat icinde iki kere dondurmesin diye son Dondurme Zamanını tutar Saat olarak (14:45:55 ise 14 tutar )
long turnMillis = 0;
int currentTime;
int timerID =1;
int notifyUptime;

DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTem(int param);
void updateMotHour(int param);
void updateMotSec(int param);

int btnPin  = 8;
int motState = HIGH;
int btnState = HIGH;
BLYNK_WRITE(V9){   // add a slider on V0 range 0 to 30 (minutes)
  int seconds = param.asInt();
  
  

}  //<--- MISSING EARLIER

BLYNK_WRITE(btnPin) {     // Blynk app WRITES button status to server
   motState = param.asInt();
    if(motState == 1){
    digitalWrite(relayMot, HIGH);
     motLed.on();
   
    //Serial.println("Relay is now ON");    
    }
    else {
    digitalWrite(relayMot, LOW); 
     motLed.off();
    //Serial.println("Relay is now OFF");   
    }
    }




BLYNK_WRITE(V7) {
  updateHum(param.asInt());
  
}

 BLYNK_WRITE(V8) {   
updateTem(param.asFloat());; // Get value as float
 } 
BLYNK_WRITE(V9) {
  updateMotHour(param.asInt());
 } 

BLYNK_WRITE(V10) {
  updateMotSec(param.asInt());
} 




// When App button is pushed - switch the state
BLYNK_WRITE(V4) {
  motState = param.asInt();
  digitalWrite(relayMot, motState);
}



 

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  Blynk.virtualWrite(V25, humLowTrigger);
  Blynk.virtualWrite(V23, temLowTrigger);
 
  Blynk.virtualWrite(V22, motHourTrigger);
  Blynk.virtualWrite(V24, motSecTrigger);

 
  if(h <= humLowTrigger) {
      digitalWrite(relayHum, LOW); 
      Blynk.virtualWrite(V26, 255);
  } else {
      digitalWrite(relayHum, HIGH);
      Blynk.virtualWrite(V26, 0);
  }
  if(t <= temLowTrigger) {
      digitalWrite(relayTem, LOW); 
      Blynk.virtualWrite(V28, 255);
    
  } else {
      digitalWrite(relayTem, HIGH);
      Blynk.virtualWrite(V28, 0);
  }
  if(t <= motHourTrigger) {
      digitalWrite(relayMot, LOW); 
      Blynk.virtualWrite(V28, 255);
    
  } else {
      digitalWrite(relayMot, HIGH);
      Blynk.virtualWrite(V28, 0);
  }
  /////////////////
 
   

void updateHum(int param) {
  humLowTrigger = param;
}


void updateTem(float param) {
  temLowTrigger = param;
}

void updateMotsat(int param) {
  motHourTrigger = param;
}

void updateMotsan(int param) {
  motSecTrigger = param;

}

void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
 

  Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();
  pinMode(relayHum, OUTPUT);
  digitalWrite(relayHum, LOW);
  pinMode(relayTem, OUTPUT);
  digitalWrite(relayTem, LOW);
  pinMode(relayMot, OUTPUT);
  digitalWrite(relayMot, LOW);
  setSyncInterval(10*60);
  pinMode(btnPin, INPUT_PULLUP);
  
  
  pinMode(relayTem, OUTPUT);
pinMode(relayHum, OUTPUT);
pinMode(relayMot, OUTPUT);
 timer.setInterval(5000L, sendSensor);
}
}
BLYNK_CONNECTED() {
  Blynk.syncAll();
} 
void loop()
{   
  Blynk.run();
  timer.run();
}

First, please remebler to format your pasted code for proper forum viewing - I have already edited you post above, look back at it to see how it is done.

Blynk - FTFC

Secondly, for your issue… This is more a “how to” programming issue then Blynk specific.

You will need to study up on how to use BlynkTimer even more then you do now, as that can do everything you are asking for.

Search this forum for other BlynkTimer delay and Relay based timing solutions. There are there are some good examples already provided.

Here is one example

And another

Gunner Iam sorry Iam pain in in the neck but the pages you are pointing have got nothing to do with my case. I’ve already seen those before I am not worrying about button Iam trying to solve the slider problems there are 2 sliders controlling one relay one of the sliders to adjust how often the relay should be on and the other slider is to adjust for how long the relay should be on and I have to see the numbers on the labeled value widgets

You are not understanding my point… these pages DO show items of necessity for your case… TIMERS!

This is NOT a “how to use sliders” question so much as a sophisticated programming question… one of which I do not have any quick answers for.

You are going to need to take input from the sliders, stop and manipulate the settings of a timer of some sort, (millis or BlynkTimer, etc.) then start the timed process, and all while maintaining the overall flow of the sketch in the background.

Thus those posts are NOT to provide you any copy/paste code… I don’t think you will find any such code here… But there are ment to provide you with some examples of how timers can be used to in a non-blocking way. But HOW to use them is up to you to learn.

Whether using buttons, hardcoded numbers, sliders , terminal etc… you need to FIRST learn how to manipulate the timers, understand how they work, before you can then take any slider info, or from those aforementioned sources, and turn that info into meaningful, adjustable, timer delays.


#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SPI.h>

#include <TimeLib.h>

BlynkTimer timer;
char auth[] = "xxxxxx";
 char ssid[] = "moonlight";
char pass[] = "baho1985";
#define EspSerial Serial1
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
int relayMot = 7;
int timeLowTrigger;
int secondsLowTrigger;
        // Aynı saat icinde iki kere dondurmesin diye son Dondurme Zamanını tutar Saat olarak (14:45:55 ise 14 tutar )
long turnMillis = 0;

int turnSeconds;              // Viyollerin Kac Saniye Dondurulecegini tutan degisken                         (EEPROM)       (1 - 15 Sn)
int turnTime;  
int nextTurnTime = -1;    // Bir sonraki dondurme zamanını hesaplayıp tutar. Saat olarak (14:45:55 ise 14 tutar )
int lastTurnTime = -1;         // Aynı saat icinde iki kere dondurmesin diye son Dondurme Zamanını tutar Saat olarak (14:45:55 ise 14 tutar )
int tm_hour;  
int tm_sec; 
               // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;   


bool isFirstConnect = true;
void updateturnTime(int param);
void updateturnSeconds(int param);
BLYNK_CONNECTED() {
 
   if (isFirstConnect) 
   {
    Blynk.syncAll();
    isFirstConnect = false;
    }
  
     }

     BLYNK_WRITE(V9) { // add a slider on V9 range 0 to 240 (minutes)
  updateturnTime(param.asInt());
 } 
BLYNK_WRITE(V10){   // add a slider on V10 range 0 to 30 (seconds)
 updateturnSeconds(param.asInt());
 
 } 

 
void turnmotorTime()
{
  
  Blynk.virtualWrite(V17, nextTurnTime);



 
  Blynk.virtualWrite(V22, turnTime);
 

 
 if(turnTime <= timeLowTrigger)
    {
      
        digitalWrite(relayMot, LOW);
       
        turnTime = 0;
    } 
   

}  
 

void updateturnTime(int param) {
  turnTime = param;
}
void updateturnSeconds(int param) {
   turnSeconds = param;
}

void turnmotorSeconds()
{
  
  
  Blynk.virtualWrite(V24, turnSeconds);

 

     if(turnSeconds == 30)
  
     if(nextTurnTime == -1)
      {
          // Daha Once hiç Donmedi ise makina ilk acildiginda 1 kere dondurme islemi yapmasını sağlıyoruz
          nextTurnTime =  turnTime;
      }
      
      if(turnTime == nextTurnTime && lastTurnTime != nextTurnTime)  
      {
    digitalWrite(relayMot, LOW);
}

}  
 



 void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  ;
  pinMode(relayMot, OUTPUT);
  digitalWrite(relayMot, LOW);

timer.setInterval(100L, turnmotorTime);
  
  timer.setInterval(1000L, turnmotorSeconds);

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

I solved the problem

Here is the full code

#define BLYNK_PRINT Serial
#include <DHT.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>


BlynkTimer timer;
#define DHTPIN 2
#define DHTTYPE DHT22

char auth[] = "xxxxxxxxxxxx";
 char ssid[] = "moonlight";
char pass[] = "baho1985";
#define EspSerial Serial1
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
const int relayHum =  5;
 #define relayTemp 6
 const int relayMot =  7;
float temLowTrigger;
int humLowTrigger;
int timeLowTrigger;
int secondLowTrigger;
int turnseconds = 0;               
int turnTime = 0; 
long turnMillis = 0; 
int nextturn = -1;   
int lastturn = -1; 
DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTem(int param);
void updateTime(int param);
void updateSecond(int param);
bool isFirstConnect = true;
int turnTimer;
int timerID =1;
int timerID2 =1;

BLYNK_CONNECTED() {
 
   if (isFirstConnect) 
   {
    Blynk.syncAll();
    isFirstConnect = false;
    }
  
     }

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
  
}

 BLYNK_WRITE(V8) {   
updateTem(param.asFloat());; // Get value as float
 
  
}
BLYNK_WRITE(V9) 
{
   updateTime(param.asInt());
   timer.deleteTimer(timerID);
     timerID = timer.setInterval(timeLowTrigger * 60000L, motorTime);
}

BLYNK_WRITE(V10) {
  updateSecond(param.asInt());
  
timer.deleteTimer(timerID2);
     timerID2 = timer.setInterval(secondLowTrigger * 1000L, motorSecond);
}
void sendSensor()
{
  int h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V25, humLowTrigger);
  Blynk.virtualWrite(V23, temLowTrigger);
  Blynk.virtualWrite(V22, timeLowTrigger);
  Blynk.virtualWrite(V24, secondLowTrigger);
 
 
  
  
  if(h < humLowTrigger) {
      digitalWrite(relayHum, LOW); 
      Blynk.virtualWrite(V26, 255);
  } else {
      digitalWrite(relayHum, HIGH);
      Blynk.virtualWrite(V26, 0);
  }
  if(t < temLowTrigger) {
      digitalWrite(relayTemp, LOW); 
      Blynk.virtualWrite(V28, 255);
    
  } else {
      digitalWrite(relayTemp, HIGH);
      Blynk.virtualWrite(V28, 0);
  }
}

  void motorSecond()
{

      digitalWrite(relayMot, HIGH); 
      Blynk.virtualWrite(V27, 0);
  
}
void motorTime()
{
    
 
  
      digitalWrite(relayMot, LOW); 
      Blynk.virtualWrite(V27, 255);
  

  
}
void updateHum(int param) {
  humLowTrigger = param;
}


void updateTem(float param) {
  temLowTrigger = param;
}
void updateTime(int param) {
  timeLowTrigger = param;
}

void updateSecond(int param) {
  secondLowTrigger = param;
}


void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();
  pinMode(relayHum, OUTPUT);
  digitalWrite(relayHum, LOW);
  pinMode(relayTemp, OUTPUT);
  digitalWrite(relayTemp, LOW);
  pinMode(relayMot, OUTPUT);

 timer.setInterval(2000L, sendSensor); 

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

And I solved your improperly formatted code posting … again :stuck_out_tongue_winking_eye:

Blynk - FTFC