#include <Arduino.h>
#include <string.h>
#define BUTTON_PIN 2
#define INTERVALO 10
#define TEMPO_PONTO 200
#define TEMPO_FIM 700 // tempo sem pressionar para considerar fim da letra
unsigned long ultimoTempo = 0;
int tempoPressionado = 0;
int tempoSemPressao = 0;
bool estadoAnterior = false;
char morse[10];
int idx = 0;
// ================= DECODER =================
char decodeMorse(char *code) {
if (strcmp(code, ".-") == 0) return 'A';
if (strcmp(code, "-...") == 0) return 'B';
if (strcmp(code, "-.-.") == 0) return 'C';
if (strcmp(code, "-..") == 0) return 'D';
if (strcmp(code, ".") == 0) return 'E';
if (strcmp(code, "..-.") == 0) return 'F';
if (strcmp(code, "--.") == 0) return 'G';
if (strcmp(code, "....") == 0) return 'H';
if (strcmp(code, "..") == 0) return 'I';
if (strcmp(code, ".---") == 0) return 'J';
if (strcmp(code, "-.-") == 0) return 'K';
if (strcmp(code, ".-..") == 0) return 'L';
if (strcmp(code, "--") == 0) return 'M';
if (strcmp(code, "-.") == 0) return 'N';
if (strcmp(code, "---") == 0) return 'O';
if (strcmp(code, ".--.") == 0) return 'P';
if (strcmp(code, "--.-") == 0) return 'Q';
if (strcmp(code, ".-.") == 0) return 'R';
if (strcmp(code, "...") == 0) return 'S';
if (strcmp(code, "-") == 0) return 'T';
if (strcmp(code, "..-") == 0) return 'U';
if (strcmp(code, "...-") == 0) return 'V';
if (strcmp(code, ".--") == 0) return 'W';
if (strcmp(code, "-..-") == 0) return 'X';
if (strcmp(code, "-.--") == 0) return 'Y';
if (strcmp(code, "--..") == 0) return 'Z';
return '?';
}
// ================= SETUP =================
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("=== DETECTOR MORSE ===");
}
// ================= LOOP =================
void loop() {
// 🔥 TIMER DE 10 ms (simulado)
if (millis() - ultimoTempo >= INTERVALO) {
ultimoTempo = millis();
bool botao = digitalRead(BUTTON_PIN) == LOW;
// pressionando
if (botao) {
tempoPressionado += 10;
tempoSemPressao = 0;
}
// soltou botão
if (!botao && estadoAnterior) {
if (tempoPressionado <= TEMPO_PONTO) {
if (idx < 9) morse[idx++] = '.';
Serial.print(".");
} else {
if (idx < 9) morse[idx++] = '-';
Serial.print("-");
}
tempoPressionado = 0;
}
// sem pressionar → detectar fim da sequência
if (!botao) {
tempoSemPressao += 10;
// ⏳ FIM DA LETRA (sequência)
if (tempoSemPressao > TEMPO_FIM && idx > 0) {
morse[idx] = '\0';
char letra = decodeMorse(morse);
Serial.print(" -> ");
Serial.println(letra);
idx = 0;
}
}
estadoAnterior = botao;
}
}Loading
st-nucleo-l031k6
st-nucleo-l031k6