#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <Adafruit_FT6206.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
#define RELAY_PIN_SEND 7
#define RELAY_PIN_RECEIVE 6
const String morseAlphabet[26] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN_SEND, OUTPUT);
pinMode(RELAY_PIN_RECEIVE, INPUT_PULLUP);
tft.begin();
tft.setRotation(0);
ts.begin(40);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.println("Enter text using the keyboard:");
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
if (p.z > 200) {
int x = map(p.y, 0, 240, 0, 320);
int y = map(p.x, 0, 320, 0, 240);
char letter = getLetterFromKeyboard(x, y);
sendCharacter(letter);
}
delay(100);
}
if (digitalRead(RELAY_PIN_RECEIVE) == LOW) {
String morseCode = receiveMorseCode();
String text = convertMorseToText(morseCode);
tft.setCursor(0, 40);
tft.print("Received Morse Code: ");
tft.print(morseCode);
tft.setCursor(0, 60);
tft.print("Converted Text: ");
tft.print(text);
delay(2000);
tft.setCursor(0, 40);
tft.println(" ");
tft.setCursor(0, 60);
tft.println(" ");
}
}
char getLetterFromKeyboard(int x, int y) {
// Determine the key based on touch coordinates
char key;
if (y < 80) {
if (x < 60)
key = 'Q';
else if (x < 120)
key = 'W';
else if (x < 180)
key = 'E';
else if (x < 240)
key = 'R';
else if (x < 300)
key = 'T';
else
key = 'Y';
} else if (y < 120) {
if (x < 60)
key = 'A';
else if (x < 120)
key = 'S';
else if (x < 180)
key = 'D';
else if (x < 240)
key = 'F';
else if (x < 300)
key = 'G';
else
key = 'H';
} else if (y < 160) {
if (x < 60)
key = 'Z';
else if (x < 120)
key = 'X';
else if (x < 180)
key = 'C';
else if (x < 240)
key = 'V';
else if (x < 300)
key = 'B';
else
key = 'N';
} else if (y < 200) {
if (x < 60)
key = 'U';
else if (x < 120)
key = 'I';
else if (x < 180)
key = 'O';
else if (x < 240)
key = 'P';
else if (x < 300)
key = 'K';
else
key = 'L';
} else if (y < 240) {
if (x < 60)
key = 'M';
else if (x < 120)
key = 'Y';
else if (x < 180)
key = 'R';
else if (x < 240)
key = 'E';
else if (x < 300)
key = 'X';
else
key = 'V';
} else {
key = ' '; // Space key
}
return key;
}
void sendCharacter(char letter) {
String morseCode = convertTextToMorse(letter);
for (int i = 0; i < morseCode.length(); i++) {
digitalWrite(RELAY_PIN_SEND, HIGH); // Activate relay to send Morse code
delay(getDelayFromMorseSymbol(morseCode[i]));
digitalWrite(RELAY_PIN_SEND, LOW); // Deactivate relay
delay(100); // Inter-symbol gap
}
}
String receiveMorseCode() {
String morseCode = "";
unsigned long startTime = millis();
while (millis() - startTime < 5000) { // Receive Morse code for up to 5 seconds
if (digitalRead(RELAY_PIN_RECEIVE) == HIGH) {
morseCode += ".";
} else {
morseCode += "-";
}
delay(100); // Time between readings
}
return morseCode;
}
String convertTextToMorse(char letter) {
if (isAlpha(letter)) {
letter = toUpperCase(letter);
return morseAlphabet[letter - 'A'];
} else {
return "";
}
}
String convertMorseToText(String morseCode) {
for (int i = 0; i < 26; i++) {
if (morseCode == morseAlphabet[i]) {
return String(char('A' + i));
}
}
return "";
}
int getDelayFromMorseSymbol(char symbol) {
if (symbol == '.') {
return 200; // Duration for a dot
} else if (symbol == '-') {
return 600; //Duration for a dash
} else {
return 0; // Pause between symbols
}
}