Skip to content

Instantly share code, notes, and snippets.

@VeggieVampire
Last active February 6, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VeggieVampire/da9ea1b18b9c80bad67eb5644c8b41e5 to your computer and use it in GitHub Desktop.
Save VeggieVampire/da9ea1b18b9c80bad67eb5644c8b41e5 to your computer and use it in GitHub Desktop.
/*
Morse Code Project
This code will loop through a string of characters and convert these to morse code. But first you have to get the password to start the morse code seation.
It will blink two LED lights and play audio on a speaker.
https://www.instructables.com/id/Arduino-Morse-Code/
*/
//**************************************************//
// Type the String to Convert to Morse Code Here //
//**************************************************//
char stringToMorseCode[] = "Do your Best";
// Create variable to define the output pins
int led12 = 12; // blink an led on output 12
int led6 = 6; // blink an led on output 6
int audio8 = 8; // output audio on pin 8
int note = 1200; // music note/pitch
/*
Set the speed of your morse code
Adjust the 'dotlen' length to speed up or slow down your morse code
(all of the other lengths are based on the dotlen)
Here are the ratios code elements:
Dash length = Dot length x 3
Pause between elements = Dot length
(pause between dots and dashes within the character)
Pause between characters = Dot length x 3
Pause between words = Dot length x 7
http://www.nu-ware.com/NuCode%20Help/index.html?m...
*/
int dotLen = 100; // length of the morse code 'dot'
int dashLen = dotLen * 10; // length of the morse code 'dash'
int elemPause = dotLen; // length of the pause between elements of a character
int Spaces = dotLen * 3; // length of the spaces between characters
int wordPause = dotLen * 20; // length of the pause between words
bool PasswordTrue = 0;
// the setup routine runs once when you press reset:
void setup() {
// Serial Monitor @ 115200
Serial.begin(115200);
Serial.println(" Starting at 115200 baud");
Serial.println("<Arduino is ready>");
Serial.println("Scouts first law?");
// initialize the digital pin as an output for LED lights.
pinMode(led12, OUTPUT);
pinMode(led6, OUTPUT);
}
// Create a loop of the letters/words you want to output in morse code (defined in string at top of code)
void loop(){
SerialCheck();
if (PasswordTrue == 1) {
Serial.println("");
Serial.println("Sending Morse Code");
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Set the case to lower case
tmpChar = toLowerCase(tmpChar);
// Call the subroutine to get the morse code equivalent for this character
GetChar(tmpChar);
}
// At the end of the string long pause before looping and starting again
LightsOff(8000);
}else if(PasswordTrue == 0){
}
}
void SerialCheck(){
if(Serial.available()){
String ch;
ch = Serial.readString();
ch.trim();
if(ch=="TRUSTWORTHY"||ch=="trustworthy"){
PasswordTrue = 1;
Serial.println("Current!");
}
else if(ch=="help"||ch=="HELP"){
Serial.println("Scouts!!! TRUSTWORTHY");
}
else if(ch=="hint"||ch=="HINT"){
Serial.println("vugd twqa qf");
}
}
}
// DOT
void MorseDot()
{
digitalWrite(led12, HIGH); // turn the LED on
digitalWrite(led6, HIGH);
tone(audio8, note, dotLen); // start playing a tone
delay(dotLen); // hold in this position
}
// DASH
void MorseDash()
{
digitalWrite(led12, HIGH); // turn the LED on
digitalWrite(led6, HIGH);
tone(audio8, note, dashLen); // start playing a tone
delay(dashLen); // hold in this position
}
// Turn Off
void LightsOff(int delayTime)
{
digitalWrite(led12, LOW); // turn the LED off
digitalWrite(led6, LOW);
noTone(audio8); // stop playing a tone
delay(delayTime); // hold in this position
}
// *** Characters to Morse Code Conversion *** //
void GetChar(char tmpChar)
{
// Take the passed character and use a switch case to find the morse code for that character
switch (tmpChar) {
case 'a':
Serial.print("A");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'b':
Serial.print("B");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'c':
Serial.print("C");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'd':
Serial.print("D");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'e':
Serial.print("E");
MorseDot();
LightsOff(elemPause);
break;
case 'f':
Serial.print("F");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'g':
Serial.print("G");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'h':
Serial.print("H");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'i':
Serial.print("I");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'j':
Serial.print("J");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'k':
Serial.print("K");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'l':
Serial.print("L");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'm':
Serial.print("M");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'n':
Serial.print("N");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'o':
Serial.print("O");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'p':
Serial.print("P");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 'q':
Serial.print("Q");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'r':
Serial.print("R");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 's':
Serial.print("S");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case 't':
Serial.print("T");
MorseDash();
LightsOff(elemPause);
break;
case 'u':
Serial.print("U");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'v':
Serial.print("V");
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'w':
Serial.print("W");
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'x':
Serial.print("X");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'y':
Serial.print("Y");
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'z':
Serial.print("Z");
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
default:
// If a matching character was not found it will default to a blank space
Serial.print(" ");
LightsOff(Spaces);
}
}
/*
Unlicensed Software:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment