# HELP! keypad response loop?

This is my first arduin project so please bare with me…ill post the sketch after i explain the problem. I ave built an airsoft prop bomb as par instructions on the net, and used the following code. hardware = 3x4 keypad, 16x2 lcd, and 2 x led’s, with arduino due.328.

The problem im havein is that after entering a random four digit code to “arm” it, the keypad is then slow to respond for the first digit of the “disarm” code. After the initial digit (im guessing it is because the timer loop is then paused), the next 3 digits are entered perfectly fine.

So my question is how can i restructure the sketch so that it see’s the keypad faster…can this be done by adding an interupt? (ive searched google for hours and some links seemed to point towards an interupt) ive tried debouncing code, although i think ive got confused as to weather this will actually accomplish anything…

also… im looking for suggestions as to having a fixed four digit password, rather than having to input a random one each time, i found several different examples through google and tried them in place of the original, however no luck was to be had. this is just a bonus if anyone could help me with this though, as my main concern is keypad response.

``````#include <Bounce.h>
#include <LiquidCrystal.h>
#define pound 14
#define LED_RED 5
#define LED_GREEN 6
#define LED_YELLOW 4
#define LED_BLUE 3
#define KP_ROWS 4
#define KP_COLS 3

#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON 10
#define DEBOUNCE_OFF 3

unsigned int timerSeconds = 300; // start with 5 minutes on the clock
byte entered[4];
int currentLength = 0;
byte i = 0;

LiquidCrystal lcd(7, 8, 10, 11, 12, 13);

char keys[KP_ROWS][KP_COLS] = {
{'1','2','3'},{'4','5','6'},
{'7','8','9'},{'*','0','#'}
};
byte rowPins[KP_ROWS] = { 2, 14, 18, 16 }; //connect to the row pinouts of the keypad {18, 2, 14, 16}
byte colPins[KP_COLS] = { 17, 15, 19 }; //connect to the column pinouts of the keypad {17, 19, 15}

char key;

//LCD setup
void setup() {
pinMode(LED_RED, OUTPUT); // sets the digital pin as output
pinMode(LED_GREEN, OUTPUT); // sets the digital pin as output
lcd.begin(16, 2);
lcd.print("Enter Code: ");
lcd.cursor();
lcd.setCursor(6, 1);
while (currentLength < 4) {
if (key != NO_KEY) {
lcd.print(key);
currentLength++;
lcd.setCursor(currentLength + 6, 1);
} else delay(2); // only sample it every 1/10 sec, no need to go apesh!t
}
lcd.noCursor();
delay(1000);
lcd.clear();
delay(200);
lcd.print("  Bomb Armed ");
lcd.setCursor(6, 1);
delay(1000);
lcd.clear();
currentLength = 0;
}

void loop() {
lcd.clear();
lcd.print("Enter Code: ");
lcd.cursor();
key = keypad.getKey(); // get the key once per this loop
if (key != NO_KEY) {
lcd.setCursor(6, 1);
lcd.print("               ");
lcd.setCursor(6, 1);
while (currentLength < 4) {
// here, we take the key that broke us into this "if" statement, instead of discarding it. this also pauses the timer, effectively.
if (key != NO_KEY) { // redundant for the first loop, but necessary for every next character
lcd.setCursor(currentLength + 6, 1);
lcd.print("*");
lcd.setCursor(currentLength + 6, 1);
lcd.print(key);
entered[currentLength] = key;
currentLength++;
} else delay(10); // only sample it every 1/10 sec
key = keypad.getKey(); // get the key every time "while" loops
}
if (memcmp(entered,password,4) == 0) { // shortcut to compare an array of bytes, use memcmp(A, B, length), will return 0 if match.
lcd.noCursor();
lcd.clear();
lcd.print(" Defused ");
lcd.setCursor(0, 1);
lcd.print("You Win");
delay(25000000); // hold that on screen for 2.5 seconds
currentLength = 0;
} else {
lcd.noCursor();
lcd.clear();
lcd.print(" Wrong Code ");
lcd.setCursor(0, 1);
lcd.print(" Time - 1:00"); // Display time penalty
if (timerSeconds > 60) timerSeconds -= 60;
else timerSeconds = 1; // will trigger BOOM next cycle
currentLength = 0;
delay(1000); // hold that on screen for 3 seconds
}
}
timer(); // prints current time on screen, checks for "go off" time, holds loop for 1 second
}

void timer() {
lcd.setCursor(0, 1); // sets cursor to 2nd line
lcd.print("Timer: ");
lcd.print(timerSeconds / 60); // integer number of minutes on clock
lcd.print(":");
lcd.print(timerSeconds % 60); // mod 60; gives us number of seconds outside of 60sec/min
delay(950); // hold us here for 0.950 seconds
digitalWrite(LED_GREEN,!digitalRead(LED_GREEN)); // toggle the green LED once a second
tone(9,784, 200); // play bleep for 50ms
delay(50); // and there's the rest of that 1 second (not counting LCD delay)

timerSeconds--;
if (timerSeconds == 0) bombomb();
}
void bombomb() {
// routine for making ze BOMB GOEZ OFF!!!1 :D
lcd.noCursor();
lcd.clear();
lcd.print(" !BoOo0o0o0om! ");
tone(9,110, 200);
digitalWrite(LED_BLUE, HIGH); // sets the red LED on
delay(500); // waits half a second
digitalWrite(LED_RED, HIGH); // sets the red LED on
delay(500); // waits half a second
tone(9,110, 200);
digitalWrite(LED_GREEN, HIGH); // sets the green LED on
tone(9,110, 200);
delay(500); // waits half a second
digitalWrite(LED_GREEN, LOW); // sets the green LED off
tone(9,110, 200);
key = NO_KEY; // enter the while() loop, otherwise it'll skip. i could use do { } while();, but I'm lazy.
while (key == NO_KEY) {
if (key == '#') {
lcd.clear();
lcd.print("Game Over");
delay(2500000000); // good ole' 2.5 sec delay
delay(2500000000);
delay(2500000000);
timerSeconds = 300; // put 5 minutes on the clock
// and we break the loop and go back to timer.
} else key = NO_KEY; // continue looking for pound key
}
}
``````

like i said im a complete noob, and i appologise if i havent been descriptave enough, but any help at all would be greatly appreciated. thanx all!

This is my first arduin project so please bare with me.

I'll keep my clothes on, thank you.

I ave built an airsoft prop bomb as par instructions on the net

Oh, those instructions... Clearly, they must have been good instructions. After all, they were on the internet.

``````         } else delay(2); // only sample it every 1/10 sec, no need to go apesh!t
``````

Why not react to the keypresses as fast as possible? Does anyone accuse you of going apeshit when you read as fast as possible. Slow down. Take an hour to read the rest of this sentence. That makes no sense, does it? Neither does your delay() or comment.

``````             delay(25000000); // hold that on screen for 2.5 seconds
``````

You seem to have a serious misunderstanding of delay(). 2,5 seconds is NOT 2.5 million microseconds. It is 2.5 million microseconds, but that is not the order of magnitude that delay() uses.

If you want to read keypresses faster, get rid of every single delay() in your code.

thanks for your comments, but a lot of the stuff in there (like the quotes) i just left, originally it was a 2.5 second delay, but i altered it gor a longer one to hold that screen, didnt feel it nescessary to change the // comments, same with the "dont go apesh!t" quote. ill try removing the delays and see what happens.