I'm trying to use a keypad library to create a simon puzzle, I had previously made a sketch for a 3x3 keypad for a simon game and got it working. I want to change it to a a single row of numbered buttons. I tried chaning the pins to duplicates of themselves. so the row and column have the same pin which would be a single button. When the code starts the serial monitor outputs
" Locked!
Matched Key=1 at position=0"
then when i touch 5V to pin3 it says
" 1Wrong key
Matched Key=1 at position=0"
Here is my modified code
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x26,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
// Define the states for the lock state machine
#define LOCKED 2
#define UNLOCKED 0
// State Variables: Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;
// Define your password key sequence here
//char* secretCode = "123456789";
char* secretCode = "123456789";
// Keypad key matrix:
const byte rows = 9;
const byte cols = 9;
char keys[rows][cols] = {{'1','2','3','4','5','6','7','8','9'},{'1','2','3','4','5','6','7','8','9'}};
// Keypad pin definitions
byte rowPins[rows] = {3,4,5,6,7,8,9,10,11};
byte colPins[cols] = {3,4,5,6,7,8,9,10,11};
// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
void setup()
{
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("LCD TEST");
Serial.begin(9600);
setLockState(LOCKED);
lcd.print("Simon begin");
lcd.clear();
}
void loop()
{
if (LockState == LOCKED)
{
lcd.clear();
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("LOCKED 123456789");
}
else if (LockState == UNLOCKED)
{
lcd.clear();
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("UNLOCKED 123456789");
}
// Run the state machine:
// Locked State - Monitor keypad for valid Password code entry
if (LockState == LOCKED)
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLockState(LOCKED);
}
if (key != 0)
{
if (key == secretCode[position]) // Valid key in Password sequence
{
Serial.print("Matched Key=");
Serial.print(key);
Serial.print(" at position=");
Serial.println(position);
position ++;
}
else // Invalid key - start all over again
{
Serial.print(key );
Serial.println("Wrong key");
lcd.clear();
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("Wrong key");
lcd.setCursor(0,1); //Set cursor to character 2 on line 0
lcd.print("Key entered ");
lcd.setCursor(13,1); //Set cursor to character 2 on line 0
lcd.print(key);
position = 0;
}
}
if (position == 9) // Password successfully entered - advance state
{
setLockState(UNLOCKED);
position = 0;
}
delay(100);
}
// UNLOCKED state - hold the solenoid open for a limited time
else if (LockState == UNLOCKED)
{
for (int i = 0; i < 30; i++)
{
// Flash the led to indicate the lock is open
//digitalWrite(LedPin, LOW);
delay(50);
//digitalWrite(LedPin, HIGH);
delay(50);
}
setLockState (LOCKED); // Time-out - go back to the locked state.
}
}
// Set the state and the time of the state change
void setLockState(int state)
{
LockState = state;
StartTime = millis ();
if (state == LOCKED)
{
Serial.println("Locked!");
lcd.print("locked! 123456789");
}
else if (state == UNLOCKED)
{
Serial.println("Unlocked!");
lcd.print("Unlocked! 123456789");
}
}
[Maze V1B1.ino|attachment](upload://pgTdwOUXeikNCDO7YQHqxCtt2KY.ino) (3.5 KB)
Update: New issue. How do i check a digit that is 2 digits long ? Like the number 10 how do i make it read 10 as 10 and not as 1 and then 0 ?