#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Incluindo a biblioteca para o LCD
// Definições dos pinos dos botões
const int buttonDot = 2; // Ponto
const int buttonDash = 3; // Traço
const int buttonEndLetter = 4; // Fim de letra
const int buttonSpace = 5; // Espaço
// Configuração do display LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Endereço I2C (0x27), 16 colunas, 2 linhas
// Dicionário Morse
const char* morseCode[26] = {
".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--",
"--.."
};
char currentMorse[100]; // Armazena o código Morse atual
int morseIndex = 0; // Índice para o código Morse
// Variáveis para o debounce
unsigned long lastPressDot = 0;
unsigned long lastPressDash = 0;
unsigned long lastPressEndLetter = 0;
unsigned long lastPressSpace = 0;
const unsigned long debounceDelay = 300; // Tempo de debounce (em milissegundos)
void setup() {
pinMode(buttonDot, INPUT_PULLUP);
pinMode(buttonDash, INPUT_PULLUP);
pinMode(buttonEndLetter, INPUT_PULLUP);
pinMode(buttonSpace, INPUT_PULLUP);
lcd.begin(16, 2); // Inicializa o LCD (16 colunas, 2 linhas)
lcd.backlight(); // Liga o retroiluminamento
lcd.setCursor(0, 0); // Define o cursor na primeira linha
lcd.print("Digite em Morse:");
}
void loop() {
// Verifica os botões com debounce
unsigned long currentMillis = millis();
if (digitalRead(buttonDot) == LOW && currentMillis - lastPressDot > debounceDelay) {
currentMorse[morseIndex++] = '.'; // Adiciona ponto
displayCurrentMorse();
lastPressDot = currentMillis;
}
if (digitalRead(buttonDash) == LOW && currentMillis - lastPressDash > debounceDelay) {
currentMorse[morseIndex++] = '-'; // Adiciona traço
displayCurrentMorse();
lastPressDash = currentMillis;
}
if (digitalRead(buttonEndLetter) == LOW && currentMillis - lastPressEndLetter > debounceDelay) {
currentMorse[morseIndex] = '\0'; // Termina a string
char letter = decodeMorse(currentMorse);
lcd.setCursor(0, 1); // Define o cursor na segunda linha
if (letter != '?') {
lcd.print(letter);
} else {
lcd.print("??"); // Letra não reconhecida
}
morseIndex = 0; // Reseta para a próxima letra
lastPressEndLetter = currentMillis;
}
if (digitalRead(buttonSpace) == LOW && currentMillis - lastPressSpace > debounceDelay) {
lcd.setCursor(0, 1); // Limpa a linha
lcd.print(" ");
morseIndex = 0; // Reseta para a próxima letra
lastPressSpace = currentMillis;
}
}
// Função para exibir o Morse atual no LCD
void displayCurrentMorse() {
lcd.setCursor(0, 0); // Define o cursor na primeira linha
lcd.print(currentMorse);
}
// Função para decodificar o código Morse
char decodeMorse(const char* morse) {
for (int i = 0; i < 26; i++) {
if (strcmp(morse, morseCode[i]) == 0) {
return 'A' + i; // Retorna a letra correspondente
}
}
return '?'; // Letra não reconhecida
}