I am working on a project which requires the user to press a key on the keypad when an interrupt is called. The interrupt is called by pressing a push button and the function that it executes requires the user to then press either 1,2,3,or 4 before proceeding to make the necessary computations.
The problem is that when I use keypad.waitForKey(), the program just stops executing.
Can anyone please help me figure this out.
Here is the code for the function
void addPassenger(){
if(digitalRead(3))
{
//When LED lights///user can choose option
digitalWrite(10,HIGH);
lcd.clear();
key = NO_KEY;
while(true){
key = keypad.waitForKey();
myDelay(100);
lcd.print("?");
if(key != NO_KEY)
break;
}
state = false;
switch(key){
case '2':
if(!onBoard[1]){
onBoard[1] = true;
numberOnBoard++;
lcd.setCursor(0,1);
lcd.print("Passenger 2 added");
}
else
{
lcd.setCursor(0,1);
lcd.print("Passenger already in");
}
break;
case '3':
if(!onBoard[2]){
onBoard[2] = true;
numberOnBoard++;
lcd.setCursor(0,1);
lcd.print("Passenger 3 added");
}
else
{
lcd.setCursor(0,1);
lcd.print("Passenger already in");
}
break;
case '4':
if(!onBoard[3]){
onBoard[3] = true;
numberOnBoard++;
lcd.setCursor(0,1);
lcd.print("Passenger 4 added");
}
else
{
lcd.setCursor(0,1);
lcd.print("Passenger already in");
}
break;
}
}
else
digitalWrite(10,LOW);
}
N.B I placed an LED on pin 10 to indicate when the interrupt is called...it lights when I press the push button
UKHeliBob:
This sounds like a very weird use of an interrupt. Can you please explain why it is appropriate here ?
I am basically using a GPS module to track the position of 4 people(each person numbered 1 to 4) as when and when they want me to start. It starts with one person and then later the user has the option of adding more people . This is to be done via the interrupt. So the interrupt has to basically wait for the person to enter the number of the passenger he/she wants to add
sterretje:
Just use getKey() and check if the returned value is not equal to NO_KEY.
Can you please explain further... I have already tried using the procedure you described but it didnt work...probably because I didnt implement it well...
Can you send a code
PaulS:
It does NOT have to be done using an interrupt. Waiting in an interrupt handler is abusing the purpose of an interrupt.
Without understanding your whole project, and knowing why you think you need to use interrupts, and what is currently generating them, and what else the Arduino has to do? No.
The thing is working now. I transferred the function into the main loop ...it will be executed only when a certain variable is true...the interrupt then only has to set the variable to true when called