Skip to content

Instantly share code, notes, and snippets.

@HunterVL
Created February 16, 2017 16:08
Show Gist options
  • Select an option

  • Save HunterVL/b14da9fc1b2b1b99f407d8e7e89effc7 to your computer and use it in GitHub Desktop.

Select an option

Save HunterVL/b14da9fc1b2b1b99f407d8e7e89effc7 to your computer and use it in GitHub Desktop.
void beep(String letter){
//Beeps letter
for (int index = 0; index < letter.length(); index++){
//Figures out if char is dot or dash and turns it on and off with varying delay.
switch (letter[index]) {
case '.':
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
break;
case '-':
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
break;
case '/': //Space.
delay(400);
break;
}
delay(100); //Delay between dots and dash
Serial.print(letter[index]);
}
delay(300); //Delay between letters
}
//Creates array with Morse code translations and their corrsponding alpha-numeric symbol.
String morseCode[41] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----.","/","--..--",".-.-.-","---..."};
char keys[41] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', ',', '.', ':'};
void setup() {
//Start Serial and make 13 output power.
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop(){
Serial.flush(); //Clear Serial of existing input.
while (!Serial.available()); //Wait for input.
String message = Serial.readString();
Serial.print(message);
//Run through each letter of message.
for (int letterIndex = 0; letterIndex < message.length(); letterIndex++) {
char letter = toupper(message[letterIndex]); //Make letter uppercase.
for (int location = 0; location < 40; location++){
if (letter == keys[location]) beep(morseCode[location]); //Figure out translation of letter and give it to Beep funtion.
}
}
Serial.print("\n"); //Add newline so next inputs not on same line.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment