Keypad issues. Please help!

Hi,
I’m having trouble with my keypad - can anyone please help.
I am using a standard 3*4 keypad (1 - 9, *,0,#) which seems to work fine when connected to multimeter.
Arduino R3. IDE 1.6.8. I am using a slightly modified CustomKeypad code as below.
The plan is to develop a security alarm with a four digit input e.g.“1379” (I wont use that code in the final run!)
On run, the first four entries from the keypad works fine, so I end up with a four digit code. Every number on the keypad is recognised and correct. I am using the # as a return, so if I get the code right at that point it is accepted and I get “well done”.
if I get my numeric code wrong, I get
“try again
attempts left - 2” etc
So far so good.
The problem occurs when I try again, or even if I enter six numbers, the input starts multiplying randomly and I get really spurious results. It also for some reason prints up “-13” after the attempts left bit…
Is the a problem with the Keypad.h library here or what? - Very confused.
Example of serial monitor results at bottom of page.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
int first;
int z;

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup(){
Serial.begin(9600);
z=3;
}

void loop(){
char customKey = customKeypad.getKey();

switch(customKey)
{
for(z=3;z>0;z=z-1){

case ‘0’ … ‘9’: // This keeps collecting the first value until a operator is pressed “±*/”

first = first * 10 + (customKey - ‘0’);
Serial.println(first);
break;

case ‘#’:
Serial.println("#");
if (first!=1379)
{
first = 0;
Serial.println(“try again”);
Serial.print ("Attempts left “);
Serial.print(z);
Serial.print (” ");
if (z<=0){
Serial.print(“LOCKDOWN”);
}
}else{Serial.println(“well done”); }
}
}
}

sample of serial monitor results;

4
45
456
4567

try again
Attempts left 3 -13
-126
-1258
-12577
2
23
232
2323
23235
-29788
5

Please Auto Format your code in the IDE and post it using code tags as described in read this before posting a programming question

auto formatted - sorry don’t know what you mean by “code tags”

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
int first;
int z;

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1’, ‘2’, ‘3’},
{‘4’, ‘5’, ‘6’},
{‘7’, ‘8’, ‘9’},
{’*’, ‘0’, ‘#’}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup() {
Serial.begin(9600);
z = 3;
}

void loop() {
char customKey = customKeypad.getKey();

switch (customKey)
{
for (z = 3; z > 0; z = z - 1) {

case ‘0’ … ‘9’: // This keeps collecting the first value until a operator is pressed “±*/”

first = first * 10 + (customKey - ‘0’);
Serial.println(first);
break;

case ‘#’:
Serial.println("#");
if (first != 1379)
{
first = 0;
Serial.println(“try again”);
Serial.print ("Attempts left “);
Serial.print(z);
Serial.print (” ");
if (z <= 0) {
Serial.print(“LOCKDOWN”);
}
} else {
Serial.println(“well done”);
}
}
}
}

sorry don't know what you mean by "code tags"

Well, that makes it clear that you didn't Read this before posting a programming question ...

Do so now before you embarrass yourself further.

PaulS, Why don't you try to help the newbee rather than being offensive. I did not post this so I could be abused. I genuinely want assistance. thanks.

/

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
int first;
int z;

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1’, ‘2’, ‘3’},
{‘4’, ‘5’, ‘6’},
{‘7’, ‘8’, ‘9’},
{’*’, ‘0’, ‘#’}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup() {
Serial.begin(9600);
z = 3;
}

void loop() {
char customKey = customKeypad.getKey();

switch (customKey)
{
for (z = 3; z > 0; z = z - 1) {

case ‘0’ … ‘9’: // This keeps collecting the first value until a operator is pressed “±*/”

first = first * 10 + (customKey - ‘0’);
Serial.println(first);
break;

case ‘#’:
Serial.println("#");
if (first != 1379)
{
first = 0;
Serial.println(“try again”);
Serial.print ("Attempts left “);
Serial.print(z);
Serial.print (” ");
if (z <= 0) {
Serial.print(“LOCKDOWN”);
}
} else {
Serial.println(“well done”);
}
}
}
}
/

You can't put a for loop inside a switch statement as you have done.... I'm amazed that even compiles.

Regards, Ray L.

Many thanks RayL.
I’m writing this again as I am not sure the reply worked first time and I am also trying to get this “code tags” business right. Seems I have been upsetting lots of people by being a confused newbee.

Anyhow, Ray - thank you. I have removed the ‘For’ loop and replaced it with a simple x=x-1, and then
if(x<=0) to get out. This has simplified things and I am now getting better results. For the purposes of the code I need, this seems to work as I am only relying on a four digit input. I am still getting very odd returns on the sixth digit input it seems to multiply by a spurious number. Strange.
Any advice much appreciated.

Code below;

/* @file CustomKeypad.pde
  || @version 1.0
  || @author Alexander Brevig
  || @contact alexanderbrevig@gmail.com
  ||
  || @description
  || | Demonstrates changing the keypad size and key values.
  || #
  Modified Simon Whitehead April 2016 as exercise to work up security sketch - 3 strikes you're out.

*/
#include <Keypad.h>
int first;
int z;


const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup() {
  Serial.begin(9600);
  z = 3;
}

void loop() {
  char customKey = customKeypad.getKey();
  switch (customKey)
  {
    case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"

      first = first * 10 + (customKey - '0');
      Serial.println(first);
      break;

    case '#':
      Serial.println("#");
      if (first != 1379)
      {
        first = 0;
        z = z - 1;
        Serial.println("try again");
        Serial.print ("Attempts left  ");
        Serial.print(z);
        Serial.print ("  ");
        if (z <= 0) {
          Serial.print("LOCKDOWN");
        }
      } else {
        Serial.println("well done");
      }
  }
}

The results I get look like this;

1
12
123
1234#
try again
attempts left 2 1
14
145
1459
-13675

etc…

I am still getting very odd returns on the sixth digit input it seems to multiply by a spurious number. Strange.

The 6th digit of a 4 digit number?

I would start with printing the value in customKey, if it is not NO_KEY, before the switch statement. Make SURE that the hardware is working perfectly BEFORE trying to use the data you get from it.

Then, I'd print first before modifying it, in the case 0 to 9 statement as well as after the modification. Make sure that the value does not change unexpectedly between key presses and that it does change appropriately, based on the key pressed.

simonmaritime: PaulS, Why don't you try to help the newbee rather than being offensive. I did not post this so I could be abused. I genuinely want assistance. thanks.

Because you obviously don't get the hint do you. We know you read his reply about using code tags. But right after that you post code without them so that if I want to help you I have to do extra work to reformat the code that the forum mangled. Had he been sweeter about saying it would you have started using them and helping us to help you?

Delta_G: Because you obviously don't get the hint do you. We know you read his reply about using code tags. But right after that you post code without them so that if I want to help you I have to do extra work to reformat the code that the forum mangled. Had he been sweeter about saying it would you have started using them and helping us to help you?

Simply because I didn't understand - as demonstrated by the fact that I still did not get it right straight after. I am a fifty something year old who is learning at an alarming rate. I do not need sarcasm to assist me. That is not the way to go. You will see that I now have a better understanding of the use of code tags - but you can bet your bottom dollar i'll mess up again with something else very soon. when I do, I apologise in advance. I do not need anybody giving me negative or demoralising returns for it. sorry. rant over. I am now going to try and progress this little project of mine. cheers.

Not trying to be demoralizing. Just trying to get you to help us help you.

If you'll look at the top of each forum you'll see s post on how to use this forum. It would be nice if you'd give that a look over. It explains code tags and several other things that can help you get better answers faster.

OK Paul, I appreciate your input thanks. I have tried to follow your suggestions as best as I can…
So I tried this;

//* @file CustomKeypad.pde
  || @version 1.0
  || @author Alexander Brevig
  || @contact alexanderbrevig@gmail.com
  ||
  || @description
  || | Demonstrates changing the keypad size and key values.
  || #
  Modified Simon Whitehead April 2016 as exercise to work up security sketch - 3 strikes you're out.

*/
#include <Keypad.h>
int first;
int z;


const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup() {
  Serial.begin(9600);
  z = 3;
}

void loop() {
  char customKey = customKeypad.getKey();
  if (customKey != NO_KEY) {
    Serial.println(customKey);
  }
}
/* switch (customKey)
  {
   case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-"

  Serial.println(first);
     first = first * 10 + (customKey - '0');
     Serial.println(first);
     break;

   case '#':
     Serial.println("#");
     if (first != 1379)
     {
       first = 0;
       z = z - 1;
       Serial.println("try again");
       Serial.print ("Attempts left  ");
       Serial.print(z);
       Serial.print ("  ");
       if (z <= 0) {
         Serial.print("LOCKDOWN");
       }
     } else {
       Serial.println("well done");
     }
  }
  }*/

Obviously i’ve knocked out everything from ‘switch’ onwards.

And got this out of the serial monitor:

1
2
3
4
5
6
7
8
9
*
0

all good - seems the keypad is working fine.

so then I continued to this;

/* @file CustomKeypad.pde
  || @version 1.0
  || @author Alexander Brevig
  || @contact alexanderbrevig@gmail.com
  ||
  || @description
  || | Demonstrates changing the keypad size and key values.
  || #
  Modified Simon Whitehead April 2016 as exercise to work up security sketch - 3 strikes you're out.

*/
#include <Keypad.h>
int first;
int z;


const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad

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

void setup() {
  Serial.begin(9600);
  z = 3;
}

void loop() {
  char customKey = customKeypad.getKey();
//  if (customKey != NO_KEY) {
 //   Serial.println(customKey);
  

 switch (customKey)
  {
   case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-"
first = first * 10 + (customKey-'0');
  Serial.println(first);
  //   first = first * 10 + (customKey - '0');
   //  Serial.println(first);
     break;

   case '#':
     Serial.println("#");
     if (first != 1379)
     {
   //    first = 0;
       z = z - 1;
       Serial.println("try again");
       Serial.print ("Attempts left  ");
       Serial.print(z);
       Serial.print ("  ");
       if (z <= 0) {
         Serial.print("LOCKDOWN");
       }
     } else {
       Serial.println("well done");
     }
  }
  }

This has evolved a bit. I started by adding within the ‘case’ loop;

Serial.println(first);

But just got zeros as a return… of course I did - I needed to define ‘first’.
So I changed it to;

first = customKey;
Serial.println(first);

The serial monitor returned 49 when I pressed 1, 50 when I hit 2 etc.
So next I changed it to;

first = (customKey-‘0’);
Serial.println(first);

Bingo - I am getting 1 when I press 1, 2 for 2 etc. the only odd bit is nothing when I hit *… strange.
Next, I write;

first = first * 10 + (customKey-‘0’);
Serial.println(first);

This is basically what the code wants to be according to the author.
And on the serial monitor I get ;

1
12
123
1234
12345
-7616

Obviously the -7616 follows me hitting the ‘6’.
So it must be the code;
first = first * 10 + (customKey-‘0’);
But why?

If you’re still here and haven’t fallen asleep at my ramblings, please help!

first is an int. The biggest number that will fit in an int is 32767. You can't put 123,456 in an int. It will roll over to negative. If you want 5 or more digits use long or unsigned long.

And there you have it!!!! Happy days. Thank you all - and sorry 'bout the dim witted eejit in the corner. I want to learn, I will get there, it may take some time but my intentions are good! Thanks again.

Now how the hell do I get a smiley face into here? oh, here goes,, :)

I want to learn, I will get there, it may take some time but my intentions are good!

And THAT is why we spend time answering questions.

It isn't for the insults.