Created
October 10, 2023 11:50
-
-
Save MitchiLaser/498c243de9b82157b53e6ecad14ec832 to your computer and use it in GitHub Desktop.
Arduino Morse code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Arduino Morse message | |
* | |
* (c) Michael Hohenstein | |
* MIT License | |
* | |
* This code takes the string <Text> and translates it into morse code. | |
* When an LED is attached at Pin <LedPin> , the LED blinks to emit the | |
* message in Morse code. | |
*/ | |
// the pin where the LED is attached | |
#define LedPin 13 | |
// optional serial debugging, turned off by defaule | |
#define serial false | |
// define all delays in milliseconds | |
#define shortDelay 500 // time of a '.' | |
#define longDelay 2*shortDelay // time of a '-' | |
#define spaceDelay 3*shortDelay // space between | |
#define textEndDelay 5*longDelay // space after the whole string | |
// change the text to whatever you want and dont forget to adjust the 'TextLength' | |
const char Text[] = "Example text"; | |
const char TextLength = 12; | |
const char MorseTable[][5] = { | |
// A to I | |
".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", | |
// J to R | |
".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", | |
// S to Z | |
"... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", | |
// 0 to 9 | |
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." | |
}; | |
void setup() { | |
// All unused pins as High-Ohm Pull-down | |
for (int i = 0; i < 8; i++) { | |
pinMode(i, INPUT); | |
digitalWrite(i, LOW); | |
} | |
// Pin where the LED should be driven as output | |
pinMode(LedPin, OUTPUT); | |
digitalWrite(LedPin, LOW); | |
if (serial) { | |
Serial.begin(9600); | |
} | |
} | |
// Blink -> takes True or False as an Argument | |
// |_ True » it is a '-' character | |
// \_ False | it is a '.' character | |
void Blink(bool state) { | |
digitalWrite(LedPin, HIGH); | |
if (state) { // long caracter | |
delay(longDelay); | |
} | |
else { // short caracter | |
delay(shortDelay); | |
} | |
digitalWrite(LedPin, LOW); | |
delay(shortDelay); | |
} | |
void loop() { | |
// output all letters of the text sequentially | |
for (int i = 0; i < TextLength; i++) { | |
// select the letter that is going to be displayed | |
char Letter = Text[i]; | |
if (serial) { | |
Serial.print(Letter); | |
Serial.print(" "); | |
} | |
// get the Morse Translation of that letter | |
// space | |
if (Letter == 32) { | |
if (serial) { | |
Serial.println(" "); | |
} | |
delay(spaceDelay); | |
continue; | |
} | |
// uppercase letters | |
if (Letter > 64 && Letter < 91) | |
Letter -= 65; // uppercase letters begin at index 65 in the ASCII table | |
// lowercase letters | |
if (Letter > 96 && Letter < 123) | |
Letter -= 97; // lowercase letters begin at index 97 in the ASCII table | |
// numbers | |
if (Letter > 47 && Letter < 58) | |
Letter -= 21; // numbers begin at Array-index 27 after the letters. | |
// error | |
if (Letter > 36) { | |
for (int j = 0; j < 6; j++) { | |
Blink(false); // 6 times long blinking is an ERROR | |
} | |
break; | |
} | |
// convert the letter using the implemented morse table | |
char Word[5] = {""}; | |
for (int j = 0; j < 5; j++) { | |
Word[j] = MorseTable[Letter][j]; | |
} | |
// now display the word on the LED | |
for (int j = 0; j < 5 && Word[j] != 32; j++) { | |
if (serial) { | |
Serial.print(Word[j]); | |
} | |
if (Word[j] == 46) { | |
Blink(false); | |
} | |
else { | |
Blink(true); | |
} | |
} | |
// end of the word | |
if (serial) { | |
Serial.println(""); | |
} | |
delay(longDelay); | |
} | |
// end of the whole Text | |
if (serial) { | |
Serial.println("\n"); | |
} | |
delay(textEndDelay); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment