[SOLVED] Interrupts not working

Hello,

my accidentally posted to much code in my last thread so I think it got a bit to long. But now I now at least the the problem is.

Here is the part of the setup rutine:

  attachInterrupt(1, keypadButtonPressed, FALLING);
#ifdef TEST_WITH_DEBUG
  Serial.println("Interrupt for keypad attached");
#endif

Here is the function keypadButtonPressed:

void keypadButtonPressed(){
#ifdef TEST_WITH_DEBUG
  Serial.println("In keypadButtonPressed()");
#endif
  detachInterrupt(1);
  int newInt = processKeypad();
  sendDigit(newInt);
  if(newInt != NOTHING && newInt != ENTER && newInt != CLEAR){
#ifdef TEST_WITH_DEBUG
    Serial.println("Is a number");
#endif
    numbers[digitForNumbers] = newInt;
    digitForNumbers--;
    lcd.print(newInt);
    numberNow = (numbers[0] * 1) + (numbers[1] * 10) + (numbers[2] * 100) + (numbers[3] * 1000) + (numbers[4] * 10000);
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
  else if(newInt == ENTER){
#ifdef TEST_WITH_DEBUG
    Serial.println("Is enter");
#endif
    addBib( (numbers[0] * 1) + (numbers[1] * 10) + (numbers[2] * 100) + (numbers[3] * 1000) + (numbers[4] * 10000) );
    numbers[0] = 0;
    numbers[1] = 0;
    numbers[2] = 0;
    numbers[3] = 0;
    numbers[4] = 0;
    numbers[5] = 0;
    numberNow = 0;
    digitForNumbers = 4;
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
  else if(newInt == CLEAR){
#ifdef TEST_WITH_DEBUG
    Serial.println("Is clear");
#endif
    numbers[0] = 0;
    numbers[1] = 0;
    numbers[2] = 0;
    numbers[3] = 0;
    numbers[4] = 0;
    numbers[5] = 0;
    numberNow = 0;
    digitForNumbers = 4;
    showTime();
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
}

So, the problem is: I newer get into the interrupt function keypadButtonPressed. The message "In keypadButtonPressed" newer arrives at the computer.

Yes, TEST_WITH_DEBUG is defined and the serial communication is started with Serial.begin()

I'm using an ATmega644 with the Sanguino core files.

Please help as fast as possible, this must be done before tomorrow at this time, and I'm sure I will get more bugs later in other parts of the code.

JanD

I forgot: The connection is like this; I have a common connected keypad, with the common connection to ground. All uncommon connections go to inputs of the ATmega644 with internal pull-ups and with diodes with the cathode pointing towards the keypad and ALL anodes connected together to INT1 and to an external 10k pull-up resistor.

JanD

Yay, it's working! So now on to the next two problems:

  1. It wont work if I press the same number two times or more in a row. But if I press a different number in between I can press the first number again. Like this: Input: 2-2-3-2 Output: 2-3-2. What's the problem now?

  2. The number saved in numberNow in the function keypadButtonPressed goes crazy. It adds zeros or random numbers ever then and then. What's the problem here?

As sad in my first post, this must be done in under 24 hours now, so please help.

JanD

EDIT: Problem number 2 is solved, but I can still not enter the same number two times in a row :frowning:

I don't think this is your problem but are all those variables defined as volatile? You expect them to persist between interrupts and I'm not sure if that works or not.

Really that's way too much code for an ISR, you should just set a variable with the key value and handle it in the main loop.

However, having said that it seems to be mostly working.

The problem you describe indicates that somewhere the new key is being tested against the last key to see if a button has been pressed, and the two are the same when you press 2 2, but I don't see anything like that in the code you've shown.

The number saved in numberNow in the function keypadButtonPressed goes crazy.

That could be if the variables aren't defined with the "volatile" keyword.

Can you post ALL the code?


Rob

Graynomad:
I don't think this is your problem but are all those variables defined as volatile? You expect them to persist between interrupts and I'm not sure if that works or not.

Really that's way too much code for an ISR, you should just set a variable with the key value and handle it in the main loop.

However, having said that it seems to be mostly working.

The problem you describe indicates that somewhere the new key is being tested against the last key to see if a button has been pressed, and the two are the same when you press 2 2, but I don't see anything like that in the code you've shown.

The number saved in numberNow in the function keypadButtonPressed goes crazy.

That could be if the variables aren't defined with the "volatile" keyword.

Can you post ALL the code?


Rob

volatile? Never heard of. Does it effect anything negative if I use that? If not I will use what.

The code part that looks for keypresses is called then in keypadButtonPressed, and the function is called processKeypad:

#define KEY1 23
#define KEY2 27
#define KEY3 31
#define KEY4 22
#define KEY5 26
#define KEY6 30
#define KEY7 21
#define KEY8 25
#define KEY9 29
#define KEY0 24
#define KEYE 28
#define KEYC 20

#define ENTER 10
#define CLEAR 11
#define NOTHING 13

const byte keypadPins[12] = { KEY0, KEY1, KEY2, KEY3, KEY4, KEY5, KEY6, KEY7, KEY8, KEY9, KEYE, KEYC };

int lastKeypadStatus[12] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
int numbersOnKeys[12] = {0,1,2,3,4,5,6,7,8,9,ENTER,CLEAR};

int processKeypad(){
  for(int keys = 0; keys < 12; keys++){
    int valueNow = digitalRead(keypadPins[keys]);
    if(valueNow != lastKeypadStatus[keys]){
      lastKeypadStatus[keys] = valueNow;
      if(valueNow == LOW){
        return numbersOnKeys[keys];
      }
    }
    lastKeypadStatus[keys] = valueNow;
  }
  return NOTHING;
}

If I press the same number twice, it always returns NOTHING (13).

I don't want to post all code, because what would mean about 3 post (3*9500=very much).
Because of that I only posted the code snippets that I think are needed. If anyone wants the code, it can be downloaded from here though: Titagremo download | SourceForge.net

I hope you can help me,
JanD

This is what I was looking for

if(valueNow != lastKeypadStatus[keys]){

When you press two keys the same this test fails and you never do the

lastKeypadStatus[keys] = valueNow;
if(valueNow == LOW){
return numbersOnKeys[keys];
}

I think that's the problem, if not hopefully someone else can have a look because it's 2AM here :slight_smile:


Rob

So what should I do instead? In some way I need to debounce the buttons. I see now why it doesn't work, but I don't have a clue how to fix it.

Please help me and thanks in advance,
JanD

Solved!