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

8 responses to “Kegerator Flow Meter Test

  1. wow this is awesome! how were you able to get the your data to start and stop based on the amount of flow that’s being measured. I’m also working on a similar project trying to measure gal/min, but when I’m using adafruits code i realized that their loop is continuous regardless when i stop my flow-meter. Also if i’m not using an LCD monitor display how would you do that only using the Arduino Serial Monitor?

    Thanks

    • Hi Darren,
      Your best bet is to take my code, run it, and start modifying it to suit your needs.
      If you wanted to write to the serial monitor instead of the lcd, just replace the ‘LCD.print()’s with ‘Serial.println()’ once your serial port is open (Serial.Begin(speed))

  2. If you wanted to skip the facial and camera parts but have users pick who they are pouring would one just skip those steps?

    100% new at RPi stuff
    Thanks for the time

    • Hi notrubr, if you’re not using the camera or facial recognition, you should be able to skip step 4, you’ll just need to take out the references in the project once you’ve got it loaded up. I’ve not personally tried this though, so let me know if you encounter any issues!

      • Thanks Randy. I have not seen that item before. That is an interesting chip, but I’m not sure it would be worth it for $40, unless you needed much more precise measurement and that can provide it. I have my flowmeters plugged directly into the pi and I get fairly consistent results, but +/- 2oz accuracy per pint. I do use the adafruit flowmeters, no mention of food safe, but others had looked inside and saw no reason it wouldn’t be food safe, so I gambled 🙂 No problems yet!

  3. Very cool project! I haven’t seen any updates for a while and am wondering if you ever got facial recognition to work. I am hoping to take your project and start with it, then add the load cells like you mention. Have you done any updated work for this since your last post?

    • Thanks Max!
      I have not touched the project in quite a while. The facial recognition is bare bones, and not feature complete, but it should be a good starting point for improvements. I do plan on coming back and finishing that, but I have no ETA for when I’d have the time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s