Kegerator Flow Meter Test

The first step along the road to getting the kegerator monitor up and running

Flow meter used available from adafruit: http://www.adafruit.com/products/828

Based on the code from: https://github.com/adafruit/Adafruit-Flow-Meter/blob/master/Adafruit_FlowMeter.pde

Source:

#include

#define lcdPin 6

SoftwareSerial LCD = SoftwareSerial(0, lcdPin);
// since the LCD does not send data back to the Arduino, we should only define the txPin
const int LCDdelay = 10; // conservative, 2 actually works

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPINLEFT 2
#define FLOWSENSORPINRIGHT 3

// count how many pulses!
volatile uint16_t pulsesLeft = 0;
volatile uint16_t pulsesRight = 0;

volatile uint16_t lastPulses = 0;

// track the state of the pulse pin
volatile uint8_t lastFlowPinStateLeft;
volatile uint8_t lastFlowPinStateRight;

volatile int32_t lastFlowTimer = 0;

float litersPerTank = 18.9;
float pricePerTank = 78;

bool clearNextTick = true;

// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect)
{
uint8_t left = digitalRead(FLOWSENSORPINLEFT);
uint8_t right = digitalRead(FLOWSENSORPINRIGHT);

if (left != lastFlowPinStateLeft && left == HIGH) {
//low to high transition!
pulsesLeft++;
}

if (right != lastFlowPinStateRight && right == HIGH) {
//low to high transition!
pulsesRight++;
}

lastFlowPinStateLeft = left;
lastFlowPinStateRight = right;

uint16_t pulses = pulsesLeft = pulsesRight;

if (pulses == lastPulses)
{
if (lastFlowTimer > -1)
lastFlowTimer++;
}
else
{
if (lastPulses == 0)
clearNextTick = true;
lastFlowTimer = 0;
}

lastPulses = pulses;
}

void useInterrupt(boolean v)
{
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
}
else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}

void setup()
{
Serial.begin(9600);
Serial.print("Flow sensor test!");

pinMode(FLOWSENSORPINLEFT, INPUT);
digitalWrite(FLOWSENSORPINLEFT, HIGH);
lastFlowPinStateLeft = digitalRead(FLOWSENSORPINLEFT);

pinMode(FLOWSENSORPINRIGHT, INPUT);
digitalWrite(FLOWSENSORPINRIGHT, HIGH);
lastFlowPinStateRight = digitalRead(FLOWSENSORPINRIGHT);

useInterrupt(true);

pinMode(lcdPin, OUTPUT);
LCD.begin(9600);
clearLCD();
lcdPosition(0, 0);
LCD.print("Starting Up");

lastFlowTimer = -1;
clearNextTick = true;
pulsesLeft = 0;
pulsesRight = 0;
}

void loop() // run over and over again
{
uint16_t pulses = pulsesLeft = pulsesRight;

if (clearNextTick)
clearLCD();
clearNextTick = false;

if (pulses == 0)
{
if (lastPulses > 0)
clearLCD();

lcdPosition(0, 4);
LCD.print("Welcome!");
lcdPosition(1, 1);
LCD.print("Begin pouring!");
}
else
{
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
liters /= 1.4; //weird const

float ounces = liters * 33.8;

float price = liters / litersPerTank * pricePerTank;

if (pulsesRight > 0)
LCD.print("right");
else
LCD.print("left");
lcdPosition(1, 0);
LCD.print("Oz: ");
LCD.print(ounces);
lcdPosition(1, 9);
LCD.print(" $:");
LCD.print(price);

// Reset
if (lastFlowTimer > 1000)
{
lastFlowTimer = -1;
clearNextTick = true;
pulsesLeft = 0;
pulsesRight = 0;
}
}

delay(100);
}

// wbp: goto with row & column
void lcdPosition(int row, int col) {
LCD.write(0xFE); //command flag
LCD.write((col + row * 64 + 128)); //position
delay(LCDdelay);
}
void clearLCD(){
LCD.write(0xFE); //command flag
LCD.write(0x01); //clear command.
delay(LCDdelay);
}

Advertisements