Hi, I just bought a 12 key numeric keypad and tried to make a lock code with it. I opened the serial monitor and typed the passcode but nothing was printed into the serial monitor!
THE CODE
#include "Keypad.h"
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
{{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char PIN[6]={'1','2','3','4','5','6'};
char attempt[6]={0,0,0,0,0,0};
int z=0;
void setup()
{
 Serial.begin(9600);
}
void correctPIN()
{
 Serial.println("CORRECT PIN ENTERED...");
}
void incorrectPIN()
{
 Serial.println("INCORRECT PIN ENTERED!");
}
void checkPIN()
{
 int correct=0;
 int i;
 for ( i = 0; i < 6 ; i++ )
 {
  if (attempt[i]==PIN[i])
  {
   correct++;
  }
 }
 if (correct==6)
 {
  correctPIN();
 } else
 {
  incorrectPIN();
 }
 for (int zz=0; zz<6; zz++)
 {
  attempt[zz]=0;
 }
}
void readKeypad()
{
 char key = keypad.getKey();
 if (key != NO_KEY)
 {
  switch(key)
  {
   case '*':
    z=0;
    break;
   case '#':
    delay(100);
    checkPIN();
    break;
   default:
    attempt[z]=key;
    z++;
  }
 }
}
void loop()
{
 readKeypad();
}
Maybe sprinkle some Serial.println() statements in your code to see where its hanging up? I usually like to put a Serial.println(key) in my "if (key != NO_KEY)" statements. That way I know if the keypad is working, if its hooked up correctly, and what keys it thinks are being pressed.
There is nothing wrong with your code, so it could be your wiring to your keypad that may be the problem. If everything is wired correctly and the baud rate on the serial monitor (located in the bottom right corner) matches the BR in your code, enter this *123456#.
wizdum:
Maybe sprinkle some Serial.println() statements in your code to see where its hanging up? I usually like to put a Serial.println(key) in my "if (key != NO_KEY)" statements. That way I know if the keypad is working, if its hooked up correctly, and what keys it thinks are being pressed.
Ohhhhhhhh ok I'll try that thanks!
It turns out that my keypad was working... I just don't know how to connect it could you please tell me? It also needs to be relative to my program.
HazardsMind:
There is nothing wrong with your code, so it could be your wiring to your keypad that may be the problem. If everything is wired correctly and the baud rate on the serial monitor (located in the bottom right corner) matches the BR in your code, enter this *123456#.
I entered that but it doesn't recognise the keys... Could someone tell me the pins locations PLEASE?
HazardsMind:
We don't know what keypad you have, so we cant tell what pins to use.
Oh yeah sorry...
My keypad is from jaycar so go on their webpage
The catalogue number is CAT. NO. SP0770
Search the number in jaycar search bar and it should be black w/ white keys!
Take a look at the Procedure section. Here is the link HERE.
The picture that came up with the link you gave us, only showed 7 pad, so it should be the first 3 pads are for the COL, and the rest are your ROW pins
HazardsMind:
We don't know what keypad you have, so we cant tell what pins to use.
Oh yeah sorry...
My keypad is from jaycar so go on their webpage
The catalogue number is CAT. NO. SP0770
Search the number in jaycar search bar and it should be black w/ white keys!
Also problems occur i.e., 3 isn't 4 in computer is it????? or # isn't 0?
I don't understand because I'm just starting the hardware side of electronics. (I'm more of a software person)
HazardsMind:
Take a look at the Procedure section. Here is the link HERE.
The picture that came up with the link you gave us, only showed 7 pad, so it should be the first 3 pads are for the COL, and the rest are your ROW pins
HazardsMind:
Take a look at the Procedure section. Here is the link HERE.
The picture that came up with the link you gave us, only showed 7 pad, so it should be the first 3 pads are for the COL, and the rest are your ROW pins