/*
https://forum.arduino.cc/t/morse-code-transmitter-project-not-working-buttons-lcd-issue/1264432
*/
#include <LiquidCrystal.h>
// Pin definitions
const int ledPin = 13;
const int buttonDotDashPin = 7;
const int buttonEnterClearPin = 9;
const int backlightPin = 6;
const int backlightContrast = 20;
// Create the LCD object
//rs,en,d4,d5,d6,d7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Morse code timings (in milliseconds)
const int dotDuration = 100;
const int dashDuration = 300;
const int gapDuration = 500;//missing??
const int debounceDelay = 50;
const int enterDuration = 100;
const int clearDuration = 300;
const int resetDuration = 600;
// Variables to store Morse code
String morseBuffer = "";
bool morseMode = false;
void setup() {
Serial.begin(115200);
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" 9/11");
// Set up the pins
pinMode(ledPin, OUTPUT);
pinMode(buttonDotDashPin, INPUT_PULLUP);
pinMode(buttonEnterClearPin, INPUT_PULLUP);
// Set LCD backlight contrast
analogWrite(backlightPin, backlightContrast);
}
void loop() {
// Read button states
bool buttonDotDashState = digitalRead(buttonDotDashPin) == LOW;
bool buttonEnterClearState = digitalRead(buttonEnterClearPin) == LOW;
// Enter Morse mode with a long press on button 1
if (!morseMode && buttonDotDashState) {
long pressStartTime = millis();
while (digitalRead(buttonDotDashPin) == LOW) {
if (millis() - pressStartTime > 1000) {
morseMode = true;
lcd.clear();
lcd.print("Morse Mode");
delay(1000); // Wait before starting Morse entry
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code:");
lcd.setCursor(0, 1);
break;
}
}
}
if (morseMode) {
// Check for dot or dash input
if (buttonDotDashState) {
long pressStartTime = millis();
while (digitalRead(buttonDotDashPin) == LOW); // Wait for release
long pressDuration = millis() - pressStartTime;
if (pressDuration >= dashDuration) {
morseBuffer += '-';
Serial.print("-");
transmitDash();
} else if (pressDuration >= dotDuration) {
morseBuffer += '.';
Serial.print(".");
transmitDot();
}
}
// Check for enter or clear input
if (buttonEnterClearState) {
long pressStartTime = millis();
while (digitalRead(buttonEnterClearPin) == LOW); // Wait for release
long pressDuration = millis() - pressStartTime;
if (pressDuration >= resetDuration) {
lcd.clear();
lcd.print(" 9/11");
morseBuffer = "";
morseMode = false;
} else if (pressDuration >= clearDuration) {
morseBuffer = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(0, 1);
} else if (pressDuration >= enterDuration) {
transmitMorse(morseBuffer);
morseBuffer = "";
}
delay(debounceDelay); // Simple debounce
}
}
}
// Function to display a character from Morse code
void transmitMorse(String morseCode) {
char letter = getLetterFromMorse(morseCode);
lcd.setCursor(0, 1);
lcd.print(letter);
}
// Function to convert Morse code to a letter
char getLetterFromMorse(String morseCode) {
if (morseCode == ".-") return 'A';
if (morseCode == "-...") return 'B';
if (morseCode == "-.-.") return 'C';
if (morseCode == "-..") return 'D';
if (morseCode == ".") return 'E';
if (morseCode == "..-.") return 'F';
if (morseCode == "--.") return 'G';
if (morseCode == "....") return 'H';
if (morseCode == "..") return 'I';
if (morseCode == ".---") return 'J';
if (morseCode == "-.-") return 'K';
if (morseCode == ".-..") return 'L';
if (morseCode == "--") return 'M';
if (morseCode == "-.") return 'N';
if (morseCode == "---") return 'O';
if (morseCode == ".--.") return 'P';
if (morseCode == "--.-") return 'Q';
if (morseCode == ".-.") return 'R';
if (morseCode == "...") return 'S';
if (morseCode == "-") return 'T';
if (morseCode == "..-") return 'U';
if (morseCode == "...-") return 'V';
if (morseCode == ".--") return 'W';
if (morseCode == "-..-") return 'X';
if (morseCode == "-.--") return 'Y';
if (morseCode == "--..") return 'Z';
return '?'; // For unknown codes
}
// Function to blink the LED for a dot
void transmitDot() {
digitalWrite(ledPin, HIGH);
delay(dotDuration);
digitalWrite(ledPin, LOW);
delay(gapDuration);
}
// Function to blink the LED for a dash
void transmitDash() {
digitalWrite(ledPin, HIGH);
delay(dashDuration);
digitalWrite(ledPin, LOW);
delay(gapDuration);
}