Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte sensorInterrupt = 1;
- byte sensorPin = 1;
- float calibrationFactor = 4.5; // According to docs: the sensor outputs 4.5 pulses/sec per L/min of flow
- volatile byte pulseCount;
- float flowRate;
- unsigned int flowMilliLitres;
- unsigned long totalMilliLitres;
- unsigned long oldTime;
- void setup()
- {
- Serial.begin(9600);
- pinMode(sensorPin, INPUT);
- digitalWrite(sensorPin, HIGH);
- pulseCount = 0;
- flowRate = 0.0;
- flowMilliLitres = 0;
- totalMilliLitres = 0;
- oldTime = 0;
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- }
- void loop()
- {
- if((millis() - oldTime) > 1000) // one counter persecond
- {
- detachInterrupt(sensorInterrupt);
- flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
- oldTime = millis();
- flowMilliLitres = (flowRate / 60) * 1000;
- totalMilliLitres += flowMilliLitres;
- unsigned int frac;
- Serial.print("Flow rate: ");
- Serial.print((flowRate));
- Serial.print("L/min");
- Serial.print("\t");
- Serial.print("Output Liquid Quantity: ");
- Serial.print(totalMilliLitres);
- Serial.println("mL");
- Serial.print("\t");
- Serial.print(totalMilliLitres/1000);
- Serial.print("L");
- pulseCount = 0;
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- }
- }
- void pulseCounter()
- {
- pulseCount++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement