Time input, scheduler for a task

I am quite new in this case so I might not understand the overall Timer Input widget. The Input Timer widget cannot be simplified and only functions like a Timer Widget, where someone gets HIGH at start and LOW times at the end time as chosen. The Input Timer widget will then function like an “inactive electronic timer” where you set your local time, and then choose a day and start and stop the time for that day. What follows is HIGH / LOW logic on the selected day / time. As I said, I might lose the point!

I want to ask how to use the time input function, just take the time start? I want to make a project with a timer? when the time has come, this tool will do a task until it is finished regardless of the stop time and only the start time? or do you have a suggest to my project, thnaks

You could still use a timer widget.

In your code, you would just check to see if the timer has triggered and if so, run your function once.

1 Like

i have done, and if i want to input a timer and some case todo, for ex( in user interface i input a clock 12.00 and task 1 is to rotate servo at 30, task 2 is 60) what widget can i use?

Ah, in that case you would probably need to use the Time Input widget, and also maybe the RTC widget to keep track of time, hardware side.

Or if you need task 2 to always run 30 minutes after task1, you could use a timer to carry out the function after the first has finished?

i have a problem about that, and i was try

Can you share what you have tried so far? - If posting code, format it properly with triple backticks (```) and remove your auth token.

I’m trying to build an application that open automaticly when the time/motor i wanted to rotate from the time input and also task input, and this what i do, i just only need time start, when the time start equal to no, the machine will do a task that i input, i am stack in to get start the machine and this my code



#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <EEPROM.h>

#define EEPROM_STATE_ADDRESS_1 128
#define EEPROM_STATE_ADDRESS_2 144
#define relayLed 27

SimpleTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V3);
WidgetLED led1(V4);
WidgetLED led2(V5);


char Date[16];
char Time[16];
char auth[] = "***********************************************";
char ssid[] = "****************************";
char pass[] = "***********************";
long startsecondswd;
long stopsecondswd;
long nowseconds;
bool isFirstConnect = true;

String displaycurrenttimepluswifi;
int wifisignal;
int manual = 0;
int oldstatus;
int automode;

char ledstate = 0;
char schestate = 0;
bool check = true;
bool check2 = true;
bool check3 = true;

void setup()
{
  pinMode(relayLed, OUTPUT);
  digitalWrite(relayLed, LOW); // set LED OFF
  Serial.begin(115200);
  Serial.println("\Starting");
  Blynk.begin(auth, ssid, pass);
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
    if ((millis() / 1000) > mytimeout + 8) { // try local server if not connected within 9 seconds
      break;
    }
  }

  EEPROM.begin(512);
  ledstate = EEPROM.read(EEPROM_STATE_ADDRESS_1) == 1 ? 1 : 0;
  schestate = EEPROM.read(EEPROM_STATE_ADDRESS_2) == 1 ? 1 : 0;
  ledonstart();

  rtc.begin();
  timer.setInterval(10000L, activetoday);  // check every 10 SECONDS if schedule should run today
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server
  timer.setInterval(5000L, clockvalue);  // check value for time
  timer.setInterval(5000L, sendWifi);    // Wi-Fi singal
  timer.setInterval(1000L, check_status);
  timer.setInterval(1000L, check_sche);
}

void ledonstart() {
  if (ledstate == 1) {
    digitalWrite(relayLed, 1);
    Blynk.virtualWrite(V2, 1);
  }
  else {
    digitalWrite(relayLed, 0);
    Blynk.virtualWrite(V2, 0);
  }

  if (schestate == 1) {
    Blynk.virtualWrite(V11, 1);
    led1.on();
    led2.off();

    if ((schestate == 1) && (check3 == true)) {
      timer.setTimeout(50, resetTerminal);
      timer.setTimeout(50, checklastbuttonpressed);
      automode = 1;
      schestate = 1;
      check3 = false;
    }
  }
  else {
    Blynk.virtualWrite(V11, 0);
    led1.off();
    led2.on();
  }
}

void check_status() {
  if (digitalRead(relayLed) == HIGH) {
    ledstate = 1;
    if (check == true) {
      EEPROM.write(EEPROM_STATE_ADDRESS_1, ledstate);
      EEPROM.commit();
      Serial.println("EEPROM Write ledstate ON 1");
    }
    check = false;
  }
  else {
    ledstate = 0;
    if (check == false) {
      EEPROM.write(EEPROM_STATE_ADDRESS_1, ledstate);
      EEPROM.commit();
      Serial.println("EEPROM Write ledstate OFF 1");
    }
    check = true;
  }
}

void check_sche() {
  if (automode == 1) {
    schestate = 1;
    if (check2 == true) {
      EEPROM.write(EEPROM_STATE_ADDRESS_2, schestate);
      EEPROM.commit();
      Serial.println("EEPROM Write schestate ON 1");

      led1.on();
      led2.off();
    }
    check2 = false;
  }
  else {
    schestate = 0;
    if (check2 == false) {
      EEPROM.write(EEPROM_STATE_ADDRESS_2, schestate);
      EEPROM.commit();
      Serial.println("EEPROM Write schestate OFF 1");

      led1.off();
      led2.on();
    }
    check2 = true;
  }
}

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    Blynk.notify("TIMER STARTING!!!!");
    isFirstConnect = false;
  }
}

void sendWifi() {
  wifisignal = map(WiFi.RSSI(), -105, -40, 0, 100);
}

void clockvalue() // Digital clock display of the time
{

  int gmthour = hour();
  if (gmthour == 24) {
    gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if (hourdigits == 1) {
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();
  if (minutedigits == 1) {
    displayminute = "0" + displayminute;
  }

  displaycurrenttimepluswifi = "                                          Clock:  " + displayhour + ":" + displayminute + "               Signal:  " + wifisignal + " %";
  Blynk.setProperty(V3, "label", displaycurrenttimepluswifi);

}

void activetoday() {       // check if schedule should run today
  if (year() != 1970) {
    if (automode == 1) {
      Blynk.syncVirtual(V10); // sync timeinput widget
    }
  }
}

void checklastbuttonpressed () {
  if (automode == 1) {
    oldstatus = 1;  //5
  } else {
    oldstatus = 2;  //6
  }
}

void restorelastbuttonpressed () {
  if (oldstatus == 1) {
    automode = 1;
    Blynk.virtualWrite(V11, 1);
  } else {
    automode = 0;
    Blynk.virtualWrite(V11, 0);
  }
}

void resetTerminal()
{
  terminal.println();
  terminal.println();
  terminal.println();
  terminal.println("AUTO Mode has been selected");
  terminal.println("Wait for update (10 seconds as maximum)");
  terminal.println();
  terminal.println();
  terminal.println();
  terminal.flush();
}

BLYNK_WRITE(V2)  // ON-OFF Manual
{
  if (param.asInt() == 1) { // boton encendido
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is ON");
    terminal.println();
    terminal.println();
    terminal.flush();

    if (manual == 0) { //está en modo automático
      checklastbuttonpressed ();
      manual = 1;
      automode = 0;
      Blynk.virtualWrite(V11, 0);
      digitalWrite(relayLed, HIGH); // set LED ON
      Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget

    } else {             //está en modo manual
      automode = 0;
      Blynk.virtualWrite(V11, 0);
      digitalWrite(relayLed, HIGH); // set LED ON
      Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
    }
  } else {

    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is OFF");
    terminal.println();
    terminal.println();
    terminal.flush();

    if (manual == 0) {   //modo automático
      checklastbuttonpressed ();
      manual = 1;
      automode = 0;
      Blynk.virtualWrite(V11, 0);
      digitalWrite(relayLed, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
    } else {
      automode = 0;
      Blynk.virtualWrite(V11, 0);
      digitalWrite(relayLed, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
    }
  }
}

BLYNK_WRITE(V11)  // Up to you selected
{
  if (param.asInt() == 1) {
    timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, checklastbuttonpressed);
    automode = 1;
    schestate = 1;
  } else {
    automode = 0;
    schestate = 0;
  }
}

BLYNK_WRITE(V10)//Up to you
{
  if (automode == 1) {
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());

    TimeInputParam t(param);

    terminal.print("Auto Mode Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    int dayadjustment = -1;
    if (weekday() == 1) {
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if (t.isWeekdaySelected(weekday() + dayadjustment)) { //Time library starts week on Sunday, Blynk on Monday
      terminal.println("Auto Mode ACTIVE today");
      terminal.flush();
     
      if (t.hasStartTime()) // Process start time
      {
        terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
        terminal.flush();
      }
     
      // Display timezone details, for information purposes only
      terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
      terminal.println("At least ONE day MUST be selected");
      //terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
      terminal.flush();

      for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
          terminal.println(String("Day ") + i + " is selected");
          terminal.flush();
        }
      }

      //
      nowseconds = ((hour() * 3600) + (minute() * 60) + second()); 
      startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
      //Serial.println(startsecondswd);  // used for debugging
      
      if (nowseconds >= startsecondswd) {
        terminal.print("Auto Mode STARTED at");
        terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
        terminal.flush();
        if (nowseconds <= startsecondswd + 30) {  // 90s on 60s timer ensures 1 trigger command is sent
          digitalWrite(relayLed, HIGH); // set LED ON
          delay(10000);
          digitalWrite(relayLed, LOW); // set LED ON
          delay(2000);
          digitalWrite(relayLed, HIGH); // set LED ON*/
          Blynk.virtualWrite(V2, 1);
          // code here to switch the relay ON
        }
      }
      else {
        digitalWrite(relayLed, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        terminal.println("Auto Mode Device NOT STARTED today");
        terminal.flush();

      }
    }
    else {
      terminal.println("Auto Mode INACTIVE today");
      terminal.flush();
      // nothing to do today, check again in 30 SECONDS time
    }
    terminal.println();
  }
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

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

i don’t know i thing this very simple but im stack about it, it just need a time input to start the machine to do task that i input. im sorry if my english language is bad, thanks

First - you will now need to create a new auth token and insert into your code, because anyone can control your machine because you share it here :wink:

Second - Sorry, now I see what you want to do, this is very easy…
Use this only as an example.

In my example, I set timer widget to V99…

BLYNK_WRITE(V99)
{
    if (param.asInt())
    {
        callMyFunctionHere();
    }
}

When the timer reaches the time I set in the timer widget on the correct day, it will start my function: callMyFunctionHere();

So instead of running callMyFunctionHere();, you could put here any code that you want to run when the timer is triggered.