Is there error in my logic?

I am a novice coder and am pasting in function logic for a project I built years back with Blynk 1.0, so perhaps there is old/new conflict, or perhaps I have an error in my setup() or function logic? My board is an adafruit Feather ESP8266. The project ultimately will be controlling relays and a water pump that will produce quarts of water, but for now I have an LED with resistor on D5 and GND. Will someone knowledgeable screen my code and function logic in ROcheck() ?

My exact problem is when using the Step V and Display Widget on the same V pin, I am unable to scroll into the 16 integers Iā€™ve allotted for that Vpin. As soon as I get 1, or 2, it resets to zero. If I am able to set the Vpin to 16, I will be able to produce up to 4 gallons, but now I canā€™t get past all the Vpins frequently being reset to zero. Perhaps this is an app side issue? IDK and am too novice to know the difference.


#define BLYNK_TEMPLATE_ID           "TMPLWzp-edHg"
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "XXXXYYYYZZZZ-x1pMvz8hqUuvYVQV"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Mike-N";
char pass[] = "password";

WidgetTerminal terminal(V10);
BlynkTimer timer;

int msPerQuart = 5454; //ms per Quart
int ROstart = 0;
int countQuarts = 0;
boolean runningRO = false;

#define TURN_ON HIGH // TURN_ON and TURN_OFF are defined to account for Active LOW relays
#define TURN_OFF LOW  // Used to switch relay states for on/off of 120VAC~ devices 
 
const int redLED = 0;              //On Board LED
const int feedRelay = 4;           //Digitial Pin
const int ROrelay = 5;             //Digitial Pin
const int RelayPin3 = 2;           //Digitial Pin
const int RelayPin4 = 16;          //Digitial Pin

int button;
int ROpumpOn;
int totalQuarts;
int feedPumps;

BLYNK_WRITE(V0) { //Test red LED on ESP8266
  button = param.asInt();  
}
BLYNK_WRITE(V1) { //triggers the RO countdown
  ROpumpOn = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V2) { //number of quarts ordered from app
  totalQuarts = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V3) { //button to activate all feed pumps
  feedPumps = param.asInt();
} 

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  if (ROpumpOn == 1 && runningRO == false)            // Activates when Blynk button is toggled
  {
    digitalWrite(ROrelay, TURN_ON);
    //digitalWrite(redLED, HIGH);
    runningRO = true;
    ROstart = millis();
    countQuarts = msPerQuart * totalQuarts;        // Calculates length of runtime for pump
    terminal.print("Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
    else if (millis() - ROstart > countQuarts)              // Determines when runtime ends
    {
      //ROpumpOn = 0;
      Blynk.virtualWrite(V1, 0);
      Blynk.virtualWrite(V2, 0);
      totalQuarts = 0;
      runningRO = false;
      digitalWrite(ROrelay, TURN_OFF);
      //digitalWrite(redLED, LOW);
    }
    terminal.flush();
}
void Test_PinZero()
{
  boolean buttonState = false;
  if (button == 1) buttonState = true;
  if (buttonState == true)
  {
    digitalWrite(redLED, HIGH);
  }
  else
  {
    button = 0;
    digitalWrite(redLED, LOW);
  }
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  //pinMode(redLED, OUTPUT);
  pinMode(feedRelay, OUTPUT);
  pinMode(ROrelay, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  
  digitalWrite(ROrelay, TURN_OFF);
  //Blynk.virtualWrite(V0, 0);      //button
  //Blynk.virtualWrite(V1, 0);      //ROpump
  //Blynk.virtualWrite(V2, 0);      //Number of Quarts
  //timer.setInterval(500L, Test_PinZero);
  timer.setInterval(500L, ROcheck);
}

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

FYI, the ROcheck function usually has 2 if() statements, but just trying to remedy the issue, I tried the latter as an else if().

Iā€™d start by adding serial print statements before your if statements, to show the current values of all the variables evaluated in the if statements.
Then add serial print statements within each if statement to track the program flow.

Pete.

1 Like

Would terminal.print commands serve the same purpose? The program relies on info collected from the app cloud to complete the function.

I donā€™t really follow what youā€™re saying. Is the serial port used for something else other than debugging?

Pete.

I took your advice and put serial prints in and see that the 2nd ā€œifā€ block is being looped.

What I tried to say before was that my program relies on data from Blynk to complete the function and believed that terminal could be the debugging monitor, but Serial has already sussed out my problem. Now I just need to figure out why the function puts the program right into the 2nd IF block and loops it.

It would be much easier to start a timeout timer for the required number of milliseconds.

Pete.

I stumbled onto the solution to my issues. I needed to add this to the 2nd IF statement

&& runningRO == true

and that made it function properly. Thank you for your time and wisdom!

The new issue is that it is now executing all the commands in each IF statement as if the IFs donā€™t even exist.

I am trying to control a 4chan relay with an Adafruit ESP8266 Feather Huzzah, and am trying to use old code that I once ran on an Arduino Mega. My old code is on my Git HERE and HERE, the latter being more abbreviated, but the former actually having the function I am trying to now use. The function that is troubling me is supposed to be initiated by the Blynk app and receive an int in the range of 0-16 and use that int to multiply a preset length of milliseconds and hold a digital pin HIGH for that duration. The problems are that the ā€˜ifā€™ statements are being ignored and the executable lines inside the curly braces are just being executed. Through all of the edits Iā€™ve made, Iā€™ve had varying degrees of the same problem and I canā€™t get past this block. I also donā€™t know if this is a question for Blynk, Arduino or maybe even Adafruit/ESP, but Iā€™m trying Blynk first.

Some instances, the project waits for the first commands from blynk app before beginning the never ending loop while other times it just gets right into that loop. I donā€™t know why this code performed flawlessly on another Arduino board using Blynk 1.0, but now itā€™s not. Iā€™m doing my best to articulate my problem without being over or under informative, so please forgive any lack of clarity.

Ideally, the project will receive the int when the button on another Vpin becomes true and perform the time based math operation and exit and stay idle until another set of commands is received from the Blynk app. Now, for the most part, the Blynk app isnā€™t instigating that process which is what Iā€™m hoping to achieve.

TYIA for your time!


#define BLYNK_TEMPLATE_ID           "TMPLWzp-edHg"
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "XXXXyyyyZZZZ"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Mike-N";
char pass[] = "password";

WidgetTerminal terminal(V10);
BlynkTimer timer;

#define TURN_ON HIGH // TURN_ON and TURN_OFF are defined to account for Active LOW relays
#define TURN_OFF LOW  // Used to switch relay states for on/off of 120VAC~ devices 
 
#define feedRelay 4           //Digitial Pin
#define ROrelay 5             //Digitial Pin
#define RelayPin3 2           //Digitial Pin
#define RelayPin4 16          //Digitial Pin

boolean runningRO = false;
int msPerQuart = 5454; //ms per Quart
int ROstart;            //Start of count
int countQuarts;        //Total Quarts from Blynk times msPerQuart

int button = 0;       //V0
int ROpumpOn = 0;     //V1
int totalQuarts = 0;  //V2
int feedPumps = 0;    //V3

BLYNK_WRITE(V0) { //Test red LED on ESP8266
  button = param.asInt();  
}
BLYNK_WRITE(V1) { //triggers the RO countdown
  ROpumpOn = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V2) { //number of quarts ordered from app
  totalQuarts = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V3) { //button to activate all feed pumps
  feedPumps = param.asInt();
} 

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  if (V1 == 1 && runningRO == false)  // Activates when Blynk button is toggled
  {
    Serial.println("First IF statement");
    digitalWrite(ROrelay, TURN_ON);
    runningRO = true;
    ROstart = millis();
    countQuarts = msPerQuart * totalQuarts;  // Calculates length of runtime 
    terminal.print("Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
  if (millis() - ROstart > countQuarts && runningRO == true)  // Ends Runtime
  {
    runningRO = false;
    countQuarts = 0;
    ROstart = 0;
    digitalWrite(ROrelay, TURN_OFF);
    Blynk.virtualWrite(V1, 0);    //ROpumpOn
    Blynk.virtualWrite(V2, 0);    //totalQuarts
    Serial.println("Second IF Statement");
    terminal.print("Finished Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
  terminal.flush();
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(feedRelay, OUTPUT);
  pinMode(ROrelay, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  
  digitalWrite(ROrelay, TURN_OFF);
  Blynk.virtualWrite(V0, 0);      //button
  Blynk.virtualWrite(V1, 0);      //ROpump
  Blynk.virtualWrite(V2, 0);      //Number of Quarts
  timer.setInterval(1000L, ROcheck);
}

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

Your if statement should be

if ( ROpumpOn== 1 && runningRO == false)
1 Like

Thatā€™s what it was originally, I changed the name to the Vpin on a whim. I thought leaving it as the Vpin would make it easier to read by others.

Edit - line 378 of the first link is the original function

Iā€™ve moved your latest topic into your previous one on the same subject.
Please donā€™t keep creating new topics on the same subject.

Pete.

2 Likes

My mistake for overstepping.

Can you guess what may be wrong here? This function worked on ESP and a Mega using Blynk 1.0, but now is buggy. Termnial prints show the value from the Vpin to always be 1 no matter what I type, yet the pin is held HIGH for the desired duration indicating the Vpin is passing the correct int. Also, after it completes the function, it just repeats the previously completed function over and over.

//------ RO pump increment of quarts
boolean runningRO = false;
int msPerQuart = 6000; //ms per Quart
int ROstart;            //Start of count
int countQuarts;        //Total Quarts from Blynk times msPerQuart

  int button;       //V0
  int ROpumpOn;     //V1
  int totalQuarts;  //V2
  int feedPumps;    //V3
  
BLYNK_WRITE(V0) { //Test red LED on ESP8266
  button = param.asInt();  
}
BLYNK_WRITE(V1) { //triggers the RO countdown
  ROpumpOn = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V2) { //number of quarts ordered from app
  totalQuarts = param.asInt();  // ROpump remote
}
BLYNK_WRITE(V3) { //button to activate all feed pumps
  feedPumps = param.asInt();
} 

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  if (ROpumpOn == 1 && runningRO == false)  // Activates when Blynk button is toggled
  {
    Serial.println("First IF statement");
    digitalWrite(ROrelay, TURN_ON);
    runningRO = true;
    ROstart = millis();
    countQuarts = msPerQuart * totalQuarts;  // Calculates length of runtime for pump
    terminal.print("Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
  if (millis() - ROstart > countQuarts && runningRO == true)  // Determines when runtime ends
  {
    runningRO = false;
    countQuarts = 0;
    ROstart = 0;
    digitalWrite(ROrelay, TURN_OFF);
    Blynk.virtualWrite(V1, 0);    
    Blynk.virtualWrite(V2, 0);  
    Serial.println("Second IF Statement");
    terminal.print("Finished Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
  terminal.flush();
}

Have you tried a labeled value widget ?

As weā€™ve no migrated the searching question back to the logic issue, Iā€™ve moved thyat part of the conversation back here.

As I said beforeā€¦

and this is something you havenā€™t done.

Also, instead of soldering-on with this clunky piece of code, I still think youā€™d be betterā€¦

Pete.

1 Like

I completely overlooked your advice to add Serial prints BEFORE. Iā€™ll try that now. I suppose you mean between the IF test and the opening curly brace?

Can you link me to Timeout Timer info? Iā€™ve never heard of this but it sounds like what Iā€™m looking to implement in the next function I add that will hold a Dpin HIGH for 30 seconds every 2-4 hours, but I first need to learn all that I need to know regarding that and I DO apologize for bombing this forum with relentless questions.

I just tried it and it produced the same result as when using the value display widget. A step V and a display together allows me to dial up any int between 0-16 and pass that int to the device when commanded to by a button press. The function should multiply a preset val by that int and hold the Dpin high for that number of millis(), than rest all variables and exit. It does all of this, but just keeps repeating.

More details about timers here

1 Like