Is there error in my logic?

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

It’s a step h, not step V, but here’s the screen. I’ve tried Send Step both ways, but still no go.

What happens if you add….

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V2);
}

to your code?

Pete.

Can you instruct me where to add that? While waiting for your reply, I’ll just paste it with the other declarations and see if that changes anything. Thanks again!

Edit - just pasting it outside of the function didn’t change anything. I sent a 3, it received a 1 and still repeats the results over and over.

Anywhere, but the sensible place is alongside the BLYNK_WRITE(vPin) functions.

Add a serial print statement into your BLYNK_WRITE(V2) function to print out the value of totalQuarts.
Do this in a way that makes it clear what information is being displayed, and where this serial print is coming from, so that it makes some sense when you view the serial output.

Pete.

As is, there is a print command in every part of the function and once the app button is ticked, all of the prints are executed in a manner that suggests that there are no IF tests. Also, V2 is not sending any integers except a 1 or a 0 which has me totally perplexed. I’ve pasted the print command into the BLYNK_WRITE(V2) call, but doing so didn’t change anything noticeably.

I was interested in the data that’s coming from the BLYNK_WRITE(V2) and when it is being triggered.

Is this your full sketch, or are there other parts that you’ve not shared?

Pete.

Here is my most current rendition with all of it’s messiness.


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID           "TMPLWzp-xxYx"
#define BLYNK_DEVICE_NAME           "Relays"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
#include "BlynkEdgent.h"

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 feedPumpState TURN_OFF
 
#define feedRelay 4           //Digitial Pin
#define ROrelay 5             //Digitial Pin
//#define RelayPin3 2           //Digitial Pin
#define RelayPin4 16          //Digitial Pin

//----- Feed Pump time
long previousMillis = 0; 
long MINUTE = 60000;          //ms per minute
long onTime = MINUTE * 1;
long offTime = MINUTE * 119;

//------ 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
  Serial.println(totalQuarts);
}
BLYNK_WRITE(V3) { //button to activate all feed pumps
  feedPumps = param.asInt();
} 
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V2);
}

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  Serial.println(totalQuarts);
  terminal.println(totalQuarts);
  
  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)  //Ends runtime
  {
    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();
}
/*
void feedTime() 
{
    unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > onTime) {

    if (feedPumpState == LOW)
      feedPumpState = HIGH;
    else
      feedPumpState = LOW;
      digitalWrite(feedRelay, feedPumpState);
  }
}
*/
void setup()
{
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  
  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);
  //timer.setInterval(offTime, feedTime);
}

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

Okay, my frustration finally made me put down my tablet and sit at my PC to try to sort this out!

First problem…

You’ve turned-on the “Send Step” option in your step widget…
image

The documentation says…

Send Step option allows you to send step to hardware instead of actual value of step widget.

You want the actual value (0-16) to be sent to the device, not a +1/-1 pulse that indicates that the step widget + or - button has been pressed.

Turn “Send Step” off.

Second problem…

Doing this in your second ‘if’ statement doesn’t reset ROpumpOn to 0…

    Blynk.virtualWrite(V1, 0);    

so next time around your first if statement evaluates to true again.

To solve this you need to add ROpumpOn = 0; to your second 'if` statement…

  if (millis() - ROstart > countQuarts && runningRO == true)  //Ends runtime
  {
    ROpumpOn = 0;
    runningRO = false;
    countQuarts = 0;
    ROstart = 0;
    digitalWrite(ROrelay, TURN_OFF);
    Blynk.virtualWrite(V1, 0);    
    Blynk.virtualWrite(V2, 0);  

Third problem…

Putting these commands in your void setup don’t work the way you expect…

  Blynk.virtualWrite(V0, 0);      //button
  Blynk.virtualWrite(V1, 0);      //ROpump
  Blynk.virtualWrite(V2, 0);      //Number of Quarts

They need to be in your BLYNK_CONNECTED() function, which makes the Blynk.syncVirtual(V2) command redundant…

BLYNK_CONNECTED()
{
  Blynk.virtualWrite(V0, 0);      //button
  Blynk.virtualWrite(V1, 0);      //ROpump
  Blynk.virtualWrite(V2, 0);      //Number of Quarts
}


It seems that when I say things like…

and…

it means something different to you than it does to me.

This is what I’d wanted you to do at before the first if statement in ROcheck()

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  Serial.println("Entered ROcheck function");

  Serial.print("ROpumpOn = ");
  Serial.println(ROpumpOn);
  
  Serial.print("runningRO = ");
  Serial.println(runningRO);

  Serial.print("millis() - ROstart = ");
  Serial.println(millis() - ROstart);

  Serial.print("countQuarts = ");
  Serial.println(countQuarts);

  Serial.print("runningRO = ");
  Serial.println(runningRO);
  
  Serial.println();

and in your BLYNK_WRITE(V2) function…

BLYNK_WRITE(V2) //number of quarts ordered from app
{ 
  totalQuarts = param.asInt();  // ROpump remote
  Serial.print("BLYNK_WRITE(V2) triggered, totalQuarts = ");
  Serial.println(totalQuarts);
}

Had you done this then these issues would have become clear very quickly.



The working code should look something like this…

#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
#include "BlynkEdgent.h"

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 feedPumpState TURN_OFF
 
#define feedRelay 4           //Digitial Pin
#define ROrelay 5             //Digitial Pin
//#define RelayPin3 2           //Digitial Pin
#define RelayPin4 16          //Digitial Pin

//----- Feed Pump time
long previousMillis = 0; 
long MINUTE = 60000;          //ms per minute
long onTime = MINUTE * 1;
long offTime = MINUTE * 119;

//------ 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
  Serial.print("BLYNK_WRITE(V2) triggered, totalQuarts = ");
  Serial.println(totalQuarts);
}

BLYNK_WRITE(V3) //button to activate all feed pumps
{
  feedPumps = param.asInt();
}

BLYNK_CONNECTED()
{
  Blynk.virtualWrite(V0, 0);      //button
  Blynk.virtualWrite(V1, 0);      //ROpump
  Blynk.virtualWrite(V2, 0);      //Number of Quarts
}

void ROcheck()  //RO Pump = 34 seconds on time per gallon
{
  Serial.println("Entered ROcheck function");

  Serial.print("ROpumpOn = ");
  Serial.println(ROpumpOn);
  
  Serial.print("runningRO = ");
  Serial.println(runningRO);

  Serial.print("millis() - ROstart = ");
  Serial.println(millis() - ROstart);

  Serial.print("countQuarts = ");
  Serial.println(countQuarts);

  Serial.print("runningRO = ");
  Serial.println(runningRO);
  
  Serial.println();
  
  
//  terminal.println(totalQuarts);
  
  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)  //Ends runtime
  {
    ROpumpOn = 0;
    runningRO = false;
    countQuarts = 0;
    ROstart = 0;
    digitalWrite(ROrelay, TURN_OFF);
    Blynk.virtualWrite(V1, 0);    
    Blynk.virtualWrite(V2, 0);  
    Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Second IF Statement");
    Serial.println("Finished Pumping");
    Serial.println();   
    terminal.print("Finished Pumping:");
    terminal.print(totalQuarts);
    terminal.println(" Quarts of RO");
  }
  terminal.flush();
}

void setup()
{
  Serial.begin(115200);

  pinMode(feedRelay, OUTPUT);
  pinMode(ROrelay, OUTPUT);
  //pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  digitalWrite(ROrelay, TURN_OFF);

  timer.setInterval(1000L, ROcheck);
  //timer.setInterval(offTime, feedTime);

  BlynkEdgent.begin();
}

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

Pete.

2 Likes

Thank you TREMENDOUSLY Pete for cleaning up my shotty ass code and pointing out my errors. I have excuses for all of the misplaced lines, or lack of knowing the syntax etc, but I’ll not waste your time further. But thank you so very much, you’re a Godsend! The sketch now performs flawlessly!

1 Like

This is the simplified version using a timeout timer…

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_NODE_MCU_BOARD

#include "BlynkEdgent.h"

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 feedPumpState TURN_OFF
 
#define feedRelay 4           //Digitial Pin
#define ROrelay 5             //Digitial Pin
//#define RelayPin3 2           //Digitial Pin
#define RelayPin4 16          //Digitial Pin

//------ RO pump increment of quarts
int msPerQuart = 6000; //ms per Quart
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
  Run_Pump();
}

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();
}

BLYNK_CONNECTED()
{
  Blynk.virtualWrite(V0, 0);      //button
  Blynk.virtualWrite(V1, 0);      //ROpump
  Blynk.virtualWrite(V2, 0);      //Number of Quarts
}

void Run_Pump()  //RO Pump = 34 seconds on time per gallon
{
  countQuarts = msPerQuart * totalQuarts;        // Calculates length of runtime for pump
  Serial.print("Pumping for ");
  Serial.print(countQuarts/1000);
  Serial.println(" seconds");   
  timer.setTimeout(countQuarts, []() 
  {  
    // When the timer completes, any code here will be executed
    digitalWrite(ROrelay, TURN_OFF);
    Blynk.virtualWrite(V1, 0);    
    Blynk.virtualWrite(V2, 0);  
    Serial.println("Finished Pumping");
    Serial.println();   
  }); 
}

void setup()
{
  Serial.begin(115200);

  pinMode(feedRelay, OUTPUT);
  pinMode(ROrelay, OUTPUT);
  //pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  digitalWrite(ROrelay, TURN_OFF);

  BlynkEdgent.begin();
}

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


I think you’ll agree that it’s much simpler!

This does highlight the fact that your numbers might be off though…

Your sketch says:
34 seconds on time per gallon
but if you enter 4 quarts (1 gallon in my book) then it calculates a run-time of 24 seconds, not 34.

You’d need to set msPerQuart to 8500 to get a 34 second run time for an input value of 4 quarts.

Pete.

2 Likes

@PeteKnight Thank you for all of your time, effort, patience, and guidance.
You are the best.

2 Likes