4x4 matrix keypad and interrupt

Hello,

how can I exit a loop when I press the button ( 2 or 8 ) on the keypad. Is possible to using functions attachInterrupt()?

I have ArduinoMega 2560.

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte rowPins[ROWS] = {45, 43, 41, 39};  
byte colPins[COLS] = {37, 35, 33, 31}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

volatile int counter =0;

void setup() {
  Serial.begin(9600);

attachInterrupt(_, pushButton, HIGH); //interrupt must working when I press key 2 or 8
}

void loop() {
  counter++;
  Serial.print("counter= ");
  Serial.println(counter);
  delay(1000);  
}

void pushButton(){
  counter=0;
}

That's not what interrupts do.

What exactly do you want to happen when you press 2 or 8? As it is you don't have any code trying to read the keypad.

So an if statement that checks to see if the pressed button was 2 or 8 and calls pushButton?

Can you describe what you are trying to actually do instead of how you think you could do it? Because you seem to have a major misunderstanding somewhere and it isn't really clear exactly where.

To get interrupts on Pin 45 ('2') and Pin 41 ('8') you need to set up Pin Change Interrupts. You won't be able to use the other keypad keys while the interrupts are enabled.

jantomsic:
I want in this code change rotary encoder with matrix keypad [keys: 8=(up); 5=(select); 2=(down)]

This makes no sense. What does it mean to change the rotary encoder? If you want it to turn an encoder you'll need some sort of motor to turn it. Now, maybe you explain what you want this code to do instead of how you think you would do it. (Why do I feel like I am repeating myself?)

Please read the forum rules about how to properly post code before posting again.

Quote tag is not good for code posting. Use code tag. Left most formatting icon </>

I believe you have missed something here, and other places.

void showAnimalBox(){
   int startString = encoder+48;
   int stopString = encoder+49;
   int i = 0;
   while (animalBox != startString) {i++;}
   while (animalBox != stopString){
     i++;
     if(animalBox != stopString) {
       lcd.print(animalBox);
     }
   }
 }

The linked photo so you don't have to download..

OK. Let's start with a simple sketch that just reads the keys pressed on the keypad and prints them out. Can you do that part? Once you learn that, I think coming back to this code and trying to add the keypad might be a bit simpler.

OK. Show me. I really am trying to help you. Nobody here is going to write your code out for you, but if you'll follow along with me you'll see that you can do it all. If you can write the sketch I mentioned and understand how it works, then doing what you want is much simpler. If you just know how to copy and paste code but don't understand it at all then this may be over your head. I'm just trying to figure out which case we are in before I try to help you out so I'll now which direction to take things.

@jantomsic Look here for keypad tutorial. When you properly can read the buttons you can start to build the menu system.

added:

jantomsic:
No I didn't,...

I just want scrolling in this menus with buttons not with rotating rotary encoder.

If I get this right... You have found some code there they use a rotary encoder to navigate the menu system.

Instead of modifying the code to use buttons to navigate, you thought it would be easier to use the buttons to emulate the behaviour of a rotary encoder..? :astonished:

jantomsic:
Yes i can, but this is not what i want. In my project i will have matrix keypad 1x5 with up down left right and select and more then 3 menus and 3 submenus...
Now i just trying and learning, how i will do it...

Please post a link to that keypad. I can visualise 3 different ones; one that needs to be hooked up to an analog pin, one that uses rows and columns and one that just requires 5 inputs on the arduino.

Unfortunately only lot of 10, but it looks good for your project.