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