Time input, start/stop time to trigger a solid state relay

Hi everibody, I need some help with subjet please. I want to use a time input to trigger my solid state relay, writing a HIGH value in a port of my arduino. But when I set the start/stop time nothing happens.
This is the code that I’m using for:

BLYNK_WRITE(V10) {
  TimeInputParam t(param);

  // Process start time

  if (t.hasStartTime())
  {
    Serial.println(String("Start: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else
  {
    // Do nothing
  }

  // Process stop time

  if (t.hasStopTime())
  {
    Serial.println(String("Stop: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());

  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)

  for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
    }
  }

  Serial.println();
}

The widget it’s setting to V10 and also I using a LED widget in V9. My hardware is: An Arduino ONE and an ethernet shield. Can someone please help me!!! Thanks in advance.

The problem with posting snippets of code like this is that its impossible to see how you’ve declared your variables, and if you’ve done anything else (like forgetting to put Blynk.run in your void loop) that would make @“nothing happen”.

I assume that something is happening - such as some sort of debug messages to your serial monitor?

When you’re having problems like this, it’s a good idea to add a few more temporary Serial.print statements to help debug your program flow.

I’s start by printing the value of “t” as soon as it’s obtained from your widget. If this is never happening then I’d take a look at the widget setup and make sure it’s linked to the correct project and the correct virtual pin.

Pete.

You are right @PeteKnight. This is my entire code:

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define BLYNK_PRINT Serial
#define echoPin 3 // Echo Pin
#define trigPin 5 // Trigger Pin
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

BlynkTimer timer;            // define a timer for use by SimpleTimer library


int WM1=6;
int EncWV1=6;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=6;                // Global variable used in Slider widget and runEveryMinute()

int WM2=7;
int EncWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int x, y, z, a;

int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;

int CounterOn2 = 0;
int CounterOff2 = 0;
int ledStatus2 = 0;

int AutoRelleno=8;
int SumpLamp=9;

float Celsius = 0;
float Fahrenheit = 0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

long duration, distance; // Duration used to calculate distance

int NivelMax=0;
int NivelMin=0;

BLYNK_CONNECTED() 
  {
    Blynk.syncAll();  // Synchronize hardware with App widgets when connected
  }

BLYNK_WRITE(V0) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    x = param.asInt();  // set variable as Slider value
    EncWV1 = x;
    Serial.print("Wave Maker 1 On Timer set to: ");
    Serial.print(EncWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V1) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    y = param.asInt();  // set variable as Slider value 
    ApagWV1 = y;  
    Serial.print("Wave Maker 1 Off Timer set to: ");
    Serial.print(ApagWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V2) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    z = param.asInt();  // set variable as Slider value
    EncWV2 = z;
    Serial.print("Wave Maker 2 On Timer set to: ");
    Serial.print(EncWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V3) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    a = param.asInt();  // set variable as Slider value 
    ApagWV2 = a;  
    Serial.print("Wave Maker 2 Off Timer set to: ");
    Serial.print(ApagWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V7) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    NivelMax = param.asInt();  // set variable as Slider value  
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMax);
    Serial.println(" segundos");
  }

BLYNK_WRITE(V8) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    NivelMin = param.asInt();  // set variable as Slider value
    
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMin);
    Serial.println(" segundos");
  }
  

BLYNK_WRITE(V10) {
  TimeInputParam t(param);

  // Process start time

  if (t.hasStartTime())
  {
    Serial.println(String("Start: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else
  {
    // Do nothing
  }

  // Process stop time

  if (t.hasStopTime())
  {
    Serial.println(String("Stop: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());

  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)

  for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
    }
  }

  Serial.println();
}

void led_control()
  {
    //First Wave Maker
      
    if ((CounterOn > 0) && (ledStatus == 0))
      {
        digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 1: ON\n");
        ledStatus = 1;
        CounterOff = 0;
      }
   
  if ((CounterOn > 0) && (ledStatus == 1))
      {
      CounterOn = (CounterOn - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
    {
      CounterOff = ApagWV1;
    }

  if ((CounterOff > 0) && (ledStatus == 1))
  {
      digitalWrite(WM1,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 1: OFF\n");
      ledStatus = 0;
      CounterOn = 0;
   }
   
  if ((CounterOff > 0) && (ledStatus == 0))
      {
      CounterOff = (CounterOff - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
    {
      CounterOn = EncWV1+1;
    }

  //Second Wave Maker

  if ((CounterOn2 > 0) && (ledStatus2 == 0))
      {
        digitalWrite(WM2,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 2: ON\n");
        ledStatus2 = 1;
        CounterOff2 = 0;
      }
   
  if ((CounterOn2 > 0) && (ledStatus2 == 1))
      {
      CounterOn2 = (CounterOn2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 1))
    {
      CounterOff2 = ApagWV2;
    }

  if ((CounterOff2 > 0) && (ledStatus2 == 1))
  {
      digitalWrite(WM2,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 2: OFF\n");
      ledStatus2 = 0;
      CounterOn2 = 0;
   }
   
  if ((CounterOff2 > 0) && (ledStatus2 == 0))
      {
      CounterOff2 = (CounterOff2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 0))
    {
      CounterOn2 = EncWV2+1;
    }

}

void MeasureCm()
  {
   /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */ 
  
       
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
  
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); 
       
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
       
   //Calculate the distance (in cm) based on the speed of sound.
   distance = duration/58.2;
  
   Serial.print("Distance: ");
   Serial.println(distance); 
   Blynk.virtualWrite(4,distance);   

   if (distance>NivelMax)
     {
      digitalWrite(AutoRelleno, HIGH);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 1.
      AR.on();
      Serial.print("Bomba de Autorelleno Encendida");
     }
   if (distance<NivelMin)
     {
      digitalWrite(AutoRelleno, LOW);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 5.
      AR.off();
      Serial.print("Bomba de Autorelleno Apagada");
     }
   }

void Temperatura()
  {
    sensors.requestTemperatures();
    Celsius = sensors.getTempCByIndex(0);
    Blynk.virtualWrite(6,Celsius);
    Serial.print("Temperatura: ");
    Serial.print(Celsius);
    Serial.print(" C");
  }

void setup()
{
  if (EncWV1== 0)
  {
    EncWV1=1;
  }
  if (ApagWV1== 0)
  {
    ApagWV1=1;
  }

  sensors.begin();
  Serial.begin(9600); 
  Blynk.begin(auth);
  CounterOn = EncWV1+1;
  // run timer every 1 second to check for led On/Off action
  timer.setInterval(1000L, led_control);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
     
  timer.setInterval(1000L, MeasureCm);
  timer.setInterval(20000L, Temperatura);
}


void loop()
{
  Blynk.run();
  timer.run(); // Initiates Timer to ensure the timer keeps running  
}

Maybe “nothing happens” it’s not so accured, of course somethings happend, most of all that I’d spect to happend from my code but nothing about the time input widget to control the start/stop time for my solid state relay.

You are trying to take action in BLYNK_WRITE(V10). This routine will be invoked only when the app writes to V10. Logically, the checking for time to start or stop and taking appropriate action should be a timed, repetitive tasks and not a one time execution.

Set the time variables in BLYNK_WRITE(V10). Move the action logic to another routine and invoke it using the timer.

@mohan_sundaram The TimeInput Widget is a one-time action… you set all your tstart, stop, etc info then press OK and it sends the whole shebang to the device, once. Then all that code parses out the start stop, etc, and process each step when “it is time”.

There is lots of other topics in this forum about this somewhat misunderstood widget… but this is one of the more recent and includes a couple of more or less “ready to go” examples.

@Gunner I was saying that is what needs to be done. Did I read the code wrong? Does it already do that? I also helped write the original code, now that I see it. Will check again on my PC. Mobile screens are tricky for reading code.

Yes… somehow it will take a one shot setting from the widget and keep processing the start stop times as long as needed, or until another one-shot setting is sent from the widget, then it continues with that info, and so on. No additional “Are we there yet” loops or BlynkTimers needed.

As to whether or not this OP’s code is working properly… i don’t know :stuck_out_tongue: I am not great at troubleshooting others code… takes me too long :wink:

@Gunner I read some of the topics that you shared and I found some issues that I wasn’t considering, like the RTC, even I read that I have to include a RTC widget in my app it’s that correct?

Yes

It lets your devices know the time and the timezone based on your phones settings.

Ok so if I set the start/stop time to sunrise/sunset the digital pin will trigger right? but if I set both timers in diferent time that sunset and sunrise I must have to code the if sentences for that. It’s that correct?

Erm… I honestly have never used the sunrise/sunset… but I will hazard a guess that you will need if() logic and possibly flags for any setting you chose, to determine if it will react to any setting a;ll of the time or only some of the settings some of the time.

Sorry, probably not the clarity you need :stuck_out_tongue: but I have only worked with that widget the one time… just not something I need right now.

Thanks @Gunner do you had used some time in the past? which widget was you use?

This was my one and only “time” :stuck_out_tongue_winking_eye: working with that Time Input Widget…

I think that it’s working now, but it desconect, maybe because of server flooding error, does’t say but I think. It’s conectects to the server an then disconect whitout any error messange. This is the new code, only add the RTC in the code and in the app.

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>

#define BLYNK_PRINT Serial
#define echoPin 3 // Echo Pin
#define trigPin 5 // Trigger Pin
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

BlynkTimer timer;            // define a timer for use by SimpleTimer library
WidgetRTC rtc;

int WM1=6;
int EncWV1=6;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=6;                // Global variable used in Slider widget and runEveryMinute()

int WM2=7;
int EncWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int x, y, z, a;

int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;

int CounterOn2 = 0;
int CounterOff2 = 0;
int ledStatus2 = 0;

int AutoRelleno=8;
int SumpLamp=9;

float Celsius = 0;
float Fahrenheit = 0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

long duration, distance; // Duration used to calculate distance

int NivelMax=0;
int NivelMin=0;

BLYNK_CONNECTED() 
  {
    Blynk.syncAll();  // Synchronize hardware with App widgets when connected
  }

BLYNK_WRITE(V0) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    x = param.asInt();  // set variable as Slider value
    EncWV1 = x;
    Serial.print("Wave Maker 1 On Timer set to: ");
    Serial.print(EncWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V1) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    y = param.asInt();  // set variable as Slider value 
    ApagWV1 = y;  
    Serial.print("Wave Maker 1 Off Timer set to: ");
    Serial.print(ApagWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V2) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    z = param.asInt();  // set variable as Slider value
    EncWV2 = z;
    Serial.print("Wave Maker 2 On Timer set to: ");
    Serial.print(EncWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V3) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    a = param.asInt();  // set variable as Slider value 
    ApagWV2 = a;  
    Serial.print("Wave Maker 2 Off Timer set to: ");
    Serial.print(ApagWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V7) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    NivelMax = param.asInt();  // set variable as Slider value  
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMax);
    Serial.println(" segundos");
  }

BLYNK_WRITE(V8) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    NivelMin = param.asInt();  // set variable as Slider value
    
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMin);
    Serial.println(" segundos");
  }
  
/*
BLYNK_WRITE(V10) {
  TimeInputParam t(param);

  // Process start time

  if (t.hasStartTime())
  {
    Serial.println(String("Start: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
    digitalWrite(SumpLamp,HIGH);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.on();
  }
  else
  {
    // Do nothing
  }

  // Process stop time

  if (t.hasStopTime())
  {
    Serial.println(String("Stop: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
    digitalWrite(SumpLamp,LOW);
    WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
    SL.off();
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());

  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)

  for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
    }
  }

  Serial.println();
}*/

void led_control()
  {
    //First Wave Maker
      
    if ((CounterOn > 0) && (ledStatus == 0))
      {
        digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 1: ON\n");
        ledStatus = 1;
        CounterOff = 0;
      }
   
  if ((CounterOn > 0) && (ledStatus == 1))
      {
      CounterOn = (CounterOn - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
    {
      CounterOff = ApagWV1;
    }

  if ((CounterOff > 0) && (ledStatus == 1))
  {
      digitalWrite(WM1,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 1: OFF\n");
      ledStatus = 0;
      CounterOn = 0;
   }
   
  if ((CounterOff > 0) && (ledStatus == 0))
      {
      CounterOff = (CounterOff - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
    {
      CounterOn = EncWV1+1;
    }

  //Second Wave Maker

  if ((CounterOn2 > 0) && (ledStatus2 == 0))
      {
        digitalWrite(WM2,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 2: ON\n");
        ledStatus2 = 1;
        CounterOff2 = 0;
      }
   
  if ((CounterOn2 > 0) && (ledStatus2 == 1))
      {
      CounterOn2 = (CounterOn2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 1))
    {
      CounterOff2 = ApagWV2;
    }

  if ((CounterOff2 > 0) && (ledStatus2 == 1))
  {
      digitalWrite(WM2,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 2: OFF\n");
      ledStatus2 = 0;
      CounterOn2 = 0;
   }
   
  if ((CounterOff2 > 0) && (ledStatus2 == 0))
      {
      CounterOff2 = (CounterOff2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 0))
    {
      CounterOn2 = EncWV2+1;
    }

}

void MeasureCm()
  {
   /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */ 
  
       
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
  
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); 
       
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
       
   //Calculate the distance (in cm) based on the speed of sound.
   distance = duration/58.2;
  
   Serial.print("Distance: ");
   Serial.println(distance); 
   Blynk.virtualWrite(4,distance);   

   if (distance>NivelMax)
     {
      digitalWrite(AutoRelleno, HIGH);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 1.
      AR.on();
      Serial.print("Bomba de Autorelleno Encendida");
     }
   if (distance<NivelMin)
     {
      digitalWrite(AutoRelleno, LOW);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 5.
      AR.off();
      Serial.print("Bomba de Autorelleno Apagada");
     }
   }

void Temperatura()
  {
    sensors.requestTemperatures();
    Celsius = sensors.getTempCByIndex(0);
    Blynk.virtualWrite(6,Celsius);
    Serial.print("Temperatura: ");
    Serial.print(Celsius);
    Serial.print(" C");
  }

void setup()
{
  if (EncWV1== 0)
  {
    EncWV1=1;
  }
  if (ApagWV1== 0)
  {
    ApagWV1=1;
  }

  sensors.begin();
  Serial.begin(9600); 
  Blynk.begin(auth);
  rtc.begin();
  
  CounterOn = EncWV1+1;
  // run timer every 1 second to check for led On/Off action
  timer.setInterval(1000L, led_control);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
     
  timer.setInterval(1000L, MeasureCm);
  timer.setInterval(20000L, Temperatura);
}


void loop()
{
  Blynk.run();
  timer.run(); // Initiates Timer to ensure the timer keeps running  
} 

I have to comment the code for advanced time input because what I said before, It’s desconects from the server. I’ll use your example @Gunner, it’s pretty much what I want to do, I’ll only miss the part of sunset/sunrise working.

Hi @Gunner this is my code, but still have some issues, please help me!!!

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>

#define BLYNK_PRINT Serial
#define echoPin 3 // Echo Pin
#define trigPin 5 // Trigger Pin
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

BlynkTimer timer;            // define a timer for use by SimpleTimer library
WidgetRTC rtc;

int WM1=6;
int EncWV1=6;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=6;                // Global variable used in Slider widget and runEveryMinute()

int WM2=7;
int EncWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int x, y, z, a;

int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;

int CounterOn2 = 0;
int CounterOff2 = 0;
int ledStatus2 = 0;

int AutoRelleno=8;
int SumpLamp=9;

float Celsius = 0;
float Fahrenheit = 0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

long duration, distance; // Duration used to calculate distance

int NivelMax=0;
int NivelMin=0;

char currentTime[9];
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;


BLYNK_CONNECTED() 
  {
    Blynk.syncAll();  // Synchronize hardware with App widgets when connected
    Blynk.syncVirtual(V10); //  Synchronize Time Input Widget when connected 
  }

BLYNK_WRITE(V0) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    x = param.asInt();  // set variable as Slider value
    EncWV1 = x;
    Serial.print("Wave Maker 1 On Timer set to: ");
    Serial.print(EncWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V1) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    y = param.asInt();  // set variable as Slider value 
    ApagWV1 = y;  
    Serial.print("Wave Maker 1 Off Timer set to: ");
    Serial.print(ApagWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V2) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    z = param.asInt();  // set variable as Slider value
    EncWV2 = z;
    Serial.print("Wave Maker 2 On Timer set to: ");
    Serial.print(EncWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V3) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    a = param.asInt();  // set variable as Slider value 
    ApagWV2 = a;  
    Serial.print("Wave Maker 2 Off Timer set to: ");
    Serial.print(ApagWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V7) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    NivelMax = param.asInt();  // set variable as Slider value  
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMax);
    Serial.println(" segundos");
  }

BLYNK_WRITE(V8) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    NivelMin = param.asInt();  // set variable as Slider value
    
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMin);
    Serial.println(" segundos");
  }


BLYNK_WRITE(V10) {  // Called whenver setting Time Input Widget
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
}

void led_control()
  {
    //First Wave Maker
      
    if ((CounterOn > 0) && (ledStatus == 0))
      {
        digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 1: ON\n");
        ledStatus = 1;
        CounterOff = 0;
      }
   
  if ((CounterOn > 0) && (ledStatus == 1))
      {
      CounterOn = (CounterOn - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
    {
      CounterOff = ApagWV1;
    }

  if ((CounterOff > 0) && (ledStatus == 1))
  {
      digitalWrite(WM1,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 1: OFF\n");
      ledStatus = 0;
      CounterOn = 0;
   }
   
  if ((CounterOff > 0) && (ledStatus == 0))
      {
      CounterOff = (CounterOff - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
    {
      CounterOn = EncWV1+1;
    }

  //Second Wave Maker

  if ((CounterOn2 > 0) && (ledStatus2 == 0))
      {
        digitalWrite(WM2,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 2: ON\n");
        ledStatus2 = 1;
        CounterOff2 = 0;
      }
   
  if ((CounterOn2 > 0) && (ledStatus2 == 1))
      {
      CounterOn2 = (CounterOn2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 1))
    {
      CounterOff2 = ApagWV2;
    }

  if ((CounterOff2 > 0) && (ledStatus2 == 1))
  {
      digitalWrite(WM2,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 2: OFF\n");
      ledStatus2 = 0;
      CounterOn2 = 0;
   }
   
  if ((CounterOff2 > 0) && (ledStatus2 == 0))
      {
      CounterOff2 = (CounterOff2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 0))
    {
      CounterOn2 = EncWV2+1;
    }

}

void MeasureCm()
  {
   /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */ 
  
       
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
  
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); 
       
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
       
   //Calculate the distance (in cm) based on the speed of sound.
   distance = duration/58.2;
  
   Serial.print("Distance: ");
   Serial.println(distance); 
   Blynk.virtualWrite(4,distance);   

   if (distance>NivelMax)
     {
      digitalWrite(AutoRelleno, HIGH);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 1.
      AR.on();
      Serial.print("Bomba de Autorelleno Encendida");
     }
   if (distance<NivelMin)
     {
      digitalWrite(AutoRelleno, LOW);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 5.
      AR.off();
      Serial.print("Bomba de Autorelleno Apagada");
     }
   }

void Temperatura()
  {
    sensors.requestTemperatures();
    Celsius = sensors.getTempCByIndex(0);
    Blynk.virtualWrite(6,Celsius);
    Serial.print("Temperatura: ");
    Serial.print(Celsius);
    Serial.print(" C");
  }

void EncendidoSumpLamp() {  // call with timer every 30 seconds or so
  // Get RTC time  
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  Serial.print("Current Time: ");
  Serial.println(currentTime);

  // Get Time Input Widget time
  sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
  Serial.print("Start Time: ");
  Serial.println(startTime);

  sprintf(startTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
  Serial.print("Stop Time: ");
  Serial.println(startTime);
  
  if (hour() == SThour) {
    if (minute() == STmin) {
      Serial.println("Doing something now");
      digitalWrite(SumpLamp, HIGH); // Turn ON built-in LED
    } else if (minute() < STmin) {
      Serial.println("Will do something");
    } else if (minute() > STmin) {
      Serial.println("Did something");
    } else {
      Serial.println("Clueless");
    }
  }

  if (hour() == SPhour) {
    if (minute() == SPmin) {
      Serial.println("Stopping something now");
      digitalWrite(SumpLamp, LOW); // Turn OFF built-in LED
    } else if (minute() < SPmin) {
      Serial.println("Will stop something");
    } else if (minute() > SPmin) {
      Serial.println("Stopped something");
    } else {
      Serial.println("Clueless");
    }
  }
  Serial.println("----------");
}


void setup()
{
  if (EncWV1== 0)
  {
    EncWV1=1;
  }
  if (ApagWV1== 0)
  {
    ApagWV1=1;
  }

  sensors.begin();
  Serial.begin(9600); 
  Blynk.begin(auth);
  rtc.begin();
  
  CounterOn = EncWV1+1;
  // run timer every 1 second to check for led On/Off action
  timer.setInterval(1000L, led_control);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(12, OUTPUT);
     
  timer.setInterval(1000L, MeasureCm);
  timer.setInterval(20000L, Temperatura);
  timer.setInterval(1000L, EncendidoSumpLamp); // EncendidoSumpLamp Time Check     
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates Timer to ensure the timer keeps running  
} 

Works fine for a while, then wen the advanced input timmer run out it crash everything and lost conection.

You have three separate timers all trying to run three separate functions at exactly the same time every second… and then every 20 seconds, a fourth. This is not a multitasking OS :stuck_out_tongue:

Stagger your timers so that each function happens by itself, giving enough time for it to finish before running the next and so on.

How can I do that? @Gunner, changing the time interval for each function? like this:

timer.setInterval(1000L, led_control);
timer.setInterval(2000L, MeasureCm);
timer.setInterval(20000L, Temperatura);
timer.setInterval(3000L, EncendidoSumpLamp); // EncendidoSumpLamp Time Check

:smiley:

A proper offset is one that doesn’t line up every 2nd, 3rd, 4th… cycle…

For example:

1000
1020
1045
1093
1107

Granted, one has to make some trial and error guesses as to how long a given function might take.

Ok @Gunner I’ll try this. Thanks