#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);
// Relay configuration
const int sendRelayPin = 10;
const int receiveRelayPin = 11;
// Morse code conversion
const int dotDuration = 200; // Duration of a dot in milliseconds
const char* morseAlphabet[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
const char* morseNumbers[] = {
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
};
void setup() {
pinMode(sendRelayPin, OUTPUT);
pinMode(receiveRelayPin, INPUT);
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();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Convert and send Morse code
char buffer[100];
int bufferIndex = 0;
while (true) {
char inputKey = keypad.getKey();
if (inputKey == '#') {
break;
} else if (inputKey) {
buffer[bufferIndex++] = inputKey;
buffer[bufferIndex] = '\0';
display.print(inputKey);
}
}
display.display();
sendMorseCode(buffer);
clearDisplay();
} else if (key == '*') {
// Receive and convert Morse code
char receivedMorse[100];
receiveMorseCode(receivedMorse);
display.print("Received: ");
display.println(receivedMorse);
char receivedText[100];
convertToText(receivedMorse, receivedText);
display.print("Text: ");
display.println(receivedText);
display.display();
} else {
display.print(key);
display.display();
}
}
}
void sendMorseCode(const char* text) {
digitalWrite(sendRelayPin, HIGH);
for (int i = 0; text[i] != '\0'; i++) {
char character = tolower(text[i]);
if (isAlpha(character)) {
int index = character - 'a';
if (index >= 0 && index < 26) {
const char* morseCode = morseAlphabet[index];
sendMorseSignal(morseCode);
}
} else if (isDigit(character)) {
int index = character - '0';
if (index >= 0 && index < 10) {
const char* morseCode = morseNumbers[index];
sendMorseSignal(morseCode);
}
}
delay(dotDuration * 3); // Pause between characters
}
digitalWrite(sendRelayPin, LOW);
}
void sendMorseSignal(const char* morseCode) {
for (int i = 0; morseCode[i] != '\0'; i++) {
if (morseCode[i] == '.') {
digitalWrite(sendRelayPin, HIGH);
delay(dotDuration);
digitalWrite(sendRelayPin, LOW);
} else if (morseCode[i] == '-') {
digitalWrite(sendRelayPin, HIGH);
delay(dotDuration * 3);
digitalWrite(sendRelayPin, LOW);
}
delay(dotDuration); // Pause between dots and dashes
}
}
void receiveMorseCode(char* receivedMorse) {
digitalWrite(receiveRelayPin, HIGH);
// Code to receive Morse code signals
digitalWrite(receiveRelayPin, LOW);
}
void convertToText(const char* morseCode, char* text) {
// Code to convert Morse code to text
}
void clearDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Enter text:");
display.display();
}