#include <stdio.h>
#include "pico/stdlib.h"
// Definições de pinos do teclado matricial
#define ROW1 2
#define ROW2 3
#define ROW3 4
#define ROW4 5
#define COL1 6
#define COL2 7
#define COL3 8
#define COL4 9
// Definições de pinos dos LEDs e buzzer
#define RED_PIN 13
#define GREEN_PIN 11
#define BLUE_PIN 12
#define BUZZER_PIN 19
void init_gpio() {
const uint rows[] = {ROW1, ROW2, ROW3, ROW4};
for (int i = 0; i < 4; i++) {
gpio_init(rows[i]);
gpio_set_dir(rows[i], GPIO_OUT);
gpio_put(rows[i], 1);
}
const uint cols[] = {COL1, COL2, COL3, COL4};
for (int i = 0; i < 4; i++) {
gpio_init(cols[i]);
gpio_set_dir(cols[i], GPIO_IN);
gpio_pull_up(cols[i]);
}
gpio_init(RED_PIN);
gpio_init(GREEN_PIN);
gpio_init(BLUE_PIN);
gpio_set_dir(RED_PIN, GPIO_OUT);
gpio_set_dir(GREEN_PIN, GPIO_OUT);
gpio_set_dir(BLUE_PIN, GPIO_OUT);
gpio_init(BUZZER_PIN);
gpio_set_dir(BUZZER_PIN, GPIO_OUT);
}
char read_keypad() {
const uint rows[] = {ROW1, ROW2, ROW3, ROW4};
const uint cols[] = {COL1, COL2, COL3, COL4};
const char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
for (int i = 0; i < 4; i++) {
gpio_put(rows[i], 0);
for (int j = 0; j < 4; j++) {
if (!gpio_get(cols[j])) {
sleep_ms(50);
if (!gpio_get(cols[j])) {
gpio_put(rows[i], 1);
return keys[i][j];
}
}
}
gpio_put(rows[i], 1);
}
return 0;
}
void control_led_rgb(char key) {
gpio_put(RED_PIN, 0);
gpio_put(GREEN_PIN, 0);
gpio_put(BLUE_PIN, 0);
switch (key) {
case 'A':
gpio_put(GREEN_PIN, 1);
break;
case 'B':
gpio_put(BLUE_PIN, 1);
break;
case 'C':
gpio_put(RED_PIN, 1);
break;
case 'D':
gpio_put(RED_PIN, 1);
gpio_put(GREEN_PIN, 1);
gpio_put(BLUE_PIN, 1);
break;
}
}
void control_buzzer(char key) {
if (key == '#') {
for (int i = 0; i < 100; i++) {
gpio_put(BUZZER_PIN, 1);
sleep_us(500);
gpio_put(BUZZER_PIN, 0);
sleep_us(500);
}
}
}
int main() {
stdio_init_all();
init_gpio();
char last_key = 0;
while (1) {
char key = read_keypad();
if (key && key != last_key) {
printf("Tecla pressionada: %c\n", key);
}
control_led_rgb(key);
control_buzzer(key);
last_key = key;
sleep_ms(50);
}
return 0;
}
Loading
pi-pico-w
pi-pico-w