I’m having trouble trying to figure out how to measure and accumulate more than 1 pulse input using 2 interrupts. I have 2 pulse flow meters with different pulses per gallons. I can measure each one separately fine.
The interrupt works fine one at a time, but I can not get a second to work. I know it’s not reading or accumulating anything on the second input as it writes to the EEPROM and the value is still the default 4294967295.
I’m guessing the issue related to the timing of the interrupts, I played with priority with no luck.
I’m just pulling water meter only relevant parts of a bigger code section.
I’m new to coding, need some expert advice on how to fix this! Thank You in advanced
#define waterPin1 D5 //Pulse Meter #1 //Works fine
#define waterPin2 D6 //Pulse Meter #2 //Not working with D5
//The Gals Per Pulse are different value in real life, maybe not the debounce not, set up just in case
double GPP1 = 1; // Volume Per Pulse - volume Gals Per Pulse
double GPP2 = 1; // Volume Per Pulse - volume Gals Per Pulse
#define DEBOUNCE_1 50 // (ms) Meter 1 Debounce (Reed Switch),
#define DEBOUNCE_2 50 // (ms) Meter 1 Debounce (Reed Switch),
int interval = 2; // (min) Publish Interval in MINUTES
unsigned long eepromInterval = 12* 60 * 60000; // 8 * (60 * 60000) = (8hours) How often to Save Totalizer Values to EEPROM milliseconds
float FlowRate_1 = 0 ; // Total Pulse over sample interval time x GPP
float FlowRate_2 = 0 ; // Total Pulse over sample interval time x GPP
unsigned long int accumPulseCt_1 = 0;
unsigned long int TGals_1 = 0;
unsigned long int accumPulseCt_2 = 0;
unsigned long int TGals_2 = 0;
volatile int PulsesPerInterval_1 = 0 ; // Flushed after every Interval / Publish
volatile int PulsesPerInterval_2 = 0 ; // Flushed after every Interval / Publish
void WaterPulseCounter(void) // Water Sensor interrupts
{
accumPulseCt_1++; //Total Pulses
TGals_1++; //Total Gallons
accumPulseCt_2++;
TGals_2++;
}
//AREA I NEED HELP They don't play nice.
void setup()
{
pinMode(waterPin1, INPUT);
pinMode(waterPin2, INPUT);
attachInterrupt(waterPin1, waterInterrupt1, FALLING); //PriorityDefault 13 ????
attachInterrupt(waterPin2, waterInterrupt1, FALLING, 12); //Priority????
}
void loop()
{ if (millis() - previousPub_1 >= (interval * 60000) )
{
// It's time to Publish the data,
FlowRate_1 = FlowRate_1 + (PulsesPerInterval_1 * GPP1) ;
TGals_1 += FlowRate_1;
accumPulseCt_1 += PulsesPerInterval_1;
FlowRate_2 = FlowRate_2 + (PulsesPerInterval_2 * GPP2) ;
TGals_2 += FlowRate_2;
accumPulseCt_2 += PulsesPerInterval_2;
PubDebugFlow(); //publishDataToCloud for debug
FlowRate_1 = 0; // Flushed after every Interval / Publish, since the Interval has ellapsed.
PulsesPerInterval_1 = 0 ; // Flushed after every Interval / Publish...
FlowRate_2= 0; // Flushed after every Interval / Publish...
PulsesPerInterval_2 = 0 ; // Flushed after every Interval / Publish...
previousPub_1 = millis(); //required
newDataP_1 = false; // Turn Off the Publish Flag, since we just pubilished.
if (millis() - previousEEPROM >= eepromInterval )
{ // Write to EEPROM every HOUR, Day, etc. cannot make the same at the publish interval
EEPROM.put(0, TGals_1);
EEPROM.put(20, TGals_2);
previousEEPROM = millis();
}
}
void waterInterrupt1()
{ // Interrupt for Water Meter 1
if (millis() - LastWaterIntTime1 >= DEBOUNCE_1 )
{
PulsesPerInterval_1++; // Tracks the # of Pulses from Meter for this time interval ONLY
newDataP_1 = true; // Set Global Flag because interrupt has fired
LastWaterIntTime1 = millis();
}
}
//Help! Tried compairing to LastWaterIntTime1 to fire after interrupt #1
void waterInterrupt2()
{ // Interrupt for Water Meter 2
if (LastWaterIntTime1 - LastWaterIntTime2 >= DEBOUNCE_2 )
//millis() was not working at LastWaerIntTime1 above
{
PulsesPerInterval_2++; // Tracks the # of Pulses from Meter for this time interval ONLY
newDataP_1 = true; // Set Global Flag because interrupt has fired
LastWaterIntTime2 = millis();
}
}