Hi, you didn't follow any of suggestion provided by @rickkas7 and @ScruffR except one:
Also you publish rate is 2 per sec you have to wait at least 2 sec for recover here is a nice explanation but with this:
you don't need to care about Publish rate ( all is done at once )
try this code (not tested):
#include "Particle.h"
#define statusLed D7
#define sensorInterrupt D2 // 0 = digital pin 2
/*byte sensorPin = D2; why why why ??? */
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per litre/minute of flow.
volatile byte pulseCount = 0;
float calibrationFactor = 4.5;
float flowRate = 0.0;
unsigned int flowMilliLitres = 0;
unsigned long totalMilliLitres = 0;
unsigned long oldTime = 0;
void setup()
{
//Initialize a serial connection for reporting values to the host
Serial.begin(38400);
//Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // We have an active-low LED attached
//Set up internal pullup on D2
pinMode(sensorInterrupt, INPUT_PULLUP);
/*digitalWrite(sensorPin, HIGH); why why why ??? old Arduino style*/
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
pulseCount = 0;
Serial.printlnf("Flow rate: %6.1f l/min \r\n"
"Current Liquid Flowing: %6d ml/s \r\n"
"Output Liquid Quantity: %6d ml"
, flowRate
, flowMilliLitres
, totalMilliLitres
);
char msg[32];
snprintf(msg, sizeof(msg), "%d ml/s (%d ml total)", flowMilliLitres, totalMilliLitres);
Particle.publish("FlowRate", msg, PRIVATE);
}
}
void pulseCounter()
{
pulseCount++;
}