PROBLEM:Flow sensor reading delayed on LCD,only displays reading once Flow stops

Hi Guys

With your help and assistance last night, I managed to get the readings of my sensors showing where I want them to be on my LCD display.

However, for some reason the FLow sensor reading only comes through after a 4 second delay, and also only sends the data once the flow sensor has stopped?

Thus, I’m not getting a continues output(of total MilliLitres) from the flow sensor

Please see the code I’m uploading…

The Code to run the DHT11, Thermistor, FlowSensor on a LCD Shield.

#include <LiquidCrystal.h>
#include <DHT.h>

#define DHTPIN 53   //pin wat ons gebruik
#define DHTTYPE DHT11

DHT dht (DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte statusLed    = 13;

byte sensorInterrupt = 3;  // 3 = digital pin 20
byte sensorPin       = 20;

float calibrationFactor = 4.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
{
  lcd.begin(16, 2);
  lcd.print("Graspan 2");
  dht.begin();
  }
double Thermister(int RawADC) {
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;           
  return Temp;
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop()
{
  float temperature, humidity;

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  delay(2000); 

 lcd.setCursor(0,0);

 char tempF[6]; 
 char humF[6];
 dtostrf(temperature, 5, 1, tempF);
 dtostrf(humidity, 2, 0, humF);

 lcd.print("T:"); 
 lcd.print(tempF);
 lcd.print((char)223);
 lcd.print("C ");
 lcd.print("H: ");
 lcd.print(humF);
 lcd.print("%");
 delay(1000);
 
   double fTemp;
  double temp = Thermister(analogRead(10));

  lcd.setCursor(0,1);
  lcd.print("T");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C ");
  lcd.setCursor(10,1);
    lcd.print(totalMilliLitres);
    lcd.print("mL");
  
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
        
    // 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.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    
    // 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.
    oldTime = millis();
    
    // 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.
    flowMilliLitres = (flowRate / 60) * 1000;
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
      
    unsigned int frac; 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
    delay(1000);
}

void pulseCounter()
{
  pulseCount++;
}

Any advice on how to rectify this?

This is the WORKING code for just the
Thermister, FlowSensor on a LCD shield

#include <LiquidCrystal.h>


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte statusLed    = 13;

byte sensorInterrupt = 3;  // 
byte sensorPin       = 20;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
{
  
  // Initialize a serial connection for reporting values to the host
  lcd.begin(16, 2);
  }
double Thermister(int RawADC) {
  double Temp;
  // See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;           // Convert Kelvin to Celcius
  return Temp;

   
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 20 which uses interrupt 3.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**
 * Main program loop
 */
void loop()
{
   double fTemp;
  double temp = Thermister(analogRead(10));  
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("T2 ");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C ");
  lcd.setCursor(0,0);
    lcd.print(totalMilliLitres);
    lcd.print("mL");
  
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
        
    // 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.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    
    // 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.
    oldTime = millis();
    
    // 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.
    flowMilliLitres = (flowRate / 60) * 1000;
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
      
    unsigned int frac; 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
    delay(1000);
}

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

As always, much appreciated.

Regards
Paul

I have not read everything but there is at least a couple pb here:

1/ dealing with Interrupts

byte sensorInterrupt = 3;  // 3 = digital pin 20
...
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
..
detachInterrupt(sensorInterrupt);

The first parameter to attachInterrupt() is an interrupt number. so it is 0 for interrupts on pin 2 or 1 for interrupts on pin 3 (32u4 based such as Leonardo, Micro or Due and Zero have more interrupts)

--> You should use digitalPinToInterrupt(sensorInterrupt) to translate the actual digital pin to the specific interrupt number.

So your code should be

byte sensorInterrupt = 3;  // 3 = digital pin 20
...
attachInterrupt(digitalPinToInterrupt(sensorInterrupt), pulseCounter, FALLING);
..
detachInterrupt(digitalPinToInterrupt(sensorInterrupt));

of course I trust that you did wire correctly to pin 3 and that whatever sensor you use sends a HIGH TO LOW pulse at each time.

2/ adding un-necessary delays

you have a delay(1000); in your code at the end of the loop which looks like this if I simplify

void loop()
{
  if ((millis() - oldTime) > 1000)   // Only process counters once per second
  {
     ...
   }
  delay(1000);
}

You are handling time already with your if statement, so don't slow your code. get rid of that delay.

3/ Same happens at the begining of the loop

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
[color=red]  delay(2000);[/color]

  lcd.setCursor(0, 0);

  char tempF[6];
  char humF[6];
  dtostrf(temperature, 5, 1, tempF);
  dtostrf(humidity, 2, 0, humF);

  lcd.print("T:");
  lcd.print(tempF);
  lcd.print((char)223);
  lcd.print("C ");
  lcd.print("H: ");
  lcd.print(humF);
  lcd.print("%");
[color=red]  delay(1000);[/color]

here you go with an additional 3 second lost for nothing. get rid of those.

if you don't feel like updating the T° reading at every loop, move that into the 1 second timeout loop you have below and then your readings/display will update every second

and you can change that update rate with whatever you put in the if statement

  if ((millis() - oldTime) > [b][color=blue]1000[/color][/b])   // Only process counters once per second

(that 1000 should be defined as a global constant at the beginning of your program rather than hardwired into the code. Makes it for easier maintenance of your code)

if you want different update times for the flow and the T°, then implement the same strategy as with the flow, have a

if (millis() - oldTimeForTemperature >  [b][color=blue]TEMPERATURE_UPDATE_DELAY[/color][/b]) { to englobe the first part and manage oldTimeForTemperature the same way you managed oldTime

Thanks J-M-L, you're a legend!

The delay went away upon removing unnecessary code as you advised, and thank you for the in-depth explanation on the interrupts, I really appreciate it.

Have an awesome day!