// HUGO FELIPE DOS SANTOS ROCHA
// https://wokwi.com/projects/421356913315756033
#include <stdio.h>
#include "pico/stdlib.h"
#include <string.h>
#define SET 1
#define RESET 0
#define COMP 4
#define LEDG_PIN 10
#define LEDR_PIN 11
char password1[COMP+1] = {0};
void led_rgb_put(bool r, bool g, bool b){
gpio_put(LEDR_PIN, r);
gpio_put(LEDG_PIN, g);
}
const uint8_t Linha[] = {2, 3, 4, 5};
const uint8_t Coluna[] ={6, 7, 8, 9};
// Mapeamento das teclas em uma matriz 4x4
char teclas[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const uint8_t segment_pins[] = {21, 22, 20, 18, 19, 16, 17};
const uint8_t mux_display_pins[]={28, 27,13,14};
// Mapeamento dos dígitos para os segmentos do display
const uint8_t display[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 0, 0, 1, 1} // 9
};
// configura pinos como saída
void setup(){
for (int i = 0; i < 7; i++) {
gpio_init(segment_pins[i]);
gpio_set_dir(segment_pins[i], GPIO_OUT);
}
for (int i=0; i<4; i++){
gpio_init(mux_display_pins[i]);
gpio_set_dir(mux_display_pins[i], GPIO_OUT);
gpio_put(mux_display_pins[i],true); // Desativa os displays
}
}
// função para mostrar um dígito em um dos display selecionado
void show_digit(uint8_t digits, uint8_t digit){
// desativa os quatro displays em catodo comum
gpio_put(mux_display_pins[0],true);
gpio_put(mux_display_pins[1],true);
gpio_put(mux_display_pins[2],true);
gpio_put(mux_display_pins[3],true);
//Ativa os segmentos referentes ao dígito
for (int i=0; i<7; i++){
gpio_put(segment_pins[i], display[digit][i]);
}
//Ativa o display desejado
gpio_put(mux_display_pins[digits], false);
}
// Inicializando os GPIO do teclado
void init_teclado() {
for (int i = 0; i < 4; i++) {
// Configuração das linhas como saídas
gpio_init(Linha[i]);
gpio_set_dir(Linha[i], GPIO_OUT);
gpio_put(Linha[i],RESET);
// Configuração das colunas como entrada
gpio_init(Coluna[i]);
gpio_set_dir(Coluna[i], GPIO_IN);
gpio_pull_down(Coluna[i]); // força as colunas para zero.
}
}
// Varredura do teclado e retorno da tecla pressionada
char leitura_teclado() {
for (int row = 0; row < 4; row++) {
// Coloca a linha atual em nível alto
gpio_put(Linha[row], SET);
for (int col = 0; col < 4; col++) {
if (gpio_get(Coluna[col])) {
// Espera um tempo para estabilização da tecla pressionada
sleep_ms(50);
while (gpio_get(Coluna[col])); // Espera até a tecla ser liberada
gpio_put(Linha[row], RESET); // Reseta a linha atual
return teclas[row][col];
}
}
// Coloca a linha atual novamente para nível baixo
gpio_put(Linha[row], RESET);
}
return 0;
}
// Função para pegar a senha digitada
void get_senha(char *senha, int tam) {
int index = 0;
while (index < tam) {
//printf("Estou aqui\n");
char key = leitura_teclado();
if (key != 0) {
printf("%c", key); //adicionei para saber qual senha era digitada
senha[index] = key;
show_digit(index, key - '0'); // Exibe o número no display correspondente
sleep_ms(500);
gpio_put(mux_display_pins[index], true); // Desativa o display atual
index=index+1; //funcionou melhor separando
sleep_ms(100);
}
}
senha[tam] = '\0';
}
// Função para comparar duas senhas
bool compare_senhas(const char *password1, const char *password2) {
return strcmp(password1, password2) == 0;
}
// Função para contagem regressiva no display
void countdown_display(uint8_t seconds) {
while (seconds > 0) {
uint8_t dezenas = seconds / 10;
uint8_t unidades = seconds % 10;
for (int j = 0; j < 100; j++) {
show_digit(2, dezenas); // Exibe as dezenas no terceiro display
sleep_ms(5);
show_digit(3, unidades); // Exibe as unidades no quarto display
sleep_ms(5);
}
seconds--;
}
}
//principal do cofre//
int main() {
setup();
stdio_init_all();
init_teclado();
printf("Bem-vindo ao sistema de segurança do cofre!\n");
printf("Cadastre sua senha de 4 digitos para o cofre!\n");
get_senha(password1, COMP);
printf("\nSenha gravada com sucesso!\n");
int cont=0;
bool acesso = 1;
// define LEDs como saída//
gpio_init(LEDR_PIN);
gpio_set_dir(LEDR_PIN, GPIO_OUT);
gpio_init(LEDG_PIN);
gpio_set_dir(LEDG_PIN, GPIO_OUT);
while (acesso) {
printf("Escreva sua senha de 4 digitos!\n");
char input[COMP+1] = {0};
get_senha(input, COMP);
if (compare_senhas(input, password1)) {
printf("\nSenha Correta!\nCofre Aberto!");
acesso = !acesso;
led_rgb_put(false, true, false); // LED verde
}
else{
printf("\nSenha Incorreta. \nAcesso ao cofre negado! \nTente novamente!\n");
cont = cont +1;
led_rgb_put(true, false, false); // LED vermelho
}
sleep_ms(50); // estabilização
if(cont >= 3){
printf("\nTrês tentativas incorretas! \nBloqueado por 5 segundos.\n");
countdown_display(5); // Contagem regressiva de 5 segundos
cont = 0;
}
}
return 0;
}