I am trying to put together an Arduino sketch to control a peristaltic pump. One block is giving me grief, well one line in particular. The “if” test proves true as the Step Widget value is reset to 0, and also the terminalPrint posts to the terminal. However, the command to reset the button widget to 0 (OFF) does not execute. The use of syncVirtual in the BLYNK_WRITE block lead me to believe that maybe there was a conflict. Is there a conflict? I also have the same 3 statements in the setup to begin the sketch in the 0 position, but still, after Blynk reconnects, the Step returns to 0, the button does not.
Any ideas?
BLYNK_WRITE(V1) { // button
button = param.asInt(); // a button press triggers the code into action
Blynk.syncVirtual(V1); // trigger V1 function below to set current stepValue from widget
}
BLYNK_WRITE(V0) { // step
stepValue = param.asFloat(); // 0.00-80,00\0.25 per step. Once desired value is set, press button on V1
}
if (running == true)
{
terminal.print("ResetButton");
terminal.flush();
//Blynk.virtualWrite(button, 0);
Blynk.syncVirtual(V1);
Blynk.virtualWrite(stepValue, 0);
}
whole sketch
/*
This is to be a sketch to run a peristaltic pump with Arduino and Blynk. The circuit is an Ethernet Shield on a Mega
with an LED connected to D3\GND. Somewhere on the internet, Blynk runs an Android\iPhone app. On my app, I have a button
rendered as a switch (on/off) and a step value incrementer\decrementer. A value between 0.00-80.00 in 0.25 steps is dialed
in according to human preference. Once at desired value, button is pressed and the rest of the sketch can run.
With a seperate sketch, I was able to run each (of 8) pumps till 100ml of koolaid was dispensed while millis was being
counted. I did this 3 times and averaged the millis count per each motor, then divided that by 100 to derive the multiplier.
When the multiplier is multiplied with the step value, the total amount of milliseconds to produce the desired amount of
ml is known.
*/
#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[] = "AuthCode"; //Paste code that app emailed you between "quotes"
int button = 0; //expecting 0 or 1
int oldButtonState = 0;
boolean running = false;
float stepValue; //expecting 0.00-80.00
const byte pump = 3; //Pump connected @ D3
uint32_t runCount; //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);
BLYNK_WRITE(V1) { // button
button = param.asInt(); // a button press triggers the code into action
Blynk.syncVirtual(V1); // trigger V1 function below to set current stepValue from widget
}
BLYNK_WRITE(V0) { // step
stepValue = param.asFloat(); // 0.00-80,00\0.25 per step. Once desired value is set, press button on V1
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
while (Blynk.connect() == false) {}
timer.setInterval(1500L, checkPump);
Blynk.syncVirtual(V1);
pinMode(pump, OUTPUT);
//Blynk.virtualWrite(button, 0);
Blynk.syncVirtual(V1);
Blynk.virtualWrite(stepValue, 0);
}
void loop()
{
Blynk.run();
timer.run();
}
void checkPump()
{
runCount = stepValue * multiplier;
if (running == false && button != oldButtonState
&& button == 1) // launch!
{
button = oldButtonState;
startCount = millis();
running = true; // launched
digitalWrite(pump, HIGH);
terminal.print("stepValue is:");
terminal.println(stepValue);
terminal.print("runCount:");
terminal.println(runCount);
terminal.print("startCount is:");
terminal.println(startCount);
terminal.flush();
if (running == true)
{
terminal.print("ResetButton");
terminal.flush();
//Blynk.virtualWrite(button, 0);
Blynk.syncVirtual(V1);
Blynk.virtualWrite(stepValue, 0);
}
}
if (millis() - startCount > runCount)
{
digitalWrite(pump, LOW);
running = false; // done
terminal.println("DONE");
terminal.flush();
}
}