Go Down

Topic: How to make instant stop button in the keypad.h library (Read 1 time) previous topic - next topic


Apr 12, 2014, 03:11 pm Last Edit: Apr 12, 2014, 03:16 pm by hola123 Reason: 1
hi :),

I would like to help me for the below
If I pressed '1' key the Arduino will do some actions for 60 seconds , I want to be able to stop these action instantly while it's running by pressing '2' key

I know that to make the loop stop or reset it, i can use
Code: [Select]
void (*resetloop)(void)=0;
but I don't know how to make it work, I tired a lot with (while) function but no luck . help the newbie please  ^_^
Code: [Select]
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
byte rowPins[ROWS] = {3, 2, 1, 0};
byte colPins[COLS] = {7, 6, 5, 4};

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
void loop(){
 char customKey = customKeypad.getKey();
 if (customKey=='1'){
   //done some actions which will take 60 second




Your code is blocking, and that is why you can't get things to work like you want to.
I can't tell you why your code is blocking, because you didn't post it (you're just flashing a part of your code as if you're teasing (doesn't work for me though)).

But because you are telling us that the part in an if statement takes a full minute, i'm sure there is a blocking code in there.
If you use non blocking code, the loop function will be run hundreds of times per second, if not thousands.
You can decide to keep doing what you are doing, or to do your "instant stop" during one of these runs.
That would in fact be a near instant stop, so in some cases you might feel the need to repair something after registering a stop was requested.
But that's for later, first the blocking needs to be addressed.

Blocking code contains delay() or while.. most of the times, there might be other ways to block and you need to learn how to avoid that.
You can start by practicing the "blink without delay" sketch, and start understanding how and why that works.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Go Up