#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
// 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', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
// Initialize the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// OLED display configuration
#define OLED_ADDRESS 0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1);
/// 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() {
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println("Enter text:");
display.display();
// Set up the relay pins
pinMode(10, OUTPUT); // Relay for sending Morse code
pinMode(12, INPUT); // Relay for receiving Morse code
}
void loop() {
// Handle keypad input
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Convert and send Morse code
sendMorseCode(buffer);
display.print("Morse: ");
display.println(buffer);
display.display();
} else if (key == '*') {
// Convert received Morse code
char receivedMorse[100];
receiveMorseCode(receivedMorse);
display.print("Received: ");
display.println(receivedMorse);
display.display();
char receivedText[100];
convertToText(receivedMorse, receivedText);
display.print("Text: ");
display.println(receivedText);
display.display();
} else {
// Add the key to the buffer
buffer[bufferIndex++] = key;
buffer[bufferIndex] = '\0';
display.print(key);
display.display();
}
}
}
// Function to send Morse code using a relay
void sendMorseCode(const char* text) {
//digitalWrite(13, HIGH);//
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(1400);
}
// Pause between characters
delay(200);
}
}
//digitalWrite(sendRelayPin, LOW);//
//}//
// Function to send a single Morse code character
void sendMorseChar(const char* morseChar) {
for (int i = 0; morseChar[i] != '\0'; i++) {
if (morseCode[i] == '.') {
digitalWrite(10, HIGH); // activate the relay
delay(200); digitalWrite(14, LOW); // deactivate the relay
} else if (morseChar[i] == '-') {
digitalWrite(10, HIGH); // activate the relay
delay(600);
digitalWrite(10, LOW); // deactivate the relay
}
delay(200); // Pause between dots and dashes
}
}
// Function to receive Morse code using a relay
void receiveMorseCode(char* receivedMorse) {
int index = 0;
while (digitalRead(12) == LOW) {
// Wait for the relay to activate
}
unsigned long startTime = millis();
while (digitalRead(12) == 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* morseCode, char* 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';
}
void clearDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Enter text:");
display.display();
}