Hey folks, a little help can go a long way. Im missing something in this sketch, and Im sure its a simple thing. when I don't have anything in the sketch for the LCD, everything works great (serial print), once I add the LCD code, the serial print wont work in the loop section, as well the flow rate wont show up on the LCD either. I have attached the code below. any help is greatly appreciated.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int flowPin = 2; //signal from flow sensor
int resetButton = 3; // reset button
int powerLED = 5; //pump on LED
int powerButton = 4; //Pump power button
int pump = 6; //signal to TIP31 for bilge pump
LiquidCrystal_I2C lcd(0x27,16,2);
double flowRate;
volatile int count;
void setup()
{
pinMode(flowPin, INPUT);
pinMode(resetButton, INPUT);
pinMode(powerLED, OUTPUT);
pinMode(powerButton, INPUT);
pinMode(pump, OUTPUT);
attachInterrupt(0, Flow, RISING);
attachInterrupt(1, Reset, RISING);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print ("FLOW METER V1.0");
lcd.setCursor(0,1);
lcd.print ("FLOW: L/m");
Serial.begin(9600);
Serial.println ("FLOW METER V1.0 BY RICHARD LUXON");
}
void loop()
{
count = 0;
interrupts();
delay (250);
noInterrupts();
flowRate = (count * 5.5);
flowRate = flowRate * 60;
flowRate = flowRate / 1000;
lcd.print(flowRate);
Serial.print("FLOW RATE: ");
Serial.print(flowRate);
Serial.println("L/m");
}
void Flow()
{
count++;
}
void Reset()
{
count==0;
}
you do not call lcd.setCursor() when updating the LCD
you use RISING interrupt.
Comments:
the HD44780 library works well and is supported by the Library Manager.
neither Serial nor LCD will be able to print without interrupts.
You can disable interrupts when you want to make a copy of the count value.
Perform calculations on this copy value.
You only need to re-write a few characters. e.g. perhaps with a trailing space.
If your hardware is connected to the INTn pins, the edge polarity does not matter.
If you use INPUT_PULLUP with FALLING edge, you get a safer electrical arrangement.
However obscure your LiquidCrystal_I2C library is, I would expect it to print a float with two decimal places. Because it should inherit the Print class.
void loop()
{
noInterrupts();
count = 0; //reset
interrupts();
delay (250); //count pulses for 250ms
noInterrupts();
int copy = count; //read current value
interrupts();
flowRate = (copy * 5.5); //use the copy value
flowRate = flowRate * 60;
flowRate = flowRate / 1000;
lcd.setCursor(8, 1); //.kbv
lcd.print(flowRate);
Serial.print("FLOW RATE: ");
Serial.print(flowRate);
Serial.println("L/m");
}
David.
p.s. formatted print is always difficult on an Arduino. It is worth using dtostrf() to provide padding and precision. Then you never get trailing garbage or moving decimal point.