Water flow Sensor

Hi Can any one please help me . I have tried to get this project to work but failed. I loaded a sketch from http://www.adafruit.com/products/828, the code works perfectly well except when i switch the unit off it looses the total litres flowed. i tried to write to EEPROM & READ but does not work.

Can anyone help or point me to example sketches than i can play with

Thanking you in advance Ivan

Give us your code so we can see why the save does not work.

Remember the EEPROM only has a "limited" number of read/writes so if you save too often, you will wear it out.

Weedpharma

[tt]Hi,thank you[/tt]

flow_sensor_trial.ino (2.88 KB)

EEPROM.read is a function which returns a byte,

use it like this:

val = EEPROM.read(addr);

Thank you Martin, I tried it the way you suggested but as being faily new to arduino it does not work,
I sure i am doing some thing wrong. I attached the new code. IF you would be so kind to take a look and point me in the right direction it would be highly app.
Thank you

flowsensortest2.ino (2.52 KB)

#include "LiquidCrystal.h"
LiquidCrystal lcd (12, 11, 5, 4, 3, 7);

#include <EEPROM.h>
int addr = 0;

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2

// 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!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  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.begin(9600);
   Serial.print("Flow sensor test!");
   lcd.begin(20, 4);
   
   pinMode(FLOWSENSORPIN, INPUT);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);
   useInterrupt(true);
}

void loop()                     // run over and over again
{ 
  lcd.setCursor(0, 0);
  lcd.print("Pulses:"); lcd.print(pulses, DEC);
  lcd.print(" Hz:");
  lcd.print(flowrate);
  //lcd.print(flowrate);
  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 /= 80;
  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        ");
  int val = liters +(addr, val);
   EEPROM.write(addr, val);
  delay(100);
  
val = EEPROM.read(addr);
 lcd.setCursor(10, 3); 
 lcd.print(val); 
 
  delay(100);
}

I don’t have the IDE or a compiler on this PC, but the obvious problem is here:

  int val = liters +(addr, val);

I assume you were wanting to read the EEPROM value and add the current flow to it.
I’m not going to correct your code because it will wear out the EEPROM in a matter of hours. You need to be looking at a state machine and devise a method of storing the current value just occasionally.

If your power-down events are planned, add a button to store the current value and read it back in the setup() function when the Arduino powers up.

Hi Martin Thank you for your advice, Basically what i am trying to do is count the total litres that has passed the flow sensor, but every time i switch off the flow count resets to 0. Actually what i need is something like a rolling odometer.or can you suggest something else Thank you Ivan

The simplest solution is to add a button to the circuit, which needs to be pressed before power-down. The button should trigger an action which stores the current flow into EEPROM. Setup() will need to read the current flow from EEPROM. The float in which the flow is stored will need to be a global variable (defined before setup).

Suedo-code below, in which I've not taken any steps to control the sampling interval (blink without delay is a good example), nor de-bounce the button.

float totalFlow;

void setup()
{
  totalFlow = readFloatFromEEP(addr);
}

void loop()
{
  totalFlow += calcCurrentFlow();
  showFlowLCD();  // LCD Stuff to show totalFlow

  if (digitalRead(buttonPin) == HIGH) {
    writeFloatToEEP(addr,totalFlow);
    showShutdownMessage();  // "OK to shut-down"
  }
}

Option 2 would be to add a battery-backed RTC chip which has some RAM, such as the Microchip MCP7940N or Dallas DS1307.

Hi Martin thank you, i have some Ds1307 lying around im going to try and use this and see if i can come right, once again thank you for your input. Regards Ivan

Please help me sir i was make flow rate project. how can write or add eeprom program ? bcz power off time flow totalizer starting 0 so plz help me.

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int X;
int Y;
float TIME = 0;
float FREQUENCY = 0;
float WATER = 0;
float TOTAL = 0;
float LS = 0;
const int input = A0;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Water Flow Meter”);
lcd.setCursor(0,1);
lcd.print(“J.D.CORPORATION”);
delay(3000);
pinMode(input,INPUT);
}
void loop()
{
X = pulseIn(input, HIGH);
Y = pulseIn(input, LOW);
TIME = X + Y;
FREQUENCY = 1000000/TIME;
WATER = FREQUENCY/7.5;
LS = WATER/60;
if(FREQUENCY >= 0)
{
if(isinf(FREQUENCY))
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“VOL. :0.00”);
lcd.setCursor(0,1);
lcd.print(“TOTAL:”);
lcd.print( TOTAL);
lcd.print(" L");
}
else
{
TOTAL = TOTAL + LS;
Serial.println(FREQUENCY);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“VOL.: “);
lcd.print(WATER);
lcd.print(” L/M”);
lcd.setCursor(0,1);
lcd.print(“TOTAL:”);
lcd.print( TOTAL);
lcd.print(" L");
}
}
delay(1000);
}

http://www.freetronics.com.au/pages/jaycar-water-flow-gauge