Blynk with NODEMCU and Timer

Hello guys,

this is my firts topic here, i have looked for the solution on this forum but I wasn't succeed on any attempt.
I am trying to build a pet feeder for my dogs and I am finished with coding and feeder construction.

The idea is to have an automatic and manual feed template at Blynk. I will set the duration of the relay on and off based on the portion amount selected on blynk app. My only problem is that I am not getting the timer to work well. I am using NTPClient and tried to use delay(), millis() and BlynkTimer.
If I choose 1, 2 or 3 at the feed amount, the result is not as planned, even if i do not change any value, the time is highly variable. For example, if feed amount is 1, I would expect to have 2 sec for relay on and I am getting values varying between 1 to 8 seconds.

What am I missing on my code? Please help me on that.

Greetings from Brazil!`

`

#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_TEMPLATE_NAME "xxx"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#define USE_NODE_MCU_BOARD
#define motor D0

#include "BlynkEdgent.h"
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const long utcOffsetInSeconds = -10800;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "0.br.pool.ntp.org", utcOffsetInSeconds);
int HH, MM,final_time;
int time_blynk, data, qtd, auto_feed, feedback;

BLYNK_WRITE(V0){
data = param.asInt();
}
BLYNK_WRITE(V1){
time_blynk = param.asInt();
}
BLYNK_WRITE(V2){
qtd = param.asInt();
}
BLYNK_WRITE(V3){
auto_feed = param.asInt();
}

void setup()
{
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  pinMode(motor, OUTPUT);
  digitalWrite(motor, LOW);
  timeClient.begin();
  qtd=1;
  feedback=0;
}
void loop() {
  BlynkEdgent.run();
  timeClient.update();
  HH = timeClient.getHours();    
  MM = timeClient.getMinutes();
  final_time = 3600*HH + 60*MM;
  
  if (time_blynk == final_time && feedback==0 && auto_feed==1){
    digitalWrite(motor, HIGH);
    Serial.print("MotorLigado");
    delay(2000*qtd);
    digitalWrite(motor,LOW);
    Serial.print("MotorDesligado");       
    feedback=1;
  }
  
  if (time_blynk != final_time){
    feedback=0;
  }

  if (data == 1){
    digitalWrite(motor, HIGH);
    Serial.print("MotorLigado");
    delay(2000*qtd);
    digitalWrite(motor,LOW);
    Serial.print("MotorDesligado"); 
  }
  Serial.print(qtd); 

}

Hi @fdsbelli

welcome to the arduino-forum.
Well done posting your code as a code-section.

I'm not familiar with Blynk. But there is a general strategy to analyse code-behaviour.
printing to the serial monitor. But not at highest possible speed.

This code-version has some debug-macros added which show with more detail what is going on.

Try this code-version and post what you get in the serial monitor.
Post the content of the serial monitor as a code-section

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);

  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}


#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_TEMPLATE_NAME "xxx"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#define USE_NODE_MCU_BOARD
#define motor D0

#include "BlynkEdgent.h"
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const long utcOffsetInSeconds = -10800;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "0.br.pool.ntp.org", utcOffsetInSeconds);
int HH, MM, final_time;
int time_blynk, data, qtd, auto_feed, feedback;

BLYNK_WRITE(V0) {
  data = param.asInt();
}

BLYNK_WRITE(V1) {
  time_blynk = param.asInt();
}

BLYNK_WRITE(V2) {
  qtd = param.asInt();
}

BLYNK_WRITE(V3) {
  auto_feed = param.asInt();
}

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

  delay(100);
  BlynkEdgent.begin();
  pinMode(motor, OUTPUT);
  digitalWrite(motor, LOW);
  timeClient.begin();
  qtd = 1;
  feedback = 0;
}


void loop() {
  BlinkHeartBeatLED(OnBoard_LED, 250);

  dbgi("01",qtd,1000); // print only once every second

  BlynkEdgent.run();
  timeClient.update();

  dbgc("00",time_blynk); // print only if value of time_blynk has CHANGED (and print only one time per change)
  
  HH = timeClient.getHours();
  MM = timeClient.getMinutes();
  final_time = 3600 * HH  +  60 * MM;

  dbgc("02",final_time); // only in case the value of "final_time" has CHANGED print the value once
  dbgc("03",feedback);   // only in case the value of "feedback" has CHANGED print the value once
  dbgc("04",auto_feed);  // only in case the value of "auto_feed" has CHANGED print the value once
  
  if (time_blynk == final_time && feedback == 0 && auto_feed == 1) {
    Serial.print("MotorLigado");
    digitalWrite(motor, HIGH);
    delay(2000 * qtd);
    Serial.print("MotorDesligado");
    digitalWrite(motor, LOW);
    feedback = 1;
  }

  if (time_blynk != final_time) {
    feedback = 0;
  }

  if (data == 1) {
    digitalWrite(motor, HIGH);
    Serial.print("MotorLigado");
    delay(2000 * qtd);
    Serial.print("MotorDesligado");
    digitalWrite(motor, LOW);
  }
}

best regards Stefan

Hi Stefan,
Thank you for your answer and sorry for my delay. I was in a business trip and without access to my personal computer.
This is what I am getting on serial monitor. I hit manual feed sometimes and auto feed once at the specific time , after that turned off auto feed. Do you see anything that is wrong below?

16:50:50.058 -> Setup-Start
16:50:50.058 -> Code running comes from file 
16:50:50.058 -> Edgent_ESP8266_vCodeForum.ino
16:50:50.058 ->   compiled Aug 26 2023 16:50:08
16:50:50.151 -> [400] 
16:50:50.151 ->     ___  __          __
16:50:50.151 ->    / _ )/ /_ _____  / /__
16:50:50.151 ->   / _  / / // / _ \/  '_/
16:50:50.151 ->  /____/_/\_, /_//_/_/\_\
16:50:50.151 ->         /___/ v1.2.0 on ESP8266
16:50:50.151 -> 
16:50:50.151 ->  #StandWithUkraine    https://bit.ly/swua
16:50:50.151 -> 
16:50:50.151 -> 
16:50:50.151 -> ----------------------------------------------------
16:50:50.151 ->  Device:    Blynk NodeMCU-71RD
16:50:50.151 ->  Firmware:  0.1.0 (build Aug 26 2023 16:50:08)
16:50:50.151 ->  Token:     kWgw - •••• - •••• - ••••
16:50:50.198 ->  Platform:  ESP8266 @ 80MHz
16:50:50.198 ->  Boot ver:  6
16:50:50.198 ->  SDK:       2.2.2-dev(38a443e)
16:50:50.198 ->  ESP Core:  3.0.2
16:50:50.198 ->  Flash:     4096K
16:50:50.198 ->  Free mem:  30184
16:50:50.198 -> ----------------------------------------------------
16:50:50.198 -> 
16:50:50.198 -> >[440] INIT => CONNECTING_NET
16:50:50.198 -> [443] Connecting to WiFi: Belli
16:50:56.015 -> [6262] Using Dynamic IP: 192.168.0.66
16:50:56.015 -> [6262] CONNECTING_NET => CONNECTING_CLOUD
16:50:56.390 -> "02" final_time changed from 0 to 60600
16:50:56.390 -> "01" qtd=1
16:50:56.531 -> [6769] Current time: Sat Aug 26 19:50:56 2023
16:50:56.531 -> [6769] Connecting to blynk.cloud:443
16:50:58.298 -> [8543] Ready (ping: 11ms).
16:50:58.486 -> [8750] CONNECTING_CLOUD => RUNNING
16:50:58.486 -> "01" qtd=1
16:50:59.512 -> "01" qtd=1
16:51:00.356 -> "02" final_time changed from 60600 to 60660
16:51:00.497 -> "01" qtd=1
16:51:01.481 -> "01" qtd=1
16:51:02.512 -> "01" qtd=1
16:51:03.496 -> "01" qtd=1
16:51:04.480 -> "01" qtd=1
16:51:05.511 -> "01" qtd=1
16:51:06.495 -> "01" qtd=1
16:51:07.479 -> "01" qtd=1
16:51:08.510 -> "01" qtd=1
16:51:09.494 -> "01" qtd=1
16:51:10.479 -> "01" qtd=1
16:51:11.510 -> "01" qtd=1
16:51:12.494 -> "01" qtd=1
16:51:13.478 -> "01" qtd=1
16:51:14.510 -> "01" qtd=1
16:51:15.494 -> "01" qtd=1
16:51:16.478 -> "01" qtd=1
16:51:17.509 -> "01" qtd=1
16:51:18.493 -> "01" qtd=1
16:51:19.477 -> "01" qtd=1
16:51:20.508 -> "01" qtd=1
16:51:21.493 -> "01" qtd=1
16:51:22.478 -> "01" qtd=1
16:51:23.509 -> "01" qtd=1
16:51:24.493 -> "01" qtd=1
16:51:25.477 -> "01" qtd=1
16:51:26.509 -> "01" qtd=1
16:51:27.493 -> "01" qtd=1
16:51:28.477 -> "01" qtd=1
16:51:29.508 -> "01" qtd=1
16:51:30.480 -> "01" qtd=1
16:51:31.511 -> "01" qtd=1
16:51:32.482 -> "01" qtd=1
16:51:33.514 -> "01" qtd=1
16:51:34.503 -> "01" qtd=1
16:51:35.487 -> "01" qtd=1
16:51:36.518 -> "01" qtd=1
16:51:37.504 -> "01" qtd=1
16:51:38.515 -> "01" qtd=1
16:51:39.499 -> "01" qtd=1
16:51:40.484 -> "01" qtd=1
16:51:41.515 -> "01" qtd=1
16:51:42.495 -> "01" qtd=1
16:51:43.620 -> "01" qtd=1
16:51:44.651 -> "01" qtd=1
16:51:45.629 -> "01" qtd=1
16:51:46.620 -> "01" qtd=1
16:51:47.651 -> "01" qtd=1
16:51:48.635 -> "01" qtd=1
16:51:49.666 -> "01" qtd=1
16:51:50.651 -> "01" qtd=1
16:51:51.635 -> "01" qtd=1
16:51:52.666 -> "01" qtd=1
16:51:53.650 -> "01" qtd=1
16:51:54.626 -> "01" qtd=1
16:51:55.660 -> "01" qtd=1
16:51:56.645 -> "01" qtd=1
16:51:57.629 -> "01" qtd=1
16:51:58.647 -> "01" qtd=1
16:51:59.631 -> "01" qtd=1
16:52:00.334 -> "02" final_time changed from 60660 to 60720
16:52:00.662 -> "01" qtd=1
16:52:01.646 -> "01" qtd=1
16:52:02.630 -> "01" qtd=1
16:52:03.661 -> "01" qtd=1
16:52:04.645 -> "01" qtd=1
16:52:05.629 -> "01" qtd=1
16:52:06.660 -> "01" qtd=1
16:52:07.646 -> "01" qtd=1
16:52:08.630 -> "01" qtd=1
16:52:09.661 -> "01" qtd=1
16:52:10.645 -> "01" qtd=1
16:52:11.631 -> "01" qtd=1
16:52:12.662 -> "01" qtd=1
16:52:13.646 -> "01" qtd=1
16:52:14.630 -> "01" qtd=1
16:52:15.661 -> "01" qtd=1
16:52:16.645 -> "01" qtd=1
16:52:17.630 -> "01" qtd=1
16:52:18.661 -> "01" qtd=1
16:52:19.645 -> "01" qtd=1
16:52:20.629 -> "01" qtd=1
16:52:21.660 -> "01" qtd=1
16:52:22.644 -> "01" qtd=1
16:52:23.628 -> "01" qtd=1
16:52:24.659 -> "01" qtd=1
16:52:25.643 -> "01" qtd=1
16:52:26.627 -> "01" qtd=1
16:52:27.658 -> "01" qtd=1
16:52:28.643 -> "01" qtd=1
16:52:29.627 -> "01" qtd=1
16:52:30.622 -> "01" qtd=1
16:52:31.653 -> "01" qtd=1
16:52:32.637 -> "01" qtd=1
16:52:33.664 -> "01" qtd=1
16:52:34.648 -> "01" qtd=1
16:52:35.632 -> "01" qtd=1
16:52:36.664 -> "01" qtd=1
16:52:37.650 -> "01" qtd=3
16:52:38.634 -> "01" qtd=3
16:52:39.665 -> "01" qtd=3
16:52:40.649 -> "01" qtd=3
16:52:41.634 -> "01" qtd=3
16:52:42.618 -> "01" qtd=3
16:52:42.805 -> MotorLigadoMotorDesligado"01" qtd=3
16:52:45.148 -> "01" qtd=3
16:52:46.132 -> "01" qtd=3
16:52:47.117 -> "01" qtd=3
16:52:48.148 -> "01" qtd=3
16:52:49.132 -> "01" qtd=3
16:52:49.507 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:52:51.147 -> "01" qtd=3
16:52:52.131 -> "01" qtd=3
16:52:52.924 -> MotorLigadoMotorDesligado"01" qtd=3
16:52:55.033 -> "01" qtd=3
16:52:56.017 -> "01" qtd=3
16:52:57.001 -> "01" qtd=3
16:52:58.032 -> "01" qtd=3
16:52:59.016 -> "01" qtd=3
16:52:59.391 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:00.328 -> "02" final_time changed from 60720 to 60780
16:53:01.031 -> "01" qtd=3
16:53:02.017 -> "01" qtd=3
16:53:03.001 -> "01" qtd=3
16:53:04.032 -> "01" qtd=3
16:53:05.016 -> "01" qtd=3
16:53:05.016 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:07.031 -> "01" qtd=3
16:53:08.015 -> "01" qtd=3
16:53:08.999 -> "01" qtd=3
16:53:09.609 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:11.014 -> "01" qtd=3
16:53:11.577 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:14.764 -> "01" qtd=3
16:53:15.795 -> "01" qtd=3
16:53:16.780 -> "01" qtd=3
16:53:17.764 -> "01" qtd=3
16:53:18.795 -> "01" qtd=3
16:53:19.779 -> "01" qtd=3
16:53:20.763 -> "01" qtd=3
16:53:21.794 -> "01" qtd=3
16:53:22.778 -> "01" qtd=3
16:53:23.762 -> "01" qtd=3
16:53:24.512 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:24.793 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:29.574 -> "01" qtd=3
16:53:30.607 -> "01" qtd=3
16:53:31.591 -> "01" qtd=3
16:53:32.575 -> "01" qtd=3
16:53:33.606 -> "01" qtd=3
16:53:33.793 -> "04" auto_feed changed from 0 to 1
16:53:34.590 -> "01" qtd=3
16:53:35.574 -> "01" qtd=3
16:53:36.605 -> "01" qtd=3
16:53:37.592 -> "01" qtd=3
16:53:38.576 -> "01" qtd=3
16:53:39.607 -> "01" qtd=3
16:53:40.591 -> "01" qtd=3
16:53:41.576 -> "01" qtd=3
16:53:42.607 -> "01" qtd=3
16:53:43.591 -> "01" qtd=3
16:53:43.638 -> "00" time_blynk changed from 0 to 60900
16:53:44.575 -> "01" qtd=3
16:53:45.606 -> "01" qtd=3
16:53:46.590 -> "01" qtd=3
16:53:47.574 -> "01" qtd=3
16:53:48.605 -> "01" qtd=3
16:53:48.980 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:53:50.573 -> "01" qtd=3
16:53:51.136 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:54.279 -> "01" qtd=3
16:53:55.310 -> "01" qtd=3
16:53:56.294 -> "01" qtd=3
16:53:57.278 -> "01" qtd=3
16:53:58.309 -> "01" qtd=3
16:53:58.356 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:54:00.278 -> "01" qtd=3
16:54:00.371 -> "02" final_time changed from 60780 to 60840
16:54:01.309 -> "01" qtd=3
16:54:02.293 -> "01" qtd=3
16:54:03.277 -> "01" qtd=3
16:54:03.371 -> MotorLigadoMotorDesligadoMotorLigadoMotorDesligado"01" qtd=3
16:54:05.292 -> "01" qtd=3
16:54:05.761 -> MotorLigadoMotorDesligado"01" qtd=3
16:54:09.135 -> "01" qtd=3
16:54:10.119 -> "01" qtd=3
16:54:11.103 -> "01" qtd=3
16:54:12.134 -> "01" qtd=3
16:54:13.118 -> "01" qtd=3
16:54:14.102 -> "01" qtd=3
16:54:15.133 -> "01" qtd=2
16:54:16.118 -> "01" qtd=2
16:54:16.211 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:18.974 -> "01" qtd=2
16:54:20.005 -> "01" qtd=2
16:54:20.989 -> "01" qtd=2
16:54:21.973 -> "01" qtd=2
16:54:23.005 -> "01" qtd=2
16:54:23.989 -> "01" qtd=2
16:54:24.880 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:28.874 -> "01" qtd=2
16:54:29.858 -> "01" qtd=2
16:54:30.873 -> "01" qtd=2
16:54:31.856 -> "01" qtd=2
16:54:32.887 -> "01" qtd=2
16:54:33.871 -> "01" qtd=2
16:54:34.106 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:38.732 -> "01" qtd=2
16:54:39.763 -> "01" qtd=2
16:54:40.747 -> "01" qtd=2
16:54:41.731 -> "01" qtd=2
16:54:42.762 -> "01" qtd=2
16:54:43.747 -> "01" qtd=2
16:54:44.731 -> "01" qtd=2
16:54:45.762 -> "01" qtd=2
16:54:45.855 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:48.621 -> "01" qtd=2
16:54:49.605 -> "01" qtd=2
16:54:50.636 -> "01" qtd=2
16:54:51.620 -> "01" qtd=2
16:54:52.604 -> "01" qtd=2
16:54:53.635 -> "01" qtd=2
16:54:54.619 -> "01" qtd=1
16:54:55.603 -> "01" qtd=1
16:54:56.634 -> "01" qtd=1
16:54:57.618 -> "01" qtd=1
16:54:58.602 -> "01" qtd=1
16:54:59.634 -> "01" qtd=1
16:55:00.383 -> "02" final_time changed from 60840 to 60900
16:55:00.383 -> MotorLigadoMotorDesligado"01" qtd=1
16:55:02.445 -> "03" feedback changed from 0 to 1
16:55:03.430 -> "01" qtd=1
16:55:04.461 -> "01" qtd=1
16:55:05.445 -> "01" qtd=1
16:55:06.429 -> "01" qtd=1
16:55:07.461 -> "01" qtd=1
16:55:08.446 -> "01" qtd=1
16:55:09.337 -> MotorLigadoMotorDesligado"01" qtd=1
16:55:13.320 -> "01" qtd=1
16:55:14.305 -> "01" qtd=1
16:55:15.336 -> "01" qtd=1
16:55:16.320 -> "01" qtd=1
16:55:17.304 -> "01" qtd=1
16:55:18.335 -> "01" qtd=1
16:55:19.319 -> "01" qtd=1


I don't know. My support was to post a code-version that enables to analyse the real program flow. As long as you can't describe very very detailed what functionality you want to have. How should I judge if everything is correct or not?

Sorry for not explaining so well.
My objective with this code is to press the "manual feed" button and depending on the quantity I selected (qtd =1,2 or 3), the motor is turned on for 2, 4 or 6 seconds. My problem is that my delay is not working properly.

If you see the serial monitor, "MotorLigadoMotorDesligado" string means that it went through my feed function and Blynk is working good. But the intervals are varying too much. First line, 16:52:45.148 minus 16:52:42.805, it stayed on for 2,3 seconds and I expected 6 seconds. Other examples:
16:53:05.016 - 16:53:07.031 = 2 seconds
16:53:14.764 - 16:53:11.577 = 3,2 seconds.

I have tried millis() and BlynkTimer as well, none of them worked well. Is there anything that I should modify on the code, or is there a possibility of being the NODEMCU the problem?

16:52:42.805 -> MotorLigadoMotorDesligado"01" qtd=3
16:52:45.148 -> "01" qtd=3

16:52:52.924 -> MotorLigadoMotorDesligado"01" qtd=3
16:52:55.033 -> "01" qtd=3

16:53:05.016 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:07.031 -> "01" qtd=3

16:53:09.609 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:11.014 -> "01" qtd=3

16:53:11.577 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:14.764 -> "01" qtd=3

16:53:24.793 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:29.574 -> "01" qtd=3

16:53:48.980 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:50.573 -> "01" qtd=3

16:53:51.136 -> MotorLigadoMotorDesligado"01" qtd=3
16:53:54.279 -> "01" qtd=3

16:53:58.356 -> MotorLigadoMotorDesligado"01" qtd=3
16:54:00.278 -> "01" qtd=3

16:54:03.371 -> MotorLigadoMotorDesligado"01" qtd=3
16:54:05.292 -> "01" qtd=3

16:54:05.761 -> MotorLigadoMotorDesligado"01" qtd=3
16:54:09.135 -> "01" qtd=3

16:54:16.211 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:18.974 -> "01" qtd=2

16:54:23.989 -> "01" qtd=2
16:54:24.880 -> MotorLigadoMotorDesligado"01" qtd=2

16:54:34.106 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:38.732 -> "01" qtd=2

16:54:45.855 -> MotorLigadoMotorDesligado"01" qtd=2
16:54:48.621 -> "01" qtd=2

16:55:00.383 -> MotorLigadoMotorDesligado"01" qtd=1
16:55:02.445 -> "03" feedback changed from 0 to 1

16:55:09.337 -> MotorLigadoMotorDesligado"01" qtd=1
16:55:13.320 -> "01" qtd=1

I haven't analysed your code if you follow the concept I describe below the timing-details are handled completely locally on the I call it the feeding-contro-arduino

Example feeding through motor for two seconds
If you are trying to remotely control the thing by sending some data to Blynk
then waiting for an answer from blynk before your feeding-arduino goes on executing code and this datatransmission is repeated multiple times within the process of

switch feeding-motor on
(dataprocessing back and forth with blynk)
2 seconds later switch feeding-motor off

Blynk wil slow down everything. If I remember right Blynk has a limitation to not process commands that wants to exchange data more often than once every second. Blynk has 100.000 of users imagine each user having 10 to 50 devices and all of them sending data 100 times per second:
that would result in a total Blynk-Server-overload

A different approach will work better:
remote unit is giving commands.

Examples:

Sending a command "Motor on for 2 seconds"
This single "command" is received over blynk by and all the details

make timestamp 
switch on motor
keep motor on for  2 seconds
switch of motor

Are done locally without any data-pin-pong-transmission over blynk between your feeding-arduino and your remote-device

So these commands could be different blynk--App-buttons
blynk-App-button 2 seconds
blynk-App-button 4 seconds
blynk-App-button 6 seconds

set automatic-feed time hour
set automatic-feed time minute
etc.

If this is running you could try adding to send back status-info
If you have tapped the 4-second-button
the feeding-arduino sends back a single byte that indicates "motor ON"
if the motor is switched off send a single byte "motor OFF"

best regards Stefan

To make it easier to analyse the serial output you should use serial.println()
for each and every printing.

  if (time_blynk == final_time && feedback == 0 && auto_feed == 1) {
    Serial.print("MotorLigado");
    digitalWrite(motor, HIGH);
    delay(2000 * qtd);
    Serial.print("MotorDesligado");
    digitalWrite(motor, LOW);
    feedback = 1;
  }

change to

  if (time_blynk == final_time && feedback == 0 && auto_feed == 1) {
    Serial.println("MotorLigado"); // <<<=== USE printLN
    digitalWrite(motor, HIGH);
    delay(2000 * qtd);
    Serial.println("MotorDesligado"); // <<<=== USE printLN
    digitalWrite(motor, LOW);
    feedback = 1;
  }

as you are using a delay(2000) even multiplied with a factor
as long as this delay() is executed there willbe no communication with Blynk
I'm pretty sure that a code-template using

  BlynkEdgent.run();
  timeClient.update();

has a comment
call BlynkEdgent.run(); repeatedly and very often

You wil have to change your code to be completely non -blocking.

This requires a completely different thinking than using delay()

You can read about it here

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.