Go Down

Topic: Decrementing Volume (Read 499 times) previous topic - next topic


Here is my Public Service contribution - the OPs code from Reply #26

Code: [Select]
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// which pin to use for reading the sensor? can use any pin!

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
  uint8_t x = digitalRead(FLOWSENSORPIN);
  if (x == lastflowpinstate) {
    return; // nothing changed!
  if (x == HIGH) {
    //low to high transition!
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);

void setup() {
   Serial.print("Flow sensor test!");
   lcd.begin(16, 2);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);

void loop()                     // run over and over again
  lcd.setCursor(0, 0);
  lcd.print("Pulses:"); lcd.print(pulses, DEC);
  lcd.print(" Hz:");
  Serial.print("Freq: "); Serial.println(flowrate);
  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  // if a plastic sensor use the following calculation
  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  // Liters = Pulses / (7.5 * 60)
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;

  // if a brass sensor use the following calculation
  float liters = pulses;
  liters /= 8.1;
  liters -= 6;
  liters /= 60.0;
  Serial.print(liters); Serial.println(" Liters");
  lcd.setCursor(0, 1);
  lcd.print(liters); lcd.print(" Liters        ");

I can sort of understand why the OP is confused. Why on earth is there such complicated interrupt code when all that is needed is something like

Code: [Select]
void setup() {
  // other stuff
  attachInterrupt(0, pulseCount, RISING);


Code: [Select]
void pulseCount() {
   totalPulses ++;



Feb 05, 2015, 10:33 am Last Edit: Feb 05, 2015, 10:34 am by PeterFW
I can sort of understand why the OP is confused. Why on earth is there such complicated interrupt code when all that is needed is something like
Code: [Select]
void pulseCount() {
   totalPulses ++;

Indeed, a reliable pulse count through polling is a bit problematic when writing to a LCD and serial port.
The above mentioned solution is extremely simpel.
In fact, the geiger counter that sits on my workbench right now uses the excact same method and delivers reliable data.

Accumulate every count delivered by the sensor, determine a equivilant pulse/liter ratio and you are done.
But you should store the acumulated value in the EEPROM so the whole unit does not "forger" it when powered down or after a reset.

To get a pulse/liter ratio just set the counter to zero and draw one liter into a measuring cup.

pulsecount / calibrationvalue = liters
remaining = startvalue - liters
dislay remaining

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131