SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Nii_Bruce
#192660
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
User avatar
By DanV
#192663
Why in the world would you put that code in an interrupt handler is beyond me!
First, you hold up the entire processor waiting for a key - extremely bad form in an ISR! You shouldn't just avoid it - you should NEVER do it!
excerpt from a simple interrupt tutorial
http://www.engblaze.com/we-interrupt-th ... nterrupts/

... interrupts are normally globally disabled inside of any ISR (this is why delay() and millis() don’t work.