Hall Effect sensor- Help

Hi All
Being very new at this I am hoping someone can help me with my project. I have a water flow sensor (hall) and I am using it to switch a gas solenoid when water is flowing through. I dont need to calculate flow rate but simply on and off when water is flowing. I have used the following which works, but sometimes the hall sensor stops on HIGH and stays high so the output remains high despite no water flowing through. Please can someone suggest a way around this?

// constants won't change. They're used here to set pin numbers:
const int hallPin = 12; // the number of the hall effect sensor pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int hallState = 0; // variable for reading the hall sensor status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the hall effect sensor pin as an input:
pinMode(hallPin, INPUT);
}

void loop(){
// read the state of the hall effect sensor:
hallState = digitalRead(hallPin);

if (hallState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Flow is what you actually need, so it might be safer to calculate the flow and apply a condition.

If flow > 2 lpm then operate solenoid.

Thanks Nick.

I guess this would work?

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor

void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the

hall effect sensors signal
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is

initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()
{

The code looks incomplete and without purpose.

Try something like this, with the control for the gas in the If section. It is just the Jaycar water flow programme with almost everything stripped out.

On reflection, the condition may have to be a bit more sophisticated, whereby, if the solenoid goes on, it is locked on until the flow stops. Otherwise it might chatter. There are surely some solenoid experts around here…

/**
 * Water Flow Gauge FOSTERS 20X4
 *
 * Uses a hall-effect flow sensor to measure the rate of water flow 
 * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
 * Copyright 2009 Hugh Blemings <hugh@blemings.org>
 *
 */

byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte sensorPin       = 2; //

float calibrationFactor = 6.71;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

void setup()
{    
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount          = 0;
  flowRate            = 0.0;
  oldTime             = 0;

  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop()
{
  if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
   
    detachInterrupt(sensorInterrupt);

    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    oldTime = millis();

if (flowRate>2)
{
//YOUR STUFF HERE
}
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}