#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
********************************************************/
// select the pins used on the LCD panel
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
///LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
byte prev_screen;
byte prev_lcd_key;
byte sensorInterrupt = 8;
byte sensorPin = 9;
byte relayPin = 10;
float calibrationFactor = 4.5;
float flowRate = 0;
volatile byte pulseCount = 0;
unsigned long flowMilliLitres = 0;
unsigned long totalMilliLitresA = 0;
byte screen = 0;
int set_point = 0;
byte edit = 0;
unsigned long oldTime = 0;
byte key_input;
byte statusLed = 13;
int total_litres = 0;
byte display_run = 0;
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.clear();
lcd.setCursor(0, 0);
Serial.begin (9600);
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
pinMode(statusLed, OUTPUT);
pinMode(relayPin, OUTPUT);
lcd_key = read_LCD_buttons(); // read the buttons
if (prev_lcd_key != lcd_key) {
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
key_input = 2;
break;
}
case btnLEFT:
{
key_input = 1;
break;
}
case btnUP:
{
if (edit == 1)
{
set_point++;
}
break;
}
case btnDOWN:
{
if (edit == 1)
{
set_point--;
}
break;
}
case btnSELECT:
{
if (screen == 1) {
screen = 0;
edit = 0;
}
else
{ screen = 1;
edit = 1;
}
break;
}
case btnNONE:
{
key_input=0;
break;
}
}
}
prev_lcd_key = lcd_key;
if (set_point < 0) {
set_point = 1;
}
if (set_point > 135) {
set_point = 135;
}
if (key_input == 1) {
totalMilliLitresA = 0;
total_litres = 0;
display_run = 1;
digitalWrite(statusLed, HIGH);
digitalWrite(relayPin, LOW);
}
if (key_input == 2) {
display_run = 0;
digitalWrite(statusLed, LOW);
digitalWrite(relayPin, HIGH);
}
if (total_litres >= set_point) {
display_run = 0;
digitalWrite(statusLed, LOW);
digitalWrite(relayPin, HIGH);
}
if ((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitresA += flowMilliLitres;
if (display_run == 1) {
total_litres = totalMilliLitresA / 1000;
}
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
Serial.print ("display: ");
Serial.print (display_run);
Serial.print (" ml: ");
Serial.print (totalMilliLitresA);
Serial.print (" L ");
Serial.print (total_litres);
Serial.print (" setpoint ");
Serial.println (set_point);
if (prev_screen != screen) {
lcd.clear();
}
lcd.setCursor(0, 0);
switch (screen) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Flow: ");
if (flowRate < 10){
lcd.print(" ");}
lcd.print(flowRate, 2);
lcd.setCursor(12, 0);
lcd.print(total_litres);
lcd.print("L");
lcd.setCursor(3, 1);
lcd.print(set_point);
lcd.print(" L");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print(set_point);
lcd.print(" L");
lcd.setCursor(0, 1);
lcd.print("select to set");
break;
}
prev_screen = screen;
}
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}