Just not working the way it should

Went to a Hamfest last week and found a matrix keypad for $2 so I figured I'd play with it.

I copied this code right from the arduino playground:

/* Keypadtest.pde
*

  • Demonstrate the simplest use of the keypad library.
  • The first step is to connect your keypad to the
  • Arduino using the pin numbers listed below in
  • rowPins[] and colPins[]. If you want to use different
  • pins then you can change the numbers below to
  • match your setup.

*/
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin = 13;

void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
digitalWrite(ledpin, LOW);
break;
case '#':
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
}
}
}

I am having 2 problems

1: * and # are not showing up in the serial monitor but 1- 0 are.

2: The led does turn on and off when * or # is hit but it glows very dimmly. I thought the led on the UNO may be going bad so I switched to an external led hooked to another pin, but the same thing, very dim.

when I hit the reset button on the UNO the led on pin 13 does blink at full brightness....So I'm stumped.

1: * and # are not showing up in the serial monitor but 1- 0 are.

Your code does not send them to the serial monitor.

2: The led does turn on and off when * or # is hit but it glows very dimmly.

Set "ledpin" as an output!

Mark

I understand setting the ledpin to output. But can you clarify on getting the * and # to show in the serial monitor?

char key = kpd.getKey();
if(key) // Check for a valid key. /// This looks for a valid key. Earlier in the program all rows and columns were set totaling 12 keys. serial is only showing 10

serial is only showing 10

Look at your code. Where have you asked it to print to the serial port? Only in the case what is not the * case and is not the # case. If you want to know as each key is pressed, move the Serial.print() statement before the switch statement.

  if(key)  // Check for a valid key.

I'd prefer to see this explicitly stated:

  if(key != NO_KEY)  // Check for a valid key.

PaulS:

  if(key)  // Check for a valid key.

I'd prefer to see this explicitly stated:

  if(key != NO_KEY)  // Check for a valid key.

Hi Paul,

I originally had NO_KEY explicitly stated but someone got confused so I changed it to what you see now. Originally I thought it was a good idea to be explicit but do you mind if I ask why you prefer it that way?

-Mark

Hi. Slightly OT but:

Went to a Hamfest last week and found a matrix keypad for $2

You went to a Hamfest and only got a $2 item??? I'm starting to believe that my Father, me, and my Son have some genetic predisposition that leads to all the Good Junk we are/were burdened with. And it all gets handed down. My Grandsons will really be in trouble unless they build a barn like I did.

Regards, Terry King
...In The Woods in Vermont, USA

My usual email SIG: The One who dies with the Most Parts LOSES. What do you need?

I MEAN it.

do you mind if I ask why you prefer it that way?

Not at all. The problem with not being explicit is then trying to remember when you need to be. For instance, the strcmp() function returns 0 when the strings match. One could imagine

if(strcmp(someArray, "DoTheyMatch"))
{
   // Yes, they do
}

But, that's wrong, because strcmp() returns +1, 0, or -1, depending on the order of the strings. If you always use explicit comparisons, you don't need to remember does 0 mean true or does it mean false. Make it explicit, and it doesn't matter.

In the NO_KEY case, to do an implicit comparison, you need to know that NO_KEY is defined as 0. It could as easily be defined as -1. Or 47. Relying on a #define not changing is risky, in my humble opinion.

I say that because I've changed the value that a #define'd name had, on more than one occasion.

terryking228:
Hi. Slightly OT but:

Went to a Hamfest last week and found a matrix keypad for $2

You went to a Hamfest and only got a $2 item???

He didn't say 'only'. It may have been one of his many purchases.

But following on from the rest of your post...
A few months ago I found a forum that dealt with recovering parts from old equipment and their possible use. I have fogotten where it was. Do you have any ideas?

Yes Hamfests are dangerous to me and my wallet :slight_smile: Here is an update I meshed together a couple different sketches to finally get this one working. Eventually "lockpin" will be a servo or actuator to lock a drawer or something. I just started teaching myself this stuff to go along with the two degrees I am going after.

/*
||  Simple Password Entry Using Matrix Keypad
||  4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
||
*/


//* is to validate password   
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
int ledpin = 4;
int lockpin = 2;
Password password = Password( "123" );

const byte ROWS = 4; // Four rows
const byte COLS = 3; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'},
};

byte rowPins[ROWS] = {9,8,7,6 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = {12, 11, 10};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.


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

void setup(){
  pinMode(ledpin, OUTPUT);
  pinMode(lockpin, OUTPUT);
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
 char keys = keypad.getKey();
  }
//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
	Serial.print(eKey);
	switch (eKey){
	  case '*': checkPassword(); break;
	  case '#': password.reset(); break;
	  default: password.append(eKey);
     }}}

void checkPassword(){
  if (password.evaluate()){
    Serial.println("Success");
    digitalWrite(ledpin, HIGH);
    digitalWrite(lockpin, HIGH);
    delay(5000);
    digitalWrite(ledpin, LOW);
    digitalWrite(lockpin, LOW);
    //Add code to run if it works
  }else{
  Serial.println("Wrong"); 
  digitalWrite(ledpin, HIGH);   
  delay(500);              
  digitalWrite(ledpin, LOW);   
  delay(500);
  digitalWrite(ledpin, HIGH);   
  delay(500);               
  digitalWrite(ledpin, LOW);    
  delay(500);  
  digitalWrite(ledpin, HIGH);   
  delay(500);              
  digitalWrite(ledpin, LOW);   
  delay(500);
  digitalWrite(ledpin, HIGH);   
  delay(500);               
  digitalWrite(ledpin, LOW);     
  }
}

I would like to find an easier way to blink the led when the wrong code is entered.

Thanks Everyone 73s

Chip KJ4QCV

void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
	Serial.print(eKey);
	switch (eKey){
	  case '*': checkPassword(); break;
	  case '#': password.reset(); break;
	  default: password.append(eKey);
     }}}

There are two coding styles, with respect to the {.

One calls for the curly brace on the same line as the if/while/for statement it goes with, separated by a space. The other calls for the { to be on the next line, indented to match the if/while/for statement it goes with.

Either:

void keypadEvent(KeypadEvent eKey) {

or

void keypadEvent(KeypadEvent eKey)
{

I prefer the latter.

None call for the { to be jammed up against the statement they go with.

There is exactly one style for the }. That is each and every one goes on a line by itself.

Using the Tools + Auto Format menu item before posting code is a nice touch. Using it far more often is a good idea.

I would like to find an easier way to blink the led when the wrong code is entered.

Look at the blink without delay example. Create a function that gets called on every pass through loop. Have that function decide if the LED should be blinking, or not. If it should, have it decide if it is time to change the state. If it is, change the state and record when the change occurred.