Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- // Morse code dictionary for ASCII characters (A-Z, 0-9, space, slash)
- const char* morseTable[38] = {
- ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
- "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
- "..-", "...-", ".--", "-..-", "-.--", "--..", // A-Z
- "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", // 0-9
- " ", "/" // Space and slash
- };
- void setup() {
- pinMode(13, OUTPUT);
- Serial.begin(9600); // Initialize serial communication
- }
- void loop() {
- if (Serial.available() > 0) {
- char input[100];
- int i = 0;
- // Read the ASCII text from serial
- while (Serial.available() > 0 && i < 99) {
- input[i] = Serial.read();
- i++;
- delay(10); // Delay to ensure proper input capture
- }
- input[i] = '\0'; // Null-terminate the string
- // Process each character and flash its Morse code
- for (int j = 0; input[j] != '\0'; j++) {
- flashMorse(input[j]);
- }
- }
- }
- // Function to find Morse code for a character
- const char* getMorse(char c) {
- if (c >= 'A' && c <= 'Z') {
- return morseTable[c - 'A']; // Map uppercase letters
- } else if (c >= 'a' && c <= 'z') {
- return morseTable[c - 'a']; // Map lowercase letters
- } else if (c >= '0' && c <= '9') {
- return morseTable[c - '0' + 26]; // Map digits 0-9
- } else if (c == ' ') {
- return morseTable[36]; // Space
- } else if (c == '/') {
- return morseTable[37]; // Slash
- }
- return ""; // Unknown characters
- }
- // Function to flash Morse code for a character
- void flashMorse(char c) {
- const char* morse = getMorse(c);
- // Print the character and its Morse code
- Serial.print("Character: ");
- Serial.print(c);
- Serial.print(" -> Morse: ");
- Serial.println(morse);
- for (int i = 0; morse[i] != '\0'; i++) {
- if (morse[i] == '.') {
- digitalWrite(13, HIGH);
- delay(200); // Dot duration
- digitalWrite(13, LOW);
- delay(200); // Gap after dot
- } else if (morse[i] == '-') {
- digitalWrite(13, HIGH);
- delay(600); // Dash duration
- digitalWrite(13, LOW);
- delay(200); // Gap after dash
- }
- }
- delay(600); // Gap between characters
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement