Control a relay's on off time using two sliders

I need help with control over a relay’s on off time basically i want to control how long it will stay on for with a slider and then with another slider control how long it will be off for if you could create the code for me and explain it would be much appreciated

I’m sorry Ace, but (for multiple reasons) no one here is gonna write the code for you!

Read the FAQ, Docs and the recommendations for when you create a new topic. Search the forum for similar projects etc…

Have you looked at Automations?

Pete.

yes but they only allow repeat for days and not hours or minutes i would want to control it for 5 min off then 1 min but then change the values using a slider or any other widget

i have some code but it does not work
/*************************************************************

This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/

/* Fill-in information from Blynk Device Info here */

#define BLYNK_TEMPLATE_ID           "TMPL2vfo7E5Qn"
#define BLYNK_TEMPLATE_NAME         "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "DihecUG1LKfshSxjxUkkG0Ad82YGo6-4"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[ ] = "";
char pass[] = "";
// Declaring a global variabl for sensor data
int sensorVal; 
int relayPin = 5;  // Replace with the actual pin connected to the relay
int onTime = 0;
int offTime = 0;
bool relayState = false;
unsigned long previousMillis = 0;
BlynkTimer timer;

void myTimer() 
{
  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V5
  Blynk.virtualWrite(V1, sensorVal);  
}

BLYNK_WRITE(V2)
{
  // Set incoming value from pin V0 to a variable
  int value=param.asInt();

 analogWrite(2,value);
}
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V3) {
  onTime = param.asInt() * 1000;  // Convert seconds to milliseconds
}

BLYNK_WRITE(V4) {
  offTime = param.asInt() * 1000;  // Convert seconds to milliseconds
}

BLYNK_WRITE(V18)
{
  // Set incoming value from pin V0 to a variable
  int pinvalue=param.asInt();

 digitalWrite(18,pinvalue);
}

BLYNK_WRITE(V19)
{
  // Set incoming value from pin V0 to a variable
  int pinvalue=param.asInt();

 digitalWrite(19,pinvalue);
}

BLYNK_WRITE(V21)
{
  // Set incoming value from pin V0 to a variable
  int pinvalue=param.asInt();

 digitalWrite(21,pinvalue);
}

void manageRelay() {
  unsigned long currentMillis = millis();

  if (relayState) {
    // Relay is currently ON, check if it's time to turn it OFF
    if (currentMillis - previousMillis >= onTime) {
      digitalWrite(relayPin, LOW);  // Turn the relay OFF
      relayState = false;
      previousMillis = currentMillis;
    }
  } else {
    // Relay is currently OFF, check if it's time to turn it ON
    if (currentMillis - previousMillis >= offTime) {
      digitalWrite(relayPin, HIGH);  // Turn the relay ON
      relayState = true;
      previousMillis = currentMillis;
    }
  }
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "");
  Blynk.setProperty(V3, "onImageUrl",  "");
  Blynk.setProperty(V3, "url", "");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V100, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(5,OUTPUT);
  pinMode(18,OUTPUT);
  pinMode(19,OUTPUT);
   pinMode(21,OUTPUT);
  pinMode(2,OUTPUT);

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

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

void loop()
{
   // Reading sensor from hardware analog pin A0
  sensorVal = analogRead(A0); 
  
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}```

@Ace777 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

It would help if you elaborated on what you mean by this.

“does not work” could mean anything from it won’t compile in your IDE to occasionally producing unexpected results.

Also, if you want people to look at your code and provide some feedback then it would help if your in-code comments made sense, and you explained what widgets you had set-up in your dashboard, and how the datastreams attached to those widgets are configured (especially the data type and min/max values).

Your in-code comments say thinks like this…

all of which refer to virtual pin 0 whan clearly they actually relate to other virtual pins, but we have bo idea what type of widgets they are connected to, and what their function within your users interface is.

You’re dong things like this…

with no explanation of what is connected to pin GPIO2.
You’re doing similar things for GPIO18, 18 and 21.

You also need to stop doing this in your void loop…

and instead use a BlynkTimer to read your A0 pin at a much more sensible frequency (once every second or once every 10 seconds maybe?).

Pete.

1 Like

So sorry for how noob i sound but i just watched a mark rober video and am now motivated again. I tried explaining as much as i could in the code as i can but what i am having an issue with is the the slider on the web dashboard do not do anything where as they should change the time for the on off relay i dont think its a problem with the datastream or anything of that matter but i will take a look at it

#define BLYNK_TEMPLATE_ID           "TMPL2vfo7E5Qn"
#define BLYNK_TEMPLATE_NAME         "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "DihecUG1LKfshSxjxUkkG0Ad82YGo6-4"


#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


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

int sensorVal; 
//the code below me is kinda the defualt on off time and also to define the relay pin
int relayPin = 5;  
int onTime = 0;
int offTime = 0;
bool relayState = false;
unsigned long previousMillis = 0;
BlynkTimer timer;

void myTimer() 
{
 
  Blynk.virtualWrite(V1, sensorVal);  
}

BLYNK_WRITE(V2)
{
  // this code is for a pwm led it currently works
  int value=param.asInt();

 analogWrite(2,value);
}

BLYNK_WRITE(V3) {
  onTime = param.asInt() * 1000;  // Convert seconds to milliseconds
}

BLYNK_WRITE(V4) {
  offTime = param.asInt() * 1000;  // Convert seconds to milliseconds
}

BLYNK_WRITE(V18)
{
 // this code is for a relay
  int pinvalue=param.asInt();

 digitalWrite(18,pinvalue);
}

BLYNK_WRITE(V19)
{
  // this code is for a relay
  int pinvalue=param.asInt();

 digitalWrite(19,pinvalue);
}

BLYNK_WRITE(V21)
{
   // this code is for a relay
  int pinvalue=param.asInt();

 digitalWrite(21,pinvalue);
}
//the code below this message is for managing the relays on off time i got it from a youtube video but it currently does not work becuase when i move the two sliders nothing happens
void manageRelay() {
  unsigned long currentMillis = millis();

  if (relayState) {
    // Relay is currently ON, check if it's time to turn it OFF
    if (currentMillis - previousMillis >= onTime) {
      digitalWrite(relayPin, LOW);  // Turn the relay OFF
      relayState = false;
      previousMillis = currentMillis;
    }
  } else {
    // Relay is currently OFF, check if it's time to turn it ON
    if (currentMillis - previousMillis >= offTime) {
      digitalWrite(relayPin, HIGH);  // Turn the relay ON
      relayState = true;
      previousMillis = currentMillis;
    }
  }
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{

}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V100, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(5,OUTPUT);
  pinMode(18,OUTPUT);
  pinMode(19,OUTPUT);
   pinMode(21,OUTPUT);
  pinMode(2,OUTPUT);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);


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

void loop()
{
   // Reading sensor from hardware analog pin A0
  sensorVal = analogRead(A0); 
  
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}```

You seem to have ignored most of what I said in my previous post, so I’ll take a back seat for a while to see if you feel like providing more info or asking/answering questions about specific comments I made.

Pete.