#define IRQ_A 0
#define IRQ_B 1
#define FlowA 2
#define FlowB 3
volatile unsigned long countIN = 0;
volatile unsigned long countOUT = 0;
unsigned long oldTime = 0;
float flowRate;
volatile byte pulseCountIN;
volatile byte pulseCountOUT;
volatile byte pulseCount;
float liters;
void setup()
{
Serial.begin(57600);
pulseCountIN = 0;
pulseCountOUT = 0;
pulseCount = 0;
flowRate = 0.0;
pinMode(FlowA, INPUT);
pinMode(FlowB, INPUT);
digitalWrite(FlowA, HIGH);
digitalWrite(FlowB, HIGH);
liters = 0.0;
attachInterrupt(IRQ_A, CounterIN, FALLING);
attachInterrupt(IRQ_B, CounterOUT, FALLING);
}
void loop()
{
unsigned long now = millis();
if((now - oldTime > 1000))
{
unsigned long duration = now - oldTime;
oldTime = now;
liters = (countIN - countOUT);
float litersPerMinute = (1000 * liters)/duration;
Serial.println(litersPerMinute, 3);
int LPM = litersPerMinute;
pulseCount = pulseCountIN - pulseCountOUT;
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount);
unsigned int frac = (flowRate - int(flowRate))*10;
Serial.print("pulseCount: ");
Serial.print(pulseCount, DEC);
Serial.println(" ");
pulseCountIN = 0;
pulseCountOUT = 0;
attachInterrupt(IRQ_A, CounterIN, FALLING);
attachInterrupt(IRQ_B, CounterOUT, FALLING);
}
}
void CounterIN()
{
countIN++;
pulseCountIN++;
}
void CounterOUT()
{
countOUT++;
pulseCountOUT++;
}
This is my last version of the sketch, and now it works! Thanks so much for your help I will keep developing this sketch to work more properly. I added two more volatile bytes, because the first sketch did not work.