Need Help with a counter based off a Button connected to a relay

You can use this formula :

Time = volume / rate

For example :
Time = 500 gallons required / 4 gallons per minute
Time = 125 minutes

Then you will use this in a timer like this :

timer.setTimeout(time_to_wait , []() 
  

Check this out :

Could you assist with coding this? I declared a blynk timer, set interval, but how would I request if the relay is active and also code in the counter? Ideally, I wouldn’t need to reset the counter

Hope this can help you.

I’m not going to write your code for you.

You could either do a digitalRead on the pin the relay is connected to, or set a flag variable when you activate the pump and check the state of that variable.

The counter is simple, if you understand basic C++ coding, and you could add a Blynk button widget connected to a virtual pin to reset the counter.

Pete.

Okay, this is my preliminary code. I apologize in advance for my weak coding skills, I’m trying to teach myself by reading everything and watching youtube videos. Copying and Pasting code is useless if you dont understand whats happening. I tried to keep my loop clean. I had my virtualwrite in the loop before and it was working fine but ive since cleaned it up. How does this look? It compiles with no issues

//A0-AIR PRESSURE SENSOR
//A1-WATER PRESSURE SENSOR
//D2-DHT SENSOR 1
//D4-DHT SENSOR 2


//V1-
//V2-
//V3-
//V4-
//V5- HUMIDITY SENSOR 1
//V6- TEMPERATURE SENSOR 1
//V7- HUMIDITY SENSOR 2
//V8- TEMPERATURE SENSOR 2
//V9- PRESSURE AIR
//V10- PRESSURE WATER
//V11- OFFLINE LED
//V12- ONLINE LED
//V13- WATER METER
//V14-



#include <Arduino.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include "DHT.h"
#define BLYNK_PRINT Serial

BlynkTimer timer;
BlynkTimer timer2;

int ispumpon, i=0;      // variable to store the read value
int sensorDataSend;

//Air Pressure Sensor
const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 150; //psi value of transducer being used
float pressureValue = 0;

//Water Pressure Sensor
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureZero1 = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax1 = 921.6; //analog reading of pressure transducer at 1000psi
const int pressuretransducermaxPSI1 = 1000; //psi value of transducer being used
float pressureValue1 = 0;



const int baudRate = 9600; 
const int sensorreadDelay = 1000; 


char auth[] = "RVovq6azrEAqop3ahndbFr38DvVrhDtm";
char ssid[] = "cait&caiden";
char pass[] = "Kinghenry3";




#define DHT1_PIN 2     
#define DHT2_PIN 4     
#define DHTTYPE DHT21  


DHT dht1(DHT1_PIN, DHTTYPE);
DHT dht2(DHT2_PIN, DHTTYPE);


 




void setup(){

 timer.setInterval(15000L, ispumpon); 
 timer2.setInterval(1000L, sensorDataSend); 
 
   Serial.begin(9600);
  dht1.begin();
  dht2.begin();
 


  sensorDataSend = 
 
        pressureValue = analogRead(pressureInput);
        pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);

        pressureValue1 = analogRead(pressureInput1);
        pressureValue1 = ((pressureValue1 - pressureZero1) * pressuretransducermaxPSI1) / (pressureMax1 - pressureZero1);



        float h = dht1.readHumidity();
        float t = dht1.readTemperature(true);
        float h1 = dht2.readHumidity();
        float t1 = dht2.readTemperature(true);

        Blynk.virtualWrite(V5, h);
        Blynk.virtualWrite(V6, t);
        Blynk.virtualWrite(V7, h1);
        Blynk.virtualWrite(V8, t1-2);
        Blynk.virtualWrite(V9, pressureValue, 1);
        Blynk.virtualWrite(V10, pressureValue1, 1);


 {      
 ispumpon = digitalRead(10); 
 if (ispumpon==1) 
 {  
  i=i+1;    
Serial.print(" Counter =");   
Serial.println(i); 
Blynk.virtualWrite(V13, i);
 }
else   
{   
 i=i;  
 } }}





void loop() {
  
  timer.run();
  timer2.run();
  
  if(Blynk.connected()){Blynk.run();
    Blynk.virtualWrite(V11, 255);
    
  } else Blynk.begin(auth, ssid, pass);
    Blynk.virtualWrite(V12, 0);




        
      }

Read the Creating Interval Timers section of my Blynk timer tutorial that was linked earlier, to see why you shouldn’t be creating multiple timer objects.

ispumpon and sensorDataSend are the names of functions, but you have no functions with these names. Instead you have some code in your void setup which makes no sense.

I take it you haven’t bothered to look at the Sketch Builder examples

Pete.

Okay Pete,

I got it working. Only issue is that its not reading the digital pin. The timer runs constantly. I have the interval set for 1 second for testing in this code. I want the counter to stop and store that value when the pin is low. Ive tried switching out a=1 for a=high with no avail.

Anything you can see to improve both the code and functionality of the counter

//A0-AIR PRESSURE SENSOR
//A1-WATER PRESSURE SENSOR
//D2-DHT SENSOR 1
//D4-DHT SENSOR 2


//V1-
//V2-
//V3-
//V4-
//V5- HUMIDITY SENSOR 1
//V6- TEMPERATURE SENSOR 1
//V7- HUMIDITY SENSOR 2
//V8- TEMPERATURE SENSOR 2
//V9- PRESSURE AIR
//V10- PRESSURE WATER
//V11- OFFLINE LED
//V12- ONLINE LED
//V13- WATER METER
//V14-

#include <Arduino.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include "DHT.h"
#define BLYNK_PRINT Serial

BlynkTimer timer;

//Water Meter
int a, i=0;      // variable to store the read value


//Air Pressure Sensor
const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 150; //psi value of transducer being used
float pressureValue = 0;

//Water Pressure Sensor
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureZero1 = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax1 = 921.6; //analog reading of pressure transducer at 1000psi
const int pressuretransducermaxPSI1 = 1000; //psi value of transducer being used
float pressureValue1 = 0;

const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 1000; //constant integer to set the sensor read delay in milliseconds

char auth[] = "RVovq6azrEAqop3ahndbFr38DvVrhDtm";
char ssid[] = "cait&caiden";
char pass[] = "Kinghenry3";

#define DHT1_PIN 2     
#define DHT2_PIN 4     
#define DHTTYPE DHT21  

DHT dht1(DHT1_PIN, DHTTYPE);//for first DHT module
DHT dht2(DHT2_PIN, DHTTYPE);




void sensorDataSend()
{  
 pressureValue = analogRead(pressureInput);
 pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
  
 Blynk.virtualWrite(V9, pressureValue, 1);
 
 pressureValue1 = analogRead(pressureInput1);
 pressureValue1 = ((pressureValue1 - pressureZero1) * pressuretransducermaxPSI1) / (pressureMax1 - pressureZero1);
 
Blynk.virtualWrite(V10, pressureValue1, 1);

 float h = dht1.readHumidity();
 
Blynk.virtualWrite(V5, h);
 
 float t = dht1.readTemperature(true);

Blynk.virtualWrite(V6, t);
 
 float h1 = dht2.readHumidity();

Blynk.virtualWrite(V7, h1);
 
 float t1 = dht2.readTemperature(true);

Blynk.virtualWrite(V8, t1-2);
  

 
}
 
void isCatPumpOn(){
  
a = digitalRead(10); 

 if (a = HIGH) 
{   
  i=i+1;    
  
Serial.print(" Counter =");   
Serial.println(i); 
Blynk.virtualWrite(V13, i);
}
else { 
i = i;
Blynk.virtualWrite(V13, i);  
}}





void setup(){
 Serial.begin(9600);
 
 timer.setInterval(1000L, sensorDataSend); 
 timer.setInterval(1000L, isCatPumpOn); 
 dht1.begin();
 dht2.begin();
}


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


 if(Blynk.connected()){Blynk.run();
    Blynk.virtualWrite(V11, 255);
    
  } else Blynk.begin(auth, ssid, pass);
    Blynk.virtualWrite(V12, 0);
}

= needs to be ==
What is the purpose of i = i?

Didn’t mean to keep that in the code. Thanks for the catch.

You can’t have Blynk.virtualWrites in your void loop, you’ll flood the server.

Pete.

Hope this can help you :

What am I missing? I can’t figure out how to read virtual pins. I tried to set it up with a 1 second timer so that I had to press and hold the button for 1 second to reset. I have two variables I am trying to reset with pins V30 and V31. I cant get the Blynk_Write to work.


BLYNK_WRITE(V30);

  g = param.asInt(); /

 if( g == 1) 
 {
  Blynk.virtualWrite(V17, 0);
  
  } 
  else { 
Blynk.virtualWrite(V17, m);  
}


BLYNK_WRITE(V31);

  h = param.asInt(); /

 if( h == HIGH) 
 {
  Blynk.virtualWrite(V17, 0);
  
  } 
  else { 
Blynk.virtualWrite(V17, m);  
}}




void setup(){
 Serial.begin(9600);
 
 timer.setInterval(500L, sensorDataSend); 
 timer.setInterval(1000L, resetMeters); 









**I was able to get this to compile but it doesnt work either.** 


void resetMeters(){
  
 if( V30 == HIGH) 
 {
  Blynk.virtualWrite(V17, 0);
  } 
  else { 
Blynk.virtualWrite(V17, m);  
}


 if( V31 == HIGH) {
  Blynk.virtualWrite(V13, 0);
  } 
  else { 
  Blynk.virtualWrite(V13, i);  


}}






void setup(){
 Serial.begin(9600);
 
 timer.setInterval(500L, sensorDataSend); 
 timer.setInterval(1000L, resetMeters);

Read this :
https://docs.blynk.io/en/blynk.edgent/api/virtual-pins

It’s impossible to understand what it is that you are trying to achieve, based on this statement and your two snippets of code.
You refer to a timer, but there is no timer in these code snippets.

Pete.

Here is my full code.

Am I using the BLYNK_WRITE function wrong?





/

//A0-AIR PRESSURE SENSOR
//A1-WATER PRESSURE SENSOR
//D2-DHT SENSOR 1
//D4-DHT SENSOR 2


//V1-
//V2-
//V3-
//V4-
//V5- HUMIDITY SENSOR 1
//V6- TEMPERATURE SENSOR 1
//V7- HUMIDITY SENSOR 2
//V8- TEMPERATURE SENSOR 2
//V9- PRESSURE AIR
//V10- PRESSURE WATER
//V11- OFFLINE LED
//V12- ONLINE LED
//V13- WATER METER
//V14- WATER METER RESETTABLE
//V15- PUMP HOUR METER
//V16- AIR COMPRESSOR HOUR METER
//V17- RUN HOUR METER

#include <Arduino.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include "DHT.h"
#define BLYNK_PRINT Serial

BlynkTimer timer;

//Water Meter
int a, i=0;       //Permanent 
int b, j=0;       //Resettable
float c, k=0;     //Pump Hour Meter
float d, l=0;     //Air Compressor Hour Meter
float e, m=0;     //Run Hour Meter
float f, n=0;     //Run Hour Meter
float g;          //Reset Water Usage This run 
float h;          //Run Hour Meter This Run

//Air Pressure Sensor
const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 150; //psi value of transducer being used
float pressureValue = 0;

//Water Pressure Sensor
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureZero1 = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax1 = 921.6; //analog reading of pressure transducer at 1000psi
const int pressuretransducermaxPSI1 = 1000; //psi value of transducer being used
float pressureValue1 = 0;

const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 1000; //constant integer to set the sensor read delay in milliseconds

char auth[] = "RVovq6azrEAqop3ahndbFr38DvVrhDtm";
char ssid[] = "cait&caiden";
char pass[] = "Kinghenry3";

#define DHT1_PIN 2     
#define DHT2_PIN 4     
#define DHTTYPE DHT21  

DHT dht1(DHT1_PIN, DHTTYPE);//for first DHT module
DHT dht2(DHT2_PIN, DHTTYPE);




void sensorDataSend()
{  
 pressureValue = analogRead(pressureInput);
 pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
  
 Blynk.virtualWrite(V9, pressureValue, 1);
 
 pressureValue1 = analogRead(pressureInput1);
 pressureValue1 = ((pressureValue1 - pressureZero1) * pressuretransducermaxPSI1) / (pressureMax1 - pressureZero1);
 
Blynk.virtualWrite(V10, pressureValue1, 1);

 float h = dht1.readHumidity();
 
Blynk.virtualWrite(V5, h);
 
 float t = dht1.readTemperature(true);

Blynk.virtualWrite(V6, t);
 
 float h1 = dht2.readHumidity();

Blynk.virtualWrite(V7, h1);
 
 float t1 = dht2.readTemperature(true);

Blynk.virtualWrite(V8, t1-2);
  

 
}
 
void isCatPumpOn(){
a = digitalRead(10); 

 if (a == HIGH) 
{   
  i=i+1;    
Blynk.virtualWrite(V13, i);
}
else { 
Blynk.virtualWrite(V13, i);  
}}


void isCatPumpOn2(){
a = digitalRead(10); 

 if (a == HIGH) 
{   
  j=j+1;    
Blynk.virtualWrite(V14, j);
}
else { 
Blynk.virtualWrite(V14, j);  
}}



void pumpHourMeter(){
a = digitalRead(10); 

 if (a == HIGH) 
{   
  k=k+.016;    
Blynk.virtualWrite(V15, k);
}
else { 
Blynk.virtualWrite(V15, k);  
}}

void airHourMeter(){
d = digitalRead(9); 
 if (d == HIGH) 
{   
  l=l+.016;    
Blynk.virtualWrite(V16, l);

}
else { 
Blynk.virtualWrite(V16, l);  
}}

void runHourMeter(){
e = digitalRead(9); 
 if (e == HIGH) 
{   
  m=m+.016;    
Blynk.virtualWrite(V17, m);

}
else { 
Blynk.virtualWrite(V17, m);  
}}

void seasonHourMeter(){
f = digitalRead(9); 
 if (f == HIGH) 
{   
  n=n+.016;    
Blynk.virtualWrite(V18, n);

}
else { 
Blynk.virtualWrite(V18, n);  
}}

void resetMeters(){

BLYNK_WRITE(V30);

  g = param.asInt(); /

 if( g == 1) 
 {
  Blynk.virtualWrite(V17, 0);
  
  } 
  else { 
Blynk.virtualWrite(V17, m);  
}


BLYNK_WRITE(V31);

  h = param.asInt(); /

 if( h == HIGH) 
 {
  Blynk.virtualWrite(V17, 0);
  
  } 
  else { 
Blynk.virtualWrite(V17, m);  
}}

  






void setup(){
 Serial.begin(9600);
 
 timer.setInterval(500L, sensorDataSend); 
 timer.setInterval(1000L, resetMeters); 
 timer.setInterval(15000L, isCatPumpOn); 
 timer.setInterval(15000L, isCatPumpOn2); 
 timer.setInterval(60000L, pumpHourMeter); 
 timer.setInterval(60000L, airHourMeter); 
 timer.setInterval(60000L, runHourMeter); 
 timer.setInterval(60000L, seasonHourMeter); 
 dht1.begin();
 dht2.begin();
}


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


 if(Blynk.connected()){Blynk.run();
    
    
  } else Blynk.begin(auth, ssid, pass);
   
}

Yes.
BLYNK_WRITE(vPin) is a special callback function which triggers automatically when the value of the virtual pin changes because of input via the app or an HTTP(S) update.
You can’t put this function inside another function.

Maybe if you explained properly what it is you are trying to achieve, and added some in-code documentation to your sketch, we’d be able to understand your issue and advise on the best way to solve it.

Pete.

And that is exactly what I am trying to do. I am trying to reset a counter. This counter is based off a relay on a digital pin. When the relay is triggered via the app, it starts counting. As I explained in my first post, I have a pump that will always be putting out 4 gallons per minute. I have made counters for hour meters for the pump, an air compressor, as well as a season long hour meter.

I also have counters to count the amount of water used each time watering, as well as the amount of hours that the pump has been on. These counters are all blynk timers with different intervals.

My question is in regards to resetting the counters. My code references virtual pin 30 and 31. On the app, I have two buttons that are tied to V30 and V31. I want to be able to reset these two counters by pushing the button and holding for 1 second, so that I dont accidentally hit the button and reset it.

to the best of my understanding, this code should run the function every 1 second, and if it see the button is pushed (variable tied to virtual pin = 1,) then it will set the variable attached to the counter to go to 0 using an if function. If the button is not pushed, it says to write the variable that’s stored.

I have tried not putting this code inside my reset function but its not reading the button as high or low but rather just reading the number of the virtual pin. I set it up to just print to serial monitor what it was receiving from BLYNK_WRITE.

This is my updated code for just the reset. I removed the blynk timer. Is there a way that i could require a 2 second press of the button to reset?


BLYNK_WRITE(V30);

g = param.asInt(); /

if ( g == 1)
{
  Blynk.virtualWrite(V17, 0);

}
else {
  Blynk.virtualWrite(V17, m);
}


BLYNK_WRITE(V31);

h = param.asInt(); /

if ( h == HIGH)
{
  Blynk.virtualWrite(V17, 0);

}
else {
  Blynk.virtualWrite(V17, m);
}

Which code are you referring to?
If it’s the code in post #21 then, as I explained before, it doesn’t work because you can’t put a BLYNK_WRUTE(vPin) callback function inside another function.

Maybe you should take a look at topics like this one:

or this one:

Pete.

Deleted!

Pete.