I'm having trouble using buttons in my sketch. This is my first attempt at using them for anything other than turning on an led.
The problem im having is the loop messing with my brain.
pretty simple setup. 2 buttons. Mode Button to change modes. StopGo button to start or stop whatever mode you selected. And a pot to set the level for one of the modes.
I'm currently able to select my mode and start it but that's where things start to fall apart.
I feel like I need a bunch of If/Else statements to keep things flowing properly when it loops. Is that the way its usually done? I searched but found mainly simple press the button to turn on the led or huge menu systems I couldn't quite wrap my head around.
One other issue is my checkflow function doesnt work. When i run it in a standalone sketch it works fine. I suspect the way its looping is off.
It would be great if somebody can take a quick look at my sketch and tell me anything obviously wrong and/or if I'm even heading in the right direction.
thanks
// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com
#include "SparkSoftLCD.h"
#define LCD_TX 3
SparkSoftLCD lcd = SparkSoftLCD(LCD_TX);
int incomingByte = -1;
int val = 0;
char code[10];
int bytesread = 0;
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor
float totaldispensed;
int newdevicemode = 0;
int devicemode = 0;
int devicemodebuttonstate;
int devicemodebuttonpin = 4;
int devicemodelastButtonState = 0;
int stopgobutton;
int stopgobuttonstate;
int stopgobuttonpin = 5;
int stopgolastButtonState = 0;
int autofillamount = 0;
int amigoing = 0;
int potPin = 1;
int potval = 0;
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
// setup lcd
pinMode(LCD_TX, OUTPUT);
lcd.begin(9600);
lcd.clear();
Serial.begin(9600);
// hidden cursor
lcd.cursor(0);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()
{
checkbuttons();
if (amigoing==1){
if (devicemode==2) {
autofill();
}
if (devicemode==1){
checkflow();
}
}
}
void checkbuttons()
{
if (devicemode == 0) {
lcd.clear();
lcd.print ("Waiting for your ");
lcd.cursorTo(2,1);
lcd.print ("command sir...");
}
if (devicemode == 1) {
// lcd.clear();
if (amigoing != 1){
lcd.cursorTo(1,1);
lcd.print ("Counter Mode");
}}
if (devicemode == 2) {
// lcd.clear();
lcd.cursorTo(1,1);
lcd.print ("Autofill Mode");
lcd.cursorTo(2,1);
lcd.print (autofillamount);
}
delay (500);
devicemodebuttonstate = digitalRead(devicemodebuttonpin);
// compare the buttonState to its previous state
if (devicemodebuttonstate != devicemodelastButtonState) {
// if the state has changed, increment the counter
if (devicemodebuttonstate == HIGH)
{
if (devicemode == 0){
newdevicemode = 1;
lcd.clear();
lcd.print ("Counter Mode");
delay (500);
autofillamount = 0;
// checkflow();
}
if (devicemode == 1) {
newdevicemode = 2;
lcd.clear();
lcd.print ("AutoFill Mode");
delay (500);
autofillamount = 0;
// autofill();
}
if (devicemode == 2) {
newdevicemode = 0;
lcd.clear();
lcd.print ("Turning Off");
delay (500);
lcd.clear();
autofillamount = 0;
}
devicemode = newdevicemode;
}
delay(50);
}
stopgobuttonstate = digitalRead(stopgobuttonpin);
if (stopgobuttonstate != stopgolastButtonState) {
// if the state has changed, do something
if (stopgobuttonstate == HIGH)
{
if (amigoing == 0) {
if (devicemode == 1) {
lcd.clear();
lcd.print ("going to checkflow");
lcd.cursorTo(2,1);
amigoing = 1;
delay (5000);
checkflow();
}
if (devicemode == 2) {
lcd.cursorTo(1,1);
lcd.print ("AutoFill Amount");
lcd.cursorTo(2,1);
lcd.print (autofillamount);
amigoing = 1;
autofill();
}
}
else {
amigoing = 0;
}
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
stopgolastButtonState = stopgobuttonstate;
// read the pot to get fill amount:
potval = analogRead(potPin);
//map fill to 200 gallon max
autofillamount = map(potval, 0,1023,1,200);
}
void autofill(){
// lcd.print("blah");
// delay(5000);
lcd.cursorTo(1,1);
lcd.print ("Filling to:");
lcd.cursorTo(2,1);
lcd.print (autofillamount);
}
void checkflow()
{
//lcd.print("counting");
// delay(5000);
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
lcd.clear();
/// lcd.print("counting123");
// delay(5000);
// block-style blinking cursor
lcd.cursor(0);
Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
lcd.print (Calc, DEC); //Prints the number calculated above
lcd.print (" L/hour"); //Prints "L/hour" and returns a new line
lcd.cursorTo(2,1);
totaldispensed +=((float)Calc / 3600);
lcd.print ("total: ");
delay(5000);
int intValue = (int)totaldispensed; // convert float PHValue to tricky int combination
float diffValue = totaldispensed - (float)intValue;
int anotherIntValue = (int)(diffValue * 1000.0);
lcd.print (intValue);
lcd.print (".");
lcd.print (anotherIntValue);
lcd.print ("L");
delay(5000);
}