I have include my sketch so you get a better idea of what i'm working with.
I have inlcuded you code snippets and called them "fromKeypad" and thay appear in the "void readKeypad()" section
#include <Keypad.h>
#include <LiquidCrystal.h>
int speakerPin= 5;//speaker
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int numRows = 3;
const int numCols = 16;
//--------------------KEYPAD SETUP-----------------------------------------------------------------------------------
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
{
'1','2','3' }
,
{
'4','5','6' }
,
{
'7','8','9' }
,
{
'*','0','#' }
};
byte rowPins[ROWS] = {
25, 24, 23, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
28, 27, 26}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char PIN[6]={
'1','2','3','4','5','6'}; // our secret (!) number
char attempt[6]={
0,0,0,0,0,0}; // used for comparison
int z=0;
byte fromKeypad = 0;
//------------------------------------------------------------------------------------------------------
void setup()
{
lcd.begin (16,2);
lcd.print("Initialising...");
delay(3000);
lcd.clear();
lcd.print("Ready");
pinMode(speakerPin, OUTPUT);
}
//--------------------------PASSCODE SECTION-----------------------------------------------------------------------------
void correctPIN() // do this if correct PIN entered
{
lcd.setCursor(0,0);
playSuccessTone();
lcd.print ("Correct");
delay(1000);
lcd.clear();
lcd.print("Alarm Disabled.");
}
void incorrectPIN() // do this if incorrect PIN entered
{
lcd.setCursor(0,0);
playWarningTone();
lcd.print ("Try again");
delay(1000);
lcd.clear();
lcd.print("Alarm Enabled.");
}
void checkPIN()
{
int correct=0;
for (int q=0; q<6; q++)
{
if (attempt[q]==PIN[q])
{
correct++;
}
}
if (correct==6)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz=0; zz<6; zz++) // wipe attempt
{
attempt[zz]=0;
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY) // only be bothered to do something if a key was pressed
{
lcd.print(key);
fromKeypad++;
lcd.setCursor(fromKeypad-1, 1);
switch(key)
{
case '*':
z=0;
break;
case '#':
delay(100); // for extra debounce
lcd.clear();
checkPIN();
break;
default:
attempt[z]=key;
z++;
// playKeyTone();// play a beep to acknowledge that key pressed
}
}
}
//--------------------------------SOUND SECTION----------------------------------------------
void playKeyTone(){ // beeping sound for key press
int elapsedtime = 0;
while (elapsedtime < 100)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(500);//time chajges pitch
digitalWrite(speakerPin, LOW);
delayMicroseconds(500);//time chajges pitch
elapsedtime++;
}
}
void playWarningTone(){ // long beep for wrong password
int elapsedtime = 0;
while (elapsedtime < 200)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(1000);//time changes pitch
digitalWrite(speakerPin, LOW);
delayMicroseconds(1000);//time changes pitch
elapsedtime++;
}
}
void playSuccessTone(){ // beep for correct password
int elapsedtime = 0;
while (elapsedtime < 250)//time duration
{
digitalWrite(speakerPin,LOW);
delayMicroseconds(500); //time changes pitch
digitalWrite(speakerPin, HIGH);
delayMicroseconds(250);//time changes pitch
elapsedtime++;
}
}
//---------------------------------------------------------------------------------------------
void loop()
{
readKeypad();
}
with your code added heres what happens on the lcd from system reset.
lcd shows "Ready"
if I press #
I get "try again" then 1 sec after
I get "alarm enabled".
Now, if I press the keypad. the first number appears on the first line next to "alarm enebled". it looks like this.
alarm enabled.1
234
so the next time i try to enter a number it now looks like this
alarm enabled.1
234
next time it looks like this.
alarm enabled.1
234
I have noticed that by moving the code snippet around the "void readKeypad(); section affects how the lcd displays the characters?
hope you can make sense of this.