Is there error in my logic?

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

No, Like this….

That’s what the forum is for, but it would help if you kept the discussions on one problem in one place, otherwise it gets very messy.

Pete.

2 Likes

I added the Serial print outside of the tests and this is the result. It initially waits till I send the int from the app, but still only holds the Dpin HIGH for about 10 seconds(supposed to be 18), switches Dpin to LOW for about 1 second, than just repeats that result over and over as illustrated in the serial monitor.

I think you’re missing the point of doing this.

You should be printing-out the variable values at the beginning of the function, before the if statements, so that you can compare these to the logic of the if tests to understand why the program flow is doing what it is.

That’s why I said…

and

Pete.

1 Like

Adding the prints to show the integer being passed shows that the int is not being passed. I can dial it up on my app, and the device waits for the button press to receive that int, but the int itself is not being sent. Am I correct in assuming that BLYNK_WRITE is the correct way to send this int from the app to the device? This is what made me think maybe BLYNK_READ is what might be needed. totalQuarts should be the number I dial up in my app, but it’s remaining zero for some reason.

Yes.

Forget BLYNK_READ

Can you post screenshots of how your V2 datastream is configured, and how the step widget you are using to input this value is configured?

Pete.

Here is the screen of the dashboard view of that variable.

Here is the screen of the V2 settings in the app. As I understand it, I can only choose from the list of preconfigured datastream variables and can’t configure them appSide, but here’s the screen of that…

That’s the screenshot for a labeled value display widget, not an input type widget.

Pete.

there aren’t any input widgets in my project. Should there be? I have a step V and display connected to V2. The step V increments the 0-16 and the display shows which increment is selected. When a button press is acknowledged, the int on V2 is supposed to be sent off to the device, but I never intended for any data to be sent back from device to app.

The Step V widget is an input widget.
I’d like to see how you’ve configured it.

Pete.

1 Like