#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <Keypad.h>
#include "SPI.h"
#define speaker 12
// Define the keypad layout
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'S'},
{'*', '0', '#', 'O'}
};
byte rowPins[ROWS] = {A2, A3, A4, A5}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {A6, A7, A8, A9}; // connect to the column pinouts of the keypad
// Initialize the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Initialize the LCD
#define TFT_MISO 50
#define TFT_MOSI 51
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST -1 // Or set to -1 and connect to Arduino RESET pin
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC ); //TFT_RST, TFT_CLK, TFT_RST, TFT_MISO, TFT_MOSI);
// Morse code conversion table
const char* morseCode[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"
};
const int numChar = sizeof(morseCode) / sizeof(morseCode[0]);
// Buffer for storing the entered text
char buffer[100];
int bufferIndex = 0;
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(0); // Set the display rotation (change accordingly)
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(1, 0);
tft.println("Enter text:");
// Set up the relay pins
pinMode(14, OUTPUT); // Relay for sending Morse code
pinMode(A15, INPUT); // Relay for receiving Morse code
pinMode(speaker, OUTPUT); // Speaker for play sound
}
void loop() {
// Handle keypad input
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Convert and send Morse code
sendMorseCode(buffer);
tft.print(" MorseSend: ");
tft.println(buffer);
clearBuffer();
} else if (key == '*') {
// Convert received Morse code
char receivedMorse[100];
receiveMorseCode(receivedMorse);
tft.print("Received: ");
tft.println(receivedMorse);
char receivedText[100];
convertToText(receivedMorse, receivedText);
tft.print("Text: ");
tft.println(receivedText);
} else {
// Add the key to the buffer
buffer[bufferIndex++] = key;
buffer[bufferIndex] = '\0';
tft.print(key);
}
}
}
// Function to send Morse code using a relay
void sendMorseCode(const char* text) {
for (int i = 0; text[i] != '\0'; i++) {
char ch = toLowerCase(text[i]);
if (ch >= 'a' && ch <= 'z') {
int index = ch - 'a';
sendMorseChar(morseCode[index]);
} else if (ch >= '0' && ch <= '9') {
int index = ch - '0' + 26;
sendMorseChar(morseCode[index]);
} else if (ch == ' ') {
// Pause between words
delay(700);
}
// Pause between characters
delay(300);
}
}
// Function to send a single Morse code character
void sendMorseChar(const char* morseChar) {
for (int i = 0; morseChar[i] != '\0'; i++) {
if (morseChar[i] == '.') {
digitalWrite(14, HIGH); // activate the relay
digitalWrite(speaker, HIGH);
tone(speaker,850);
delay(200);
digitalWrite(14, LOW); // deactivate the relay
digitalWrite(speaker, LOW);
noTone(speaker);
} else if (morseChar[i] == '-') {
digitalWrite(14, HIGH); // activate the relay
digitalWrite(speaker, HIGH);
tone(speaker,850);
delay(600);
digitalWrite(14, LOW); // deactivate the relay
digitalWrite(speaker, LOW);
noTone(speaker);
}
// Pause between dots and dashes
delay(200);
}
}
// Function to receive Morse code using a relay
void receiveMorseCode(char* receivedMorse) {
int index = 0;
while (digitalRead(A15) == LOW) {
// Wait for the relay to activate
}
unsigned long startTime = millis();
while (digitalRead(A15) == HIGH) {
// Wait for the relay to deactivate
}
unsigned long endTime = millis();
unsigned long duration = endTime - startTime;
if (duration >= 50 && duration <= 300) {
receivedMorse[index++] = '.';
} else if (duration >= 300 && duration <= 900) {
receivedMorse[index++] = '-';
}
receivedMorse[index] = '\0';
}
// Function to convert Morse code back to text
void convertToText(const char* receivedMorse, char* receivedText) {
int index = 0;
char* token = strtok(receivedMorse, " ");
while (token != NULL) {
for (int i = 0; i < numChar; i++) {
if (strcmp(token, morseCode[i]) == 0) {
if (i < 26) {
receivedText[index++] = i + 'A';
} else {
receivedText[index++] = i - 26 + '0';
}
break;
}
}
token = strtok(NULL, " ");
}
receivedText[index] = '\0';
}
// Function to clear the buffer
void clearBuffer() {
for (int i = 0; i < bufferIndex; i++) {
buffer[i] = '\0';
}
bufferIndex = 0;
}