BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

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

Hello,

I am looking to create a counter that counts water usage. I have a relay that is connected to digital pin 10 on my Arduino UNO wifi rev2. This relay works to complete a circuit on a contactor for a 220v motor and pump. This pump will always be putting out 4 gallons per minute. I want to start a counter whenever the button is pressed. I am using the legacy version of BLYNK. I want the counter to increase by 4 every minute while the pump is on. I thought the easiest way would be to tie the counter to the voltage of the digital pin, but I cannot make the pin both an input and output. I want to tie this to a Virtual Pin for the counter. Any help would be appreciated. If i can get this to work, id love to have one counter thats resettable and one that is just continious.

Thank you

Caiden

I would recommend a flow sensor for more accurate measurements.

Eventually that’s my goal but I’m not too worried about precise measurements. More looking for an approximate number.

I’d use a BlynkTimer that calls a function every 15 seconds. If the relay is active when the timer is called then the gallons counter is increased by 1.

Pete.

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