Compare output from Keypad and random function

I would like to compare the output from a keypad and the random function( nailed the subject huh?) I wrote (stole) some of the code (most of it) and came up with the creation below. I have a nano like chip, a 2102 display and a usb cable. The code actually works to a degree… it pulls an almost random number and then allows you to type one into the key pad… then it prints them out. That took 2 days. Now when I compare the numbers, they can be the same or different; it does not matter. The code always reports that the x is lower than y. Now, there is a point where I print both answers to the lcd screen and when it prints the two codes, an extra character appears. ie the display will say 2102x2102. “x” is the weird little character and that character is never the same. So, with that being said… here is some code… I know that the numbers are the same but… they don’t compare well. Also… any thoughts as to what that little random character is?

one more thing, I just want a Hi or Low guessing game. The nano makes up a 4 character code and then you guess it and get “hi” or “low” or “correct” back. I don’t want to make a better mouse trap… just want this part of my life to be over… oh… this is part of a ammo can geocache. FYI

#include “Keypad.h”
#include “Wire.h” // for I2C LCD
#include “LiquidCrystal_I2C.h” // for I2C bus LCD module
#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int pinx = 0;
int piny = 0;
char PINa[5];
char PINb[5];
char z = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin,
D4_pin, D5_pin, D6_pin, D7_pin);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{ {‘1’, ‘4’, ‘7’, ‘#’},
{‘2’, ‘5’, ‘8’, ‘0’},
{‘3’, ‘6’, ‘9’, ‘*’},
{‘1’, ‘2’, ‘3’, ‘4’}
};
byte rowPins[ROWS] = {
5, 4, 3, 2
}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
9, 8, 7, 6
}; // connect to the column pinouts of the keypad
char count = 0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins,
ROWS, COLS );

void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
lcd.begin(16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear();
delay(1000);

}
void loop()
{
if (pinx == 0) // setting up the pin
{
lcd.print(“I am choosing…”);
delay(1000);
// char PINa[4];
// PINa[0] = random(1, 10);
// PINa[1] = random(0, 10);
// PINa[2] = random(0, 10);
// PINa[3] = random(0, 10);
// pinx = PINa[0] * 1000 + PINa[1] * 100 + PINa[2] * 10 + PINa[3];
pinx = random(1000, 10000);
lcd.clear();
lcd.print(pinx);
delay(1000);
lcd.clear();
}

char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
PINb[z] = key;
z++;
switch (key)
{
case ‘*’:
z = 0;
break;
case ‘#’:
z = 0;
break;
}
if (z == 4)
{
char piny = PINb[0] * 1000 + PINb[1] * 100 + PINb[2] * 10 + PINb[3];
lcd.print(piny);

lcd.print(pinx);
// lcd.print(PINb[0]);
// lcd.print(PINb[1]);
// lcd.print(PINb[2]);
// lcd.print(PINb[3]);
delay(2000);

char l = pinx;
char m = piny;
if (l == m)
{ lcd.print(“yes”);
delay(2000);
}

if (l > m)
{ lcd.print(“high”);
delay(2000);
lcd.clear();
}
if (l < m)
{
lcd.print(“low”);
delay(2000);
lcd.clear();
}

z = 0;
}

}
}

Something like below is wrong

char piny = PINb[0] * 1000 + PINb[1] * 100 + PINb[2] * 10 + PINb[3];

A char is a one byte variable and can contain values from 0 to 255 (or -128 to +127); it depends on interpretation and is (for now) outside the scope of the reply.

The result of your calculation will be truncated to a char. Change the type of piny to an int (the same type as pinx) and it can store a value up to 32768.

I have not read your whole program because it has a fatal flaw in the section where you read the keyboard.   char key = keypad.getKey();read a char into the key variable, not a digit. For instance, when the user presses 2 then the key variable will contain the character 2 not the number 2. To correct this subtract '0' from the value read before you use it, perhaps like this

    PINb[z] = key - '0';

Ok, here is the code that works, once I found the “subtract 48 from the key value” trick, things quickly moved into place. The code pulls a random 4 digit number. The code then pulls a number of guesses and does a compare. Later, back at the ranch, the guesses will foster a correct answer and will light up a pin. (set it to high) that pin sets off either a relay or an electroswitch.

#include "Keypad.h"
#include "Wire.h" // for I2C LCD
#include "LiquidCrystal_I2C.h" // for I2C bus LCD module
#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7


// Many thanks to all the people whose code I observed and borrowed from
int pinx = 0;
int piny = 0;
char PINa[5];
int PINb[5];
char z = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin,
                     D4_pin, D5_pin, D6_pin, D7_pin);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{ {'1', '4', '7', '#'},
 {'2', '5', '8', '0'},
 {'3', '6', '9', '*'},
 {'1', '2', '3', '4'}
};
byte rowPins[ROWS] = { 5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 9, 8, 7, 6}; // connect to the column pinouts of the keypad
char count = 0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
 pinMode(15, OUTPUT);
 digitalWrite(15, LOW);
 Serial.begin(9600);
 randomSeed(analogRead(0));
 lcd.begin(16, 2);
 lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
 lcd.setBacklight(HIGH);
 lcd.clear();
 delay(1000);
}
void loop()
{
 if (pinx == 0) // setting up the pin
 {
   lcd.print("This GEOCACHE");
   lcd.setCursor(1, 1);
   lcd.print("was placed by ");
   lcd.setCursor(0, 1);
   delay(5000);
   lcd.clear();
   lcd.print("CACHETEAM6");
   lcd.setCursor(1, 1);
   lcd.print("305-");
   lcd.setCursor(0, 1);
   delay(4000);
   lcd.clear();
   lcd.print("I am choosing a ");
   lcd.setCursor(1, 1);
   lcd.print("4 digit code");
   lcd.setCursor(0, 1);
   delay(3000);
   lcd.clear();
   lcd.print("from 1000 ");
   lcd.setCursor(1, 1);
   lcd.print("to 9999");
   lcd.setCursor(0, 1);
   delay(3000);
   lcd.clear();
   lcd.print("can you guess");
   lcd.setCursor(1, 1);
   lcd.print("what it is?");
   lcd.setCursor(0, 1);
   delay(3000);
   pinx = random(1000, 10000);
   lcd.clear();
 }
 char key = keypad.getKey();
 if (key != NO_KEY)
 {
   lcd.print(key);
   PINb[z] = key - 48;
   z++;
   if (z == 4)
   {
     int piny = PINb[0] * 1000 + PINb[1] * 100 + PINb[2] * 10 + PINb[3];
     delay(1000);
     int l = pinx;
     int m = piny;
     piny = 0;
     PINb[0] = 0;
     PINb[1] = 0;
     PINb[2] = 0;
     PINb[3] = 0;
     if (l == m)
     { lcd.clear();
       digitalWrite(15, HIGH);
       lcd.print("THE LOCK IS OPEN");
       lcd.setCursor(1, 1);
       lcd.print("PULL THE LATCH");
       delay(2000);
     }
     if (l < m)
     { lcd.print("  Too HIGH");
       delay(2000);
       lcd.clear();
       int l = 0;
       int m = 0;
     }
     if (l > m)
     {
       lcd.print("  Too LOW");
       delay(2000);
       lcd.clear();
       char l = 0;
       char m = 0;
     }
     if (m == 77) //super secret code
     {lcd.clear();
       digitalWrite(15, HIGH);
       lcd.print("THE LOCK IS OPEN");
       lcd.setCursor(1, 1);
       lcd.print("PULL THE LATCH");
       delay(2000);
     }
     z = 0;
   }
 }
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R

@glharrisjr:

Welcome to the Forum. Please read the two posts by Nick Gammon at the top of the Forum for guidelines on posting here, especially the use of code tags when posting source code. It will help us help you.