Blynk with millis()

I am trying to write a sketch that queries the virtual pin state of an app on my Android phone. The software that handles all of the networking is called Blynk and is available for free in Google Play and appStore. The circuit for now is just an LED on pin 2 w/220 ohm resistor.

In the app, I have 2 things, a switch and a value generator and that increments from 0-80 in .25 increments. I want to dial in the value I want (when I want), then press the button to send that value over the internet to my Mega. In there, I want that value to be multiplied by a preset number of milliseconds. This new number will then be added to the startmillis count to know what the stopmillis count is to be. While the stopmillis count is below the total millis uptime, light up the LED. When stopmillis catches up to millis, turn off LED and reset the button value to zero so the process may be repeated at any given moment in the future.

The networking aspect of the sketch works as the button was toggled off in the app on my phone, but after bootup and setup, the LED simply turns on and stays on. I also posted this question on the Blynk forum to increase my chances of being shown my error(s). I hope someone can help. I've been working on this short sketch for the better part of a week.

TYIA

#include <SPI.h>                         //Used by Blynk
#include <Ethernet.h>                    //Used by Blynk
#include <BlynkSimpleEthernet.h>         //Used by Blynk
#include <SimpleTimer.h>                 //Used to run functions at preset intervals
#define BLYNK_PRINT Serial
char auth[] = "PasteAuthCodeHere";       //Paste code that app emailed you between "quotes"

#define pump 3                           //Pump connected @ D2
float ml;                                //Step Widget to determine amount of ml/Output 0-80/Step 0.2                              //
uint32_t startCount;                     //Millis reference used to calculate stopCount
uint32_t stopCount;                      //
uint32_t multiplier = 858;               //Number of milliseconds for motor to produce 1ml of liquid

SimpleTimer timer;                       //SimpleTimer instance named timer

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(2000L, checkPump);   //checkPump function is tested every 2 seconds
  pinMode(pump, OUTPUT);
  digitalWrite(pump, LOW);
  Blynk.virtualWrite(V1, 0);
}

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

void checkPump()
{
  BLYNK_READ(V1);                         //Read Button/Switch state
  if ((V1) == 1)                          //If Switch is HIGH, execute following code
  {
    BLYNK_READ(V0);                       //Read Step Widget Value x multiplier
    {
      ml = (V0) * multiplier;             //Step Widget Value times Calibration Multiplier
      startCount = millis();              //Reference the Millis count
      stopCount = ml + startCount;
    }
    if (stopCount <= millis())            //If V0 x multi + start is less than or equal to total millis
    {
      digitalWrite(pump, HIGH);
      Blynk.virtualWrite(V1, 0);          //Reset Switch back to OFF position
    }
    else
    {
      //ml = 0;
      //startCount = 0;
      //stopCount = 0;
      digitalWrite(pump, LOW);
    }
  }

}

I hope someone can help.

You've described your expectations. You have not described what the sketch actually does, or what the problem is.

PaulS:
You've described your expectations. You have not described what the sketch actually does, or what the problem is.

Sorry for that. The project is a peristaltic pump (12v motor). The problem I am having is that shortly after the setup, the LED I'm using in place of pump circuit goes HIGH and remains so indefinitely. I've verified the network connection and the button state returning to off when expected to which leads me to believe I didn't structure the millis reads and math correctly. I read through the code and it makes sense in my mind, but for some reason the LED keeps going HIGH and I don't know why.

What do your Serial.print()s tell you is happening?

Why not?

are you sure the contents of V0 are correct ? the loop isnt dangerous ? if you multiply then compare the value to see if you can switch the led off. if you read V0 (im not sure i understood it right)
then, AFTER that you initilize pump to 0 ??

PaulS:
What do your Serial.print()s tell you is happening?

Why not?

You were correct, I added Serial.print(ln) and the return values were not what I was hoping for. Can you please point out where I went wrong? I see that I need to figure out why no value is being sent from Blynk (V0), but it appears as if other things are not going as planned such as the difference between start and stopCount.

oswe:
are you sure the contents of V0 are correct ? the loop isnt dangerous ? if you multiply then compare the value to see if you can switch the led off. if you read V0 (im not sure i understood it right)
then, AFTER that you initilize pump to 0 ??

I want to dial up my desired amount of milliliters in the Blynk app. Once I have that value, I then tick the button (V1) to the ON state. If the Arduino sees this button is ON, I want it to read the value from V0 and multiply this value times the amount of milliseconds it takes a pump to produce 1ml of liquid. So for 10ml of liquid, 10x858=8580. StartCount + 8580 = stopCount. If stopCount is less than or equal to millis, pump HIGH. Else, pump LOW.

Posting pictures of text is a sure way of getting people to ignore you. Post the text AS TEXT.

Though doing so now is rather pointless, since you know that the problem is that the phone is either not sending data or is not sending it the way that you expect.

You need to take that part up with the blynk forum.

I'll note that for next time, thank you.

With the help of other Blynkers, I now have a functional sketch that produces correctly according to the sketch, but I still have incorrect structure in my tests and the result is the LED connected to D3 only illuminates momentarily. What I would like is for the LED to illuminate for the entire duration of milliseconds that is derived from the math.

In the Blynk app, a Step widget is used to dial a value from 0.00 - 80.00 in 0.25 increments. Once the value I choose is ready to be passed to the Arduino, a button widget is ticked and the Arduino sees the state change and retrieves the float called stepValue. stepValue is multiplied with a calibration multiplier (858) as it takes the pump 858 ms to produce 1ml of fluid. I think it's after this point that I go astray, as I am very new to working with millis.

#include <SPI.h>                           //Used by Blynk
#include <Ethernet.h>                      //Used by Blynk
#include <BlynkSimpleEthernet.h>           //Used by Blynk
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
char auth[] = "PasteAuthCodeHere";       //Paste code that app emailed you between "quotes"

int buttonState;                           //expecting 0 or 1
float stepValue;                           //expecting 0.00-80.00
#define pump 3                             //Pump connected @ D3
float ml;                                  //Step Widget to determine amount of ml/Output 0-80/Step 0.2                              //
uint32_t startCount;                       //Millis reference used to calculate stopCount
uint32_t stopCount;                        //
uint32_t multiplier = 858;                 //Number of milliseconds for motor to produce 1ml of liquid
SimpleTimer timer;                         // SimpleTimer instance named timer
WidgetTerminal terminal(V2);

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(1500L, checkPump);     // 33 second intervals between timed routiness

  Blynk.syncVirtual(V1);
  pinMode(pump, OUTPUT);
  //analogWrite(pump, 0);
}

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


// set as globals


BLYNK_WRITE(V1) { // button
  buttonState = param.asInt();             // set the global button state as ON or OFF
  Serial.println(String("button state = ") + buttonState); // will output 1 or 0 to serial monitor
  Blynk.syncVirtual(V1); // trigger V1 function below to set current stepValue from widget, also put this line in setup() right after connecting to update on boot
}

BLYNK_WRITE(V0) { // step
  stepValue = param.asFloat(); // set the global step value
  Serial.println(String("stepValue = ") + stepValue ); // will output stepValue to serial
}

void checkPump()
{
  if (buttonState == 1)
  {
    ml = stepValue * multiplier;             //Step Widget Value times Calibration Multiplier
    startCount = millis();              //Reference the Millis count
    stopCount = ml + startCount;
    terminal.print("stepValue is: ");
    terminal.println(stepValue);
    terminal.print("ml: ");
    terminal.println(ml);
    terminal.print("startCount is: ");
    terminal.println(startCount);
    terminal.print("stopCount is: ");
    terminal.println(stopCount);
    Blynk.virtualWrite(buttonState, 0);
    terminal.flush();
  }

  if (stopCount <= (millis() - startCount))            //If V0 x multi + start is less than or equal to total millis
  {
    analogWrite(pump, 255);
    terminal.println("LED");
    terminal.flush();
  }
  else (buttonState = 0);
  {
    ml = 0;
    startCount = 0;
    stopCount = 0;
    analogWrite(pump, 0);
  }
}

I've been learning Arduino, code, logic, circuitry and other aspects that directly relate to my project for almost 3 years. My approach has been "fake it till ya make it" so to speak. I expose myself to everything that "appears" to be related and read/listen till I prove that it does or doesn't apply. I've learned a lot, but if I don't use it frequently, I've also forgotten a lot and may need to one day relearn something I already went through. I am a hobbyist at best, so please forgive the crudeness of my sketch. Any help anyone can give, or just general insight into how my math sequences should be structured will be most helpful and greatly appreciated in advance.

  else (buttonState = 0);
  {
    ml = 0;
    startCount = 0;
    stopCount = 0;
    analogWrite(pump, 0);
  }

What is that else statement supposed to be doing?

That is not laid out the same as, but is functionally equivalent to:

  else
    buttonState = 0;

  {
    ml = 0;
    startCount = 0;
    stopCount = 0;
    analogWrite(pump, 0);
  }

It seems unlikely that that is what you want.

The else statement (the way I'm understanding it) is supposed to acknowledge that the buttonState has been changed to =0, and to also revert the other variables to =0, and also bring D3 LOW again. To my knowledge, else does the opposite of previous tests.

To my knowledge, else does the opposite of previous tests.

You need to write an if/else statement, then, showing your understanding of "if the water level is more than 4 feet high, drainTheTank(). Otherwise, addMoreWater()".

I suspect that you do NOT understand what goes after the else statement, but I need to see what your understanding is, to be sure.

PaulS:
You need to write an if/else statement, then, showing your understanding of "if the water level is more than 4 feet high, drainTheTank(). Otherwise, addMoreWater()".

I suspect that you do NOT understand what goes after the else statement, but I need to see what your understanding is, to be sure.

byte checkLevel = digitalRead(sensorPin);
byte addMoreWater = digitalWrite(pumpPin, HIGH);
byte drainTheTank = digitalWrite(valvePin, HIGH);

if (checkLevel == HIGH)
{ 
drainTheTank;
}

else 
{
addMoreWater;
}

It would actually be:

byte checkLevel = digitalRead(waterLevelPin);
void addMoreWate();
void drainTheTank();

if (checkLevel == HIGH)
{
   drainTheTank();
}

else
{
   addMoreWater();
}

But, you had the correct idea that ELSE IS FOLLOWED BY NOTHING EXCEPT A BODY.

So, explain what the "(buttonState = 0);" following your else statement is, if it isn't the body (the code to be executed if the if condition is false).

If it IS the body, then:

  1. Why is it enclosed in parentheses?
  2. Why are the next few statements enclosed in curly braces?