I have this program in which I'm using 4x4 keypad to input numbers. The program works perfectly first time but when we call any of the two functions again from the loop the keypad stops working. The problem seems to be Key = kpd.getKey() since it doesn't return any value the second time it is used. I have tried almost all the possibilities but couldn't find the culprit. BTW it is a code for milk dispensing machine using Hall effect sensor.
Here is the code :
// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(42, 38, 28, 26, 24, 22);
volatile int NbTopsFan; //measuring the rising edges of the signal
//int Calc;
int hallsensor = 2;//The pin location of the sensor
unsigned long TotalTicks = 0;
unsigned long LcdOut = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { A7, A6, A5, A4 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { A3, A2, A1, A0};
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String LitreValue = "";
int Litre = 0;
String MilkValue = "";
int Milk = 0;
double LitreMilk = 0;
char key;
char key1;
char key2;
void EnterPrice(){
start:
LitreValue = "";
lcd.clear();
lcd.print("ENTER PRICE");
lcd.setCursor(0, 1);
key = NO_KEY;
key = kpd.getKey();
do{
key = kpd.getKey();
Serial.print(key);
switch (key)
{
case '1':
lcd.print('1');
Serial.print("SDSD");
LitreValue.concat("1");
break;
case '2':
lcd.print('2');
LitreValue.concat("2");
break;
case '3':
lcd.print('3');
LitreValue.concat("3");
break;
case '4':
lcd.print('4');
LitreValue.concat("4");
break;
case '5':
lcd.print('5');
LitreValue.concat("5");
break;
case '6':
lcd.print('6');
LitreValue.concat("6");
break;
case '7':
lcd.print('7');
LitreValue.concat("7");
break;
case '8':
lcd.print('8');
LitreValue.concat("8");
break;
case '9':
lcd.print('9');
LitreValue.concat("9");
break;
case '0':
lcd.print('0');
LitreValue.concat("0");
break;
}
}while(key!='*');
Litre = LitreValue.toInt();
if(Litre<=0 || Litre>=500)
goto start;
lcd.clear();
}
void EnterValue(){
start:
MilkValue = "";
lcd.clear();
lcd.print("ENTER VALUE");
lcd.setCursor(0, 1);
key1 = kpd.getKey();
do{
Serial.print(key1);
key1 = kpd.getKey();
switch (key1)
{
case '1':
lcd.print('1');
MilkValue.concat("1");
break;
case '2':
lcd.print('2');
MilkValue.concat("2");
break;
case '3':
lcd.print('3');
MilkValue.concat("3");
break;
case '4':
lcd.print('4');
MilkValue.concat("4");
break;
case '5':
lcd.print('5');
MilkValue.concat("5");
break;
case '6':
lcd.print('6');
MilkValue.concat("6");
break;
case '7':
lcd.print('7');
MilkValue.concat("7");
break;
case '8':
lcd.print('8');
MilkValue.concat("8");
break;
case '9':
lcd.print('9');
MilkValue.concat("9");
break;
case '0':
lcd.print('0');
MilkValue.concat("0");
break;
}
}while(key1!='#');
Milk = MilkValue.toInt();
if(Milk<=0 || Milk>=50000)
goto start;
LitreMilk = (double) Milk / Litre;
TotalTicks = 0;
lcd.clear();
NewLoop();
}
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
//Serial.print("D");
}
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
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
EnterPrice();
//lcd.print("Enter Price");
EnterValue();
}
void NewLoop()
{
do{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (100); //Wait 1 second
cli(); //Disable interrupts
//Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
TotalTicks += NbTopsFan;
Serial.print (TotalTicks, DEC); //Prints the number calculated above
//Serial.print ("\n"); //Prints "L/hour" and returns a new line
lcd.setCursor(0,0);
lcd.print("DISPENSING...");
lcd.setCursor(0,1);
lcd.print(TotalTicks1.683/1000);
lcd.setCursor(8,1);
lcd.print(LitreMilk);
if(LcdOut!=TotalTicks){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DISPENSING...");
lcd.setCursor(0,1);
lcd.print(TotalTicks1.683/1000);
lcd.setCursor(8,1);
lcd.print(LitreMilk);
LcdOut = TotalTicks;
}
if(TotalTicks*1.683/1000>=LitreMilk)
EnterValue();
key2 = kpd.getKey();
if(key2=='A')
EnterValue();
if(key2=='D')
setup();
//EnterPrice();
Serial.print("!");
}while(1);
}
void loop() {
/*
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (100); //Wait 1 second
cli(); //Disable interrupts
//Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
TotalTicks += NbTopsFan;
Serial.print (TotalTicks, DEC); //Prints the number calculated above
//Serial.print ("\n"); //Prints "L/hour" and returns a new line
lcd.setCursor(0,0);
lcd.print("DISPENSING...");
lcd.setCursor(0,1);
lcd.print(TotalTicks1.683/1000);
lcd.setCursor(8,1);
lcd.print(LitreMilk);
if(LcdOut!=TotalTicks){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DISPENSING...");
lcd.setCursor(0,1);
lcd.print(TotalTicks1.683/1000);
lcd.setCursor(8,1);
lcd.print(LitreMilk);
LcdOut = TotalTicks;
}
if(TotalTicks1.683/1000>=LitreMilk)
EnterValue();
char key = kpd.getKey();
if(key=='A')
EnterValue();
if(key=='D')
EnterPrice();/
}