Subversion Repositories Arduino.arduino

Rev

Rev 37 | Blame | Compare with Previous | Last modification | View Log | Download

#include <TimedAction.h>
void ta_FlowSensor();
TimedAction readFlow = TimedAction(2000,ta_FlowSensor);

/*
 Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

 Measure the liquid/water flow rate using this code. 
 Connect Vcc and Gnd of sensor to arduino, and the 
 signal line to arduino digital pin 2.
*/

struct flow_sensor {
  byte sensorPin       = 5;

  volatile byte pulseCount = 0;  
  // The hall-effect flow sensor outputs approximately 4.5 pulses per second per
  // litre/minute of flow.
  //float calibrationFactor = 5.5; //plastic
  float calibrationFactor = 11.0;  //copper

  float flowRate = 0.0;

  float oneLitreTotal = 0.0;
  unsigned long totalLitres = 0;

  unsigned long oldTime = 0;
}; //flow_sensor

struct flow_sensor flow;

void setup()
{
  
  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  pinMode(flow.sensorPin, INPUT);
  digitalWrite(flow.sensorPin, HIGH);
  attachInterrupt(digitalPinToInterrupt(flow.sensorPin), pulseCounter, FALLING);
}

/**
 * Main program loop
 */
void loop()
{

  readFlow.check();
}

void ta_FlowSensor() {

    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(digitalPinToInterrupt(flow.sensorPin));
        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    unsigned long get_duration = millis() - flow.oldTime;
    flow.flowRate = (((1000.0 / get_duration) * flow.pulseCount) / flow.calibrationFactor) * 60.0;

    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    //flow.flowMilliLitres = (unsigned int)((flow.flowRate * 1000.0) / 3600.0);
    
    // Add the millilitres passed in this second to the cumulative total
    flow.oneLitreTotal += ((flow.flowRate * (float)get_duration)/36.0E+5);
    if (flow.oneLitreTotal > 1.0) {

      unsigned long ul_temp = (unsigned long)flow.oneLitreTotal;
      flow.totalLitres += ul_temp;
      flow.oneLitreTotal -= (float)flow.oneLitreTotal;
    }
        
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(flow.flowRate);  // Print the integer part of the variableflowRate
    Serial.print("L/hour");
    Serial.print("\t");       // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");        
    Serial.print(flow.totalLitres);
    //Serial.print("mL"); 
    //Serial.print("\t");       // Print tab space
    //Serial.print(flow.totalMilliLitres/1000);
    Serial.println("L");
    
    // Reset the pulse counter so we can start incrementing again
    flow.pulseCount = 0;
    
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    flow.oldTime = millis();
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(digitalPinToInterrupt(flow.sensorPin), pulseCounter, FALLING);

}

/*
Insterrupt Service Routine
 */
void pulseCounter()
{
  // Increment the pulse counter
  flow.pulseCount++;
}