Trouble receiving value from Step Widget

I’ve been trying to write a sketch to monitor a Blynk button state, once ticked ON, retrieve the value from the step widget. This is the ground work for a pump controller, I had to comment out a lot to solely focus on the Blynk side as everything else hinges upon Blynk. I am racking my brain trying to decipher what I’m reading in the docs section as the descriptive language kind of leads me to believe that each of the things I’ve tried to get a value printed applies to what I’m searching for.

Essentially the sketch watches V1 for a change, and immediately the Serial.println produces the expected result, but after that is where I am clearly clueless. I’ve tried BLYNK_READ, BLYNK_WRITE, Blynk.virtualWrite and Blynk.syncVirtual, all in various configurations based upon nothing more than undereducated guesses, all returning the value of zero. I have the step widget output to V0 (0~80), step: 0.25, SendStep: No, Loop Values: On. The plan is to dial up a value between 0-80 in .25 steps, then tick the button widget to tell arduino to look at V0 for the value. No luck retrieving a value at all.


#include <SPI.h>                         //Used by Blynk
#include <Ethernet.h>                    //Used by Blynk
#include <BlynkSimpleEthernet.h>         //Used by Blynk
#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

//WidgetTerminal terminal(V2);

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  //pinMode(pump, OUTPUT);
  //digitalWrite(pump, LOW);
}

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


BLYNK_WRITE(V1)
{
  int button = param.asInt(); // assigning incoming value from pin V1 to a variable
  if (button == 1)
    Serial.println("button is HIGH");
  BLYNK_WRITE(V0);
  {
    Blynk.syncVirtual(V0);
    Serial.println(V0);
  }
}  /*
  ml = V0 * multiplier;             //Step Widget Value times Calibration Multiplier
  startCount = millis();              //Reference the Millis count
  stopCount = ml + startCount;
  terminal.print("ml: ");
  terminal.println(ml);
  terminal.print("startCount is: ");
  terminal.println(startCount);
  terminal.print("stopCount is: ");
  terminal.println(stopCount);
  terminal.flush();
  if (stopCount <= millis())            //If V0 x multi + start is less than or equal to total millis
  {
    digitalWrite(pump, HIGH);
  }
  else
  {
    //ml = 0;
    //startCount = 0;
    //stopCount = 0;
    //buttonState = false;
    digitalWrite(pump, LOW);
  }
  }*/

Woah you cant have BLYNK_WRITE inside another BLYNK_WRITE.

// set as globals
int buttonState; // expecting 0 or 1
float stepValue; // expecting 0.00-80.00

BLYNK_WRITE(V0){ // 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(V1){ // step
  stepValue = param.asFloat(); // set the global step value 
  Serial.println(String("stepValue = ") + stepValue ); // will output stepValue to serial
}  

Hope this makes sense. You can then use buttonState and stepValue anywhere in your sketch as globals.

Think of SyncVirtual like youre changing the step widget in the app… .as you make a change with your finger it will trigger this function to update the stepValue. but you can also “trigger” it from your hardware using SyncVirtual.

2 Likes

@myggle, just updated the code, see virtualsync and globals comments for changes

1 Like

Thank you so much Jamin! I got it working pretty good now. Still some bugs to iron out, but at least my Blynk side problems are resolved all thanks to you of course!


#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);     //1.5 second intervals

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


1 Like