Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone! My question is same title: How to reslove this problem when typing a password on matrix keypad? When i'am type a password on matrix keypad, is a crating a problem. When ask to enter password on matrix keypad, my password is 8795, but when i'am type a 8795, in screen is shows 888795. It's duplicating a first number. How to reslove this? This is a my code (in C++):
C++
// Written by UKI COMPUTERS (ukicomputers.github.io - github.com/ukicomputers)
// Based on (C) UKI COMPUTERS Self-Protecting Author License 1.0 (https://github.com/ukicomputers/UKI-COMPUTERS-Self-Protecting-Authors-License)

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "lcd_i2c_quotes/lcd_i2c.h"
#include "hardware/timer.h"
#include "./pico_keypad4x4/pico_keypad4x4.c"

void loop();
void open();
void clearData();

LCDI2C lcd(0x27 , 2, 16, 16, 17, 0);

#define sifra 5		

int pos = 0;    

char Data[sifra]; 
char Master[sifra] = "8795"; // ovo je sifra - u navodnicima. podrazumevana sifra je 123456. i mora imati 6 cifre.
// Koristi se operator <7, zato što je šifra MANJA od sedam
int data_count = 0, master_count = 0;

bool Pass_is_good;
char customKey;

uint columns[4] = { 18, 19, 20, 21 };
uint rows[4] = { 10, 11, 12, 13 };
char matrix[16] = {
    '1', '2' , '3', 'A',
    '4', '5' , '6', 'B',
    '7', '8' , '9', 'C',
    '*', '0' , '#', 'D'
};
bool vrata = true;

#define bravica 15

int main()
{
  
  stdio_init_all();
  gpio_init(bravica);
  gpio_set_dir(bravica, GPIO_OUT);

  gpio_put(bravica, 1);
 
  lcd.init();
  lcd.lcd_clear();
  lcd.lcd_set_cursor(0,0);
  lcd.lcd_string("UKI COMPUTERS");
  lcd.lcd_set_cursor(1, 0);
  lcd.lcd_string("--Wait--");
  sleep_ms(3000);
  lcd.lcd_clear();
  loop();
  return 0;

}

void clearData()
{
  while (data_count != 0)
  { 
    Data[data_count--] = 0; 
  }
  return;
}

void Open()
{
  lcd.lcd_clear();
  pico_keypad_init(columns, rows, matrix);
  char customKey;
  lcd.lcd_set_cursor(0, 0);
  lcd.lcd_string(" Enter Password");
  
  while (data_count <5)
  {
    
	  customKey = pico_keypad_get_key();
    
    if (customKey){
		  Data[data_count] = customKey;
		  lcd.lcd_set_cursor(1, data_count);
		  lcd.lcd_string(Data);
		  data_count++;
      sleep_ms(1000);
	  }
    
  }
}


void loop()
{
  if (vrata== 0)
  {
    pico_keypad_init(columns, rows, matrix);
    char customKey;

    customKey = pico_keypad_get_key();
    
    if (customKey == '#')
    {
      lcd.lcd_clear();
      gpio_put(bravica, 1);
      lcd.lcd_string("  Door is close");
      sleep_ms(3000);
      vrata= 1;
    }

    while(1){
      tight_loop_contents();
    }
  }
  else Open();

  if (data_count == sifra - 1)
  {
    if (!strcmp(Data, Master))
    {
      gpio_put(bravica, 0);
	    lcd.lcd_clear();
      lcd.lcd_string("  Door is Open");
      vrata= 0;
	    sleep_ms(10000);
	    gpio_put(bravica, 1);
	    lcd.lcd_clear();
	    lcd.lcd_string("   Door is closed");
	    vrata= 1;
	    clearData();
	    Open();
    }
    else
    {
      lcd.lcd_clear();
      lcd.lcd_set_cursor(0, 0);
      lcd.lcd_string("Wrong password!");
      sleep_ms(1000);
      lcd.lcd_clear();
      
      pico_keypad_init(columns, rows, matrix);
      char customKey;
      lcd.lcd_set_cursor(0, 0);
      lcd.lcd_string(" Enter Password");

      while (data_count <5)
      {
	      customKey = pico_keypad_get_key();
    
        if (customKey){
		    Data[data_count] = customKey;
		    lcd.lcd_set_cursor(1, data_count);
		    lcd.lcd_string(Data);
		    data_count++;
        sleep_ms(1000);
	    }
    }
  }
    clearData();
  }
}



FUN FACT!: I'am when ask some question CodeProject, every time i'am saying "My question is same title: ..." :))).

What I have tried:

I'am tryed converting time_ms interval from 3000 to 1000, and moving a state of this sleep_ms function.
Posted
Updated 15-Nov-21 1:03am

I found the library you're using and where you copied the code from. That would have been helpful to know in your original post.

The library you're using doesn't appear to do any key "debounce", so the code is seeing multiple key presses when you press the button "once".

How do you fix it? Ask the guy who wrote the library. His email address is at the bottom of the README.MD page where you got the code from.
 
Share this answer
 
I´ve not tested it, just found this:
Button debouncer using the Raspberry Pico PIO
Some_RPI-Pico_stuff/Button-debouncer at main · GitJer/Some_RPI-Pico_stuff · GitHub[^]
 
Share this answer
 
Comments
[no name] 5-Nov-21 6:53am    
I'am see, it's works how push button :-).
Debug it. Use a printf function. Is maybe correctly, but on screen isn't showing a correctly. It's not a sloution. If maybe correctly, maybe problem is a set_cursor. That duplicating number on screen.
 
Share this answer
 
Comments
[no name] 15-Nov-21 7:03am    
Yes, on debug is correctly, but some duplicating number on screen:)
Richard Deeming 17-Nov-21 6:40am    
Talking to yourself is the first sign of madness.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900