#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LinkedList.h>
int pinOkay = 11;
int pinShort = 10;
int pinLong = 9;
LinkedList<String> list = LinkedList<String>();
bool sPressed = false;
bool lPressed = false;
bool oPressed = false;
LiquidCrystal_I2C lcd(0x27, 16, 2);
struct translate {
const String morse;
const char letter;
};
String input_morse;
translate dictionary[] = {
{ "SL", 'A' },
{ "LSSS", 'B' },
{ "LSLS", 'C' },
{ "LSS", 'D' },
{ "S", 'E' },
{ "SSLS", 'F' },
{ "LLS", 'G' },
{ "SSSS", 'H' },
{ "SS", 'I' },
{ "SLLL", 'J' },
{ "LSL", 'K' },
{ "SLSS", 'L' },
{ "LL", 'M' },
{ "LS", 'N' },
{ "LLL", 'O' },
{ "SLLS", 'P' },
{ "LLSL", 'Q' },
{ "SLS", 'R' },
{ "SSS", 'S' },
{ "L", 'T' },
{ "SSL", 'U' },
{ "SSSL", 'V' },
{ "SLL", 'W' },
{ "LSSL", 'X' },
{ "LSLL", 'Y' },
{ "LLSS", 'Z' }
};
const int dictSize = 26;
void setup() {
pinMode(pinShort, INPUT);
pinMode(pinLong, INPUT);
pinMode(pinOkay, INPUT);
//intro by Alfred
lcd.init();
lcd.backlight();
delay(500);
lcd.setCursor(0, 0);
lcd.print("Greetings!");
lcd.setCursor(0, -1);
delay(500);
lcd.print("I am Alfred.");
delay(1000);
lcd.clear();
delay(1000);
lcd.setCursor(0, 0);
lcd.print("I can translate");
lcd.setCursor(0, -1);
lcd.print("morse code!");
delay(1000);
lcd.clear();
lcd.print("Middle is long,");
lcd.setCursor(0, -1);
lcd.print("right is beep.");
delay(1000);
lcd.clear();
}
void loop() {
int sButtonState = digitalRead(pinShort);
int lButtonState = digitalRead(pinLong);
int oButtonState = digitalRead(pinOkay);
if (sPressed == false){
if (sButtonState == HIGH) {
digitalRead(pinShort);
lcd.print("S");
list.add("S");
delay(300);
sPressed = true;
}
}
if (sButtonState == LOW) {
sPressed = false;
}
if (lPressed == false){
if (lButtonState == HIGH) {
digitalRead(pinLong);
lcd.print("L");
list.add("L");
delay(300);
lPressed = true;
}
}
if (lButtonState == LOW) {
lPressed = false;
}
if (oPressed == false){
if (oButtonState == HIGH) {
digitalRead(pinOkay);
lcd.clear();
lcd.print("Translating!");
delay(500);
lcd.clear();
delay(500);
for (int i = 0; i < list.size(); i++) {
oPressed = true;
input_morse += list.get(i);
}
lcd.println(translate(input_morse));
}
if (oButtonState == LOW) {
oPressed = false;
}
}
}
char translate(String morse) {
for (int i = 0; i < dictSize; i++) {
if (dictionary[i].morse == morse) {
return dictionary[i].letter;
}
}
return("T");
}