Trying to count an event

I’ve been working on this for awhile, I’m down to the last part. I want to count how many times an event happens. I’ve looked all over for an example, and haven’t really seen one.
I’ve copied and pasted a few things together, that should work, but I am getting an error when trying to load it, seems it’s a missing element in the programing.

//working on chip 1    ,all 4- DS18b20's and the DTH11 and A0 voltage sensor reading correct
// *****************    Trying to add counter on pin D5 and V11     *********************
// pin D2 is connented to ds1820's
// pin D7 is DTH11
// Pin D6 is Led Out Horn Button
// A0 Pin is battery voltage pin
// Pin D5 is Pump Counter

// V1 = Bilge Water Temp
// V2 = Port Side Temp
// V3 = Starboard Temp
// V4 = Engine Room Temp
// V5 =  
// V6 = Humidity
// V7 = Inside temp
// V13 = Adjusted Battery Voltage 
// V11 = Counter

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <DHT.h>
#include <OneWire.h>
#include <OneWire.h>
#include <DallasTemperature.h> 

SimpleTimer timer;

#define DHTPIN 13
#define b1pin 15
#define DHTTYPE DHT11
#define VT_PIN A0 
#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Probe01 = { 0x28, 0xff, 0x40, 0x93, 0x3c, 0x04, 0x00, 0x96 }; //S1
DeviceAddress Probe02 = { 0x28, 0xff, 0x82, 0x93, 0x3c, 0x04, 0x00, 0x37 }; //S2
DeviceAddress Probe03 = { 0x28, 0xff, 0xe6, 0x70, 0x3b, 0x04, 0x00, 0x82 }; //S3
DeviceAddress Probe04 = { 0x28, 0xff, 0x8d, 0x51, 0x3a, 0x04, 0x00, 0xb7 }; //S4
//DeviceAddress Probe05 = { 0x28, 0xAB, 0x58, 0x2D, 0x17, 0x13, 0x01, 0x2B }; //S5

boolean isButtonPressed1 = false;

int COUNT =0;

//Blynk token

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";  // boat2

// WiFi credentials.
//char ssid[] = "lvl1";
//char pass[] = "xxxxxxxxxxx";

char ssid[] = "lockadoc";
char pass[] = "xxxxxxxxxxx";

//char ssid[] = "LOCKMASTER";
//char pass[] = "xxxxxxxxxxxxxx";

//DHT Sensor
void sendSensor()
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);

  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V7, t);

  int Value = param.asInt(); // assigning incoming value from pin V11 to a variable
  if (Value=1) 
  isButtonPressed1 = true;  
  COUNT++;  //the COUNT increases when D5 is grounded


 attachInterrupt(D5, handleKey1, FALLING);


// temp sensor 
void getTempData()
  Blynk.virtualWrite(V1, sensors.getTempFByIndex(0));
  Blynk.virtualWrite(V2, sensors.getTempFByIndex(1));
  Blynk.virtualWrite(V3, sensors.getTempFByIndex(2));
  Blynk.virtualWrite(V4, sensors.getTempFByIndex(3));
  Blynk.virtualWrite(V8, sensors.getTempFByIndex(4));
// Voltage Sensor
//void getVoltage() 


// this  is where the missing element is----------------------------------------------
int vt_read = analogRead(VT_PIN);


 // Blynk.virtualWrite(V10, voltage);
 // Blynk.virtualWrite(V11, voltage2);
 // Blynk.virtualWrite(V12, voltage3);
 // Blynk.virtualWrite(V13, vt_read / 65.3);

void setup()
  Blynk.begin(auth, ssid, pass);

//DHT Sensor
//watertemp sensor

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1500L, getTempData);
  timer.setInterval(2000L, getVoltage); // no need to add another of these!
  timer.setInterval(500L, sendUptime);

void sendUptime(){
  //send to the smartphone the value COUNT 
   Blynk.virtualWrite(11, " count= ");
   Blynk.virtualWrite(11, COUNT); 

void loop() {;;

This is out of place, it should be inside the BLYNK_WRITE (V11).

Same with this, should be inside your setup.

Probably this too.

You also need to create an interrupt routine, and probably some other stuff too.

Basically your code needs some work/cleaning up.

Should but don’t. There is more to copy and paste programming (one of my preferred methods) then just blindly copying and pasting. You need to have a basic understanding of what is going on with the code and the functions it uses.

1 Like

I agree with everything @Toro_Blanco said… and wish to point out the apparent fallacy of this statement :stuck_out_tongue_winking_eye:

As you have already used… (Yes, I didn’t see that until after posting… :blush: ) … a simple…

void randomLoop() {
eventCount++; // increments by one each time this command happens
Blynk.virtualWrite(vPin, eventCount); // if not to frequent as to cause Server disconnection

…each time something (a function or whatever) happens is a very prevalent example of counting.

I have used this simple counting method many times as a diagnostic, let alone for other needed purposes.

Even works in really fast situations, with a little timer coding… say, counting how frequently my void loop() cycles under varying loads, without overloading a virtual write command. (this has been seen and commented on in some of my examples posted here).

int CNTR = 0;

void setup() {
timer.setInterval(1000L, CNTRdisplay);  // Update Loop counter Widget every second

void loop() {;;

//===== Loop Counter =====
void CNTRdisplay() {
  Blynk.virtualWrite(vPin, CNTR);  // Displays loop counts per second.
  CNTR = 0; // Resets counter

BTW, all these two commands do is overwrite each other in your Display Widget. Combine them into a single write command.

void sendUptime() { //send to the smartphone the value COUNT 
Blynk.virtualWrite(V11, "Count = ", displayCount);

Or use a Labeled Value Widget with appropriate LABEL and just send the count

void sendUptime() { //send to the smartphone the value COUNT 
Blynk.virtualWrite(V11, displayCount);


Thanks for your inputs, got it working awhile back.
working on the debounce on the input.
The more I play with Blynk the more I’m liking it.