Getting a stop signal when timer starts

I’m trying to create a timer that will start a motor if it’s not running and stop it at a certain time if it is running.

My problem is, if the motor is already running when the timer starts, it get’s a stop signal then when the timer ends it get’s the normal stop signal. I’m not sure why. The problem is in the “BLYNK_WRITE(V5)” area of my sketch.
I’m using a NodeMCU with Blynk.


#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#define N_DIMMERS 3
#define led_pin D0
bool enableTimer = true;  // Defaults to enable timer (button on vPin 3)

// Temp Data Start
#define ONE_WIRE_BUS D7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
// Temp Data Stop
int dimmer_pin[] = {14, 5, 15};

// Voltage Sensor Data
float vout = 0.0;
float vin = 0.0;
float R1 = 29980.0; //
float R2 = 7520.0; //
int value = 0;

char auth[] = "**";

char ssid[] = "**";
char pass[] = "**";
char host[] = "ESP-OTA";


// Select your pin with physical button
const int btnPin = D5;
const int Stop = D6;
const int Prime = D6;


WidgetLED led3(V3);
WidgetLED led4(V4);

BlynkTimer timer;

BLYNK_CONNECTED() {
  Blynk.syncAll();
}


void LedWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    led3.on();

  } else {

    led3.off ();


  }
  if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    led4.on();

  } else {

    led4.off ();

  }
}

void ButtonWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V20, HIGH);
  } else {

    Blynk.virtualWrite(V20, LOW);
  }
  if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V21, HIGH);
  } else {

    Blynk.virtualWrite(V21, LOW);
  }
  if  (digitalRead(Prime) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V22, HIGH);

  } else {

    Blynk.virtualWrite(V22, LOW);
  }

}
void myTimerEvent()
{
  value = analogRead(A0);
  vout = (value * 3.253 / 1024); // see text
  vin = vout / (R2 / (R1 + R2));
  Blynk.virtualWrite(V1, vin);
}
// Temp Data Start
void myTempEvent()
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  printTemperature(insideThermometer); // Use a simple function to print out the data
}
// Temp Data Stop

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(ONE_WIRE_BUS, INPUT);
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
  pinMode(btnPin, OUTPUT);
  pinMode(Stop, OUTPUT);
  digitalWrite(btnPin, LOW);
  digitalWrite(Stop, LOW);



  timer.setInterval(100L, LedWidget);
  timer.setInterval(100L, ButtonWidget);
  timer.setInterval(1000L, myTimerEvent);
  // Temp data start

  timer.setInterval(1000L, myTempEvent);
  Serial.println("Dallas Temperature IC Control Library Demo");

  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();
  sensors.setResolution(insideThermometer, 9);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC);
  Serial.println();

  // Temp Data Stop

  Serial.println("Booting");

  WiFi.mode(WIFI_STA);



  Blynk.begin(auth, ssid, pass);



  while (WiFi.waitForConnectResult() != WL_CONNECTED) {

    Blynk.begin(auth, ssid, pass);

    Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
    Serial.println("Retrying connection...");

  }

  /* switch off led */

  digitalWrite(led_pin, HIGH);



  /* configure dimmers, and OTA server events */

  analogWriteRange(1000);

  analogWrite(led_pin, 990);



  for (int i = 0; i < N_DIMMERS; i++) {

    pinMode(dimmer_pin[i], OUTPUT);

    analogWrite(dimmer_pin[i], 50);

  }



  ArduinoOTA.setHostname(host);

  ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade

    for (int i = 0; i < N_DIMMERS; i++) {

      analogWrite(dimmer_pin[i], 0);

    }

    analogWrite(led_pin, 0);

  });



  ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end

    for (int i = 0; i < 30; i++) {

      analogWrite(led_pin, (i * 100) % 1001);

      delay(50);

    }

  });



  ArduinoOTA.onError([](ota_error_t error) {

    (void)error;

    ESP.restart();

  });



  /* setup the OTA server */

  ArduinoOTA.begin();

  Serial.println("Ready");


}

BLYNK_WRITE(V5)  {
  int setting = param.asInt();
  enableTimer = setting;

 if ((enableTimer == 1) && (vin < 10)) {
    Serial.println("Start x on");
    digitalWrite(btnPin, HIGH);
    timer.setTimeout(15000L, []() { // 15 seconds
      digitalWrite(btnPin, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V20, LOW);
    //stop running
   
}  else  {

 if ((enableTimer == 0) && (vin > 10)) 
Serial.println("Shutdown x on");
    digitalWrite(Stop, HIGH);
    timer.setTimeout(2000L, []() { // 2 seconds
      digitalWrite(Stop, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V21, LOW);
    //stop running
  }
}

BLYNK_WRITE(V20) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
  int value = param.asInt();
  if (value == 1) {
    Serial.println("Start x on");
    digitalWrite(btnPin, HIGH);
    timer.setTimeout(15000L, []() { // 15 seconds
      digitalWrite(btnPin, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V20, LOW);
    //stop running
  }
}
BLYNK_WRITE(V21) {
  digitalWrite(Stop, param.asInt());  // Sets Stop HIGH or LOW depending on state of Button Widget.
  int kill = param.asInt();
  if (kill == 1) {
    Serial.println("Shutdown x on");
    digitalWrite(Stop, HIGH);
    timer.setTimeout(2000L, []() { // 2 seconds
      digitalWrite(Stop, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V21, LOW);
    //stop running
  }
}
BLYNK_WRITE(V22) {
  digitalWrite(Prime, param.asInt());  // Sets Prime HIGH or LOW depending on state of Button Widget.
  int Prime = param.asInt();
  if (Prime == 1) {
    Serial.println("Prime x on");
    digitalWrite(Stop, HIGH);
    timer.setTimeout(15000L, []() { // 15 seconds
      digitalWrite(Stop, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V22, LOW);
    //stop running
  }
}

BLYNK_WRITE(V23) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V24) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.

}
// Temp Data Start
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  // Serial.print(" Temp F: ");
  // Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  Blynk.virtualWrite(V2, DallasTemperature::toFahrenheit(tempC));
}
/*
   Main function. It will request the tempC from the sensors and display on Serial.
*/
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}
// Temp Data Stop

void loop()
{
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();

}

Any help would be appreciated.

Thank you in advance.

I am sure there is more that is causing your issue, but this may be contributing to it. You are declaring the same pin twice.

You also have two button widgets doing the same thing, which may cause conflict when syncing.

Also, I am not certain on this, but I do not think you can you can do a digitalRead() on an pin set as an output. I think you should make it to where ever you are setting the output pin HIGH or LOW, you also make the change.

I would personally eliminate enableTimer, and just use setting. If you are using enableTimer in other parts of your code, I would just declare setting globally and use it there as well.

Toro. this is the only place I used enableTimer… This only started after I added "&& (vin <= 10) and && (vin >= 10)) to the variables. The check to see if the equipment is running.

Thank you for the advice. I was using these as place holders. I removed them.

BLYNK_WRITE(V23) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V24) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

I fixed the problem by moving the second if variable. like this:

BLYNK_WRITE(V5)  {
  int setting = param.asInt();
  enableTimer = setting;

  if ((enableTimer == 1) && (vin < 10)) {
    Serial.println("Start x on");
    digitalWrite(btnPin, HIGH);
    timer.setTimeout(15000L, []() { // 15 seconds
      digitalWrite(btnPin, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V20, LOW);
    //stop running

  }  else if ((enableTimer == 0)&& (vin > 10)) {
    Serial.println("Shutdown x on");
    digitalWrite(Stop, HIGH);
    timer.setTimeout(2000L, []() { // 2 seconds
      digitalWrite(Stop, LOW);

    });  // END Timer Function

    Blynk.virtualWrite(V21, LOW);
    //stop running
  }
}

Thank you again.

Yes, you can. What you actually read in this case is the status of the output register. This is accessable for read and write operation. To toggle e.g. an LED the easiest way is to do something like

digitalWrite(Led, digitalRead(Led)^1);

or:

digitalWrite(Led, !digitalRead(Led));