Thanks. I have cleaned up a bit more here, but its not perfect yet. The whole system is not accurate enough. I need the sensors to be very accurate for this project, and I think it is something inside here who makes the difference:
#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 float pulseCountIN;
volatile float pulseCountOUT;
volatile float pulseCount;
float liters;
void setup()
{
Serial.begin(57600);
pulseCountIN = 0.0;
pulseCountOUT = 0.0;
pulseCount = 0.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;
int LPM = litersPerMinute;
pulseCount = (pulseCountIN - pulseCountOUT) / 10000.0 * 3600;
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount);
unsigned int frac;
Serial.print("Liter per time: ");
Serial.println(pulseCount, 4);
pulseCountIN = 0;
pulseCountOUT = 0;
}
}
void CounterIN()
{
countIN++;
pulseCountIN++;
}
void CounterOUT()
{
countOUT++;
pulseCountOUT++;
}
When I connect the hoses and sensors to the water faucet, the sensors gives me this: results
Liter per time: 23.4000
Liter per time: 24.1200
Liter per time: 24.8400
Liter per time: 25.2000
Liter per time: 25.5600
Liter per time: 24.8400
...and that is not what I had expected.
The engines I develop this flow meter to, uses between 3 and 50 liters per hour.
Do you understand what I have done wrong?
Cheers, Fredrik