Issue with input and display password

Hey there all,

I'm pretty new to arduino and c++ programming, please forgive me if this is in the wrong place.

I am having an issue when trying to input a code with the buttons on an ebay LCD keypad shield on a "Funduino" mega

The program is designed to sit idle until the select key is pressed. Once that happens, it prompts for an 8 character code, then displays the code back and moves on to the next function.

The issue I'm having is that when I enter any key, it immediately populates the code with that key, and displays some odd character afterwards.

I have modified a different project to suit my needs, and it works mostly to my satisfaction except for this.
Any help is appreciated,
Thanks!

if (button == btnSELECT)
{
  tone(52, 880, 90);
  lcd.clear();
  lcd.setCursor(0,0);
  lastDebounceTime = millis(); //set the current time
    lcd.print("Enter Code");
  while (currentLength < 8)
 
  {
    lcd.setCursor(currentLength + 4, 1);
    lcd.cursor();
   
    int button = read_LCD_buttons();
   if ( (millis() - lastDebounceTime) > debounceDelay)
   
    button == btnNONE;
    if (button != btnNONE)
    {
      tone(52, 880, 90);
      if ((button != btnSELECT))
      {
      lcd.print(button);
      password[currentLength] = button;
      currentLength++;
      lastDebounceTime = millis(); //set the current time
      }
    }
  }
 
  if (currentLength == 8)
  {
    delay(1000);
    lcd.noCursor();
    lcd.clear();
    lcd.home();
    lcd.print("Entered: ");
    lcd.setCursor(4,1);
    tone(52, 880, 200);
    delay(300);
    tone(52, 660, 200);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    lcd.print(password[4]);
    lcd.print(password[5]);
    lcd.print(password[6]);
    lcd.print(password[7]);
    tone(1319, 200);
    delay(3000);
    lcd.clear();
    currentLength = 0;
    lcd.print("Click on '5'");
    lcd.clear();
    }

EDIT Pics of Password error

I tried to have a look at your code.

It is hard to understand what is going on.

I have to suggestions:

  1. spend some time in the formatting of your code
  2. place more of your code. and add some comment

Things that may be your problem:

  1. what is the intial value of currentLength? maybe it is not zero
  2. debounceDelay may not be large enough. try something ridiculously long. I would try it with 1000, to make sure the logic in my code is correct or not.

Nothing else comes to my mind at the moment.

Sorry, here is the full code.

I did not write it completely, just modified and added what I needed.

#include <Wire.h>
#include <LiquidCrystal.h>


#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
#define pound 14
byte puste[8] = {//We create the symbol shown under "loading"
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

int adc_key_in = 0;
int Scount = 0; // second valu
int Mcount = 5; // minute value
int Hcount = 0; // hour value 
int EventTimer = 0; // setting time to 0
 
long secMillis = 0; // store last time for second add
long interval = 1000; // interval for seconds
 
char password[8]; // password length
int currentLength = 0; //defines which number we are currently writing
int i = 0;
char entered[8];
 
int ledPin = 22; //red LED
//int ledPin2 = 51; //yellow led
//int ledPin3 = 49; //green led
 
int event = 45;//connect relay here
 
int wire1 = 36;//cord 1
int wire2 = 51;//cord 2
//int wire3 = 4;//cord 3
//int wire4 = 5;//cord 4
//int wire5 = 6;//cord 5
//int wire6 = 7;//cord 6
 
int wire1State = 0;// status wire 1
int wire2State = 0;// status wire 2
//int wire3State = 0;// status wire 3
//int wire4State = 0;// status wire 4
//int wire5State = 0;// status wire 5
//int wire6State = 0;// status wire 6
// initialize the library with the numbers of the interface pins
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT; 
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;  
  return btnNONE;  // when all others fail, return this...

}
// Left in for future use
/*const byte ROWS = 4; //4 Rows 
const byte COLS = 4; //4 columns 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
 
byte rowPins[ROWS] = {A7, A6, A5, A4};
byte colPins[COLS] = {A3, A2, A1, A0};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 */
////////////////////////////////////////////////////////////
void setup(){
  ////////////////////////////////////////////////////////////
lcd.createChar(0, puste);
  pinMode(ledPin, OUTPUT); // sets the digital pin as output
  //pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  //pinMode(ledPin3, OUTPUT); // sets the digital pin as output
  pinMode(wire1, INPUT);
  pinMode(wire2, INPUT);
  //pinMode(wire3, INPUT);
  //pinMode(wire4, INPUT);
  //pinMode(wire5, INPUT);
  //pinMode(wire6, INPUT);
  pinMode (event, OUTPUT);
  digitalWrite (event, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("OP HB V.1");
   lcd.setCursor(0,1);
  lcd.print("By kuro");
  delay(5000);
  lcd.clear();
   lcd.setCursor(3,0);
 lcd.print("Loading");
 lcd.setCursor(0,1);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 delay(500);
 lcd.write(8);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Enter Time:");
 lcd.setCursor(0,1);
 ustawienie();
 
   
}
void loop()////////////////////////////////////////////////////////
{
 
   int button = read_LCD_buttons(); // get the key
   //filter out any noise by setting a time buffer
  if ( (millis() - lastDebounceTime) > debounceDelay) 
 {
  if (button == btnUP)
    {
      tone(52, 880, 90);
      Mcount ++;
      ustawienie();
      lastDebounceTime = millis(); //set the current time
      }
     
    if (Mcount > 58){
      tone(52, 880, 90);
    Mcount = 0;
   Hcount ++;
     }
/////////////////////////////////////////
  if (button == btnDOWN)
    {
      tone(52, 880, 90);
      Mcount--;
     ustawienie();
     lastDebounceTime = millis(); //set the current time
 
      }
     
     if (Mcount < 0){
      tone(52, 880, 90);
     Hcount --;
     Mcount = 58;
    }
/////////////////////////////////////////
/*  if (key1 == '3')
    {
      tone(52, 880, 90);
      Scount ++;
      ustawienie();
      }
     
    if (Scount > 58){
      tone(52, 880, 90);
    Mcount ++;
   Scount = 0;
     }
/////////////////////////////////////////
  if (key1 == '9')
    {
      tone(52, 880, 90);
      Scount--;
     ustawienie();
 
      }
     
     if (Scount < 0){
      tone(52, 880, 90);
     Mcount --;
     Scount = 58;
    }
*/
/////////////////////////////////////////
}

Continued in next post

EDIT maybe not, it won't let me post all the code, how do I post all of it without using more than the character limit?

forum_post_reply.ino (5.18 KB)

I think the password works fine just it does not show correctly...
According to my opinion, as the input password type is character and the matching password is also character type so when you entered the right code, your code matched and it works as your demand but when you display it, its type is in integer and it shows in convertible form as ASCII...
So if you want it to display correctly then you have to first convert it into ASCII form before print or you can just print it in hidden form i.e. ****** the easy way just like the other passwords....