Button logic

hey just removing all the delays and cleaning things up seems to have helped a bunch. my button presses seem to be recognized better now too. i dont have to hold the buttons down so long. still a little erratic ill keep working on it.
my checkflow function seems to be working again as well. :wink:
I removed all the delays except in the interupt. ill try removing that one next. I also fixed some places that were clearing the lcd and making it blink really fast.

// 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;

long previousMillis = 0;    
long interval = 1000;
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 ()   
{
  unsigned long currentMillis = millis();
  checkbuttons();
  if (amigoing==1){
    if (devicemode==2) { 
      autofill(); 
    }
    if (devicemode==1){ 
      checkflow(); 
    }
  }
  else {
    if (devicemode == 0) {
      lcd.cursorTo(1,1);
      lcd.print ("Waiting ");

    }
    if (devicemode == 1) {
      if (amigoing != 1){
        lcd.cursorTo(1,1);
        lcd.print ("Counter Mode");
      }
    }
    if (devicemode == 2) {

      lcd.cursorTo(1,1);
      lcd.print ("Autofill Mode");
      lcd.cursorTo(2,1);
      lcd.print (autofillamount);
    }
  }
}
void checkbuttons()
{

  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");
        autofillamount = 0;

      } 
      if (devicemode == 1) {
        newdevicemode = 2;
        lcd.clear();
        lcd.print ("AutoFill Mode");
        autofillamount = 0;

      }
      if (devicemode == 2) {
        newdevicemode = 0;
        lcd.clear();
        lcd.print ("Turning Off");
        lcd.clear();
        autofillamount = 0;
      }  
      devicemode = newdevicemode;

    }



  }

  stopgobuttonstate = digitalRead(stopgobuttonpin);  
  if (stopgobuttonstate != stopgolastButtonState) {

    // if the state has changed, do something

    if (stopgobuttonstate == HIGH)
    { 
      if (amigoing == 0) {

        if (devicemode == 1) {
          amigoing = 1;
          checkflow();
        }
        if (devicemode == 2) {
          amigoing = 1;
          autofill();
        }

      }

      else {
        amigoing = 0;
        devicemode = 0;
        lcd.clear();

      }
    }


  }   
  // 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.cursorTo(1,1);
  lcd.print ("Filling to:  ");
  lcd.cursorTo(2,1);
  lcd.print (autofillamount);


}

void checkflow()
{ 

  NbTopsFan = 0;   //Set NbTops to 0 ready for calculations
  sei();      //Enables interrupts
  delay (1000);   //Wait 1 second
  cli();      //Disable interrupts
  lcd.clear();

  // 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: ");

  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");

}