Calling functions through keypad 4x4

Hello,

I have written code in order to call functions when I press a button on a 4x4 Keypad:

void loop()
{
 . . .
    if(keypressed == '*') 
    {
        function_1();   
    }
    if(keypressed == '#') 
    {
        function_2();
    }
 . . .
}

but it takes to long to respond. How can I make it work more quickly? What I mean is, if it is possible, at the time I press the button, the Arduino let what is doing, and instantly call the function? I use Arduino MEGA 2560 R3.

Thank you...

How long is too long?

Maybe if you posted your code, you’d get help.

TheMemberFormerlyKnownAsAWOL: How long is too long?

Maybe if you posted your code, you'd get help.

About 1 minute pressing the button...it is too long...

Thank you

Write your functions to be non-blocking.

The code you posted will not necessarily take long at all...so the problem is in the parts you didn't bother posting. Like how you get 'keypressed" and what the functions are doing.

Steve

What is the Arduino doing during the minute it takes to respond ?

At a guess there is at least one for loop, probably two, possibly three or even more and inside the for loops there are delay()s so the effect of the for loops, particularly if they are nested is to repeat any short delay() many times which means that the loops take a minute before the code escapes from the clutches of the for loops and is able to read they keypad again.

I look forward to seeing the OPs code to see how good my crystal ball is

Can interrupts work in this case? I mean, whatever the code is running, when I press 'A' to instantly run the function and when it finishes, to continue to the code it was running before the function?

Thank you

Maybe. It depends what it is you're doing that you apparently don't want to tell us about.

If it is using library functions that are already using interrupts then maybe not. Or if "the function" you want to run takes a long time to run then maybe not again. It's anyone's guess.

Steve

alex5678:
Can interrupts work in this case? I mean, whatever the code is running, when I press ‘A’ to instantly run the function and when it finishes, to continue to the code it was running before the function?

Thank you

Attempting that is usually an indication that your code is poorly-designed and/or poorly-structured. Until you post the complete code that you have now, you’re unlikely to get significant help. For one thing, people will get tired of asking and simply move on to someone who shows (by being cooperative) that they deserve help.