Skip to content

Instantly share code, notes, and snippets.

@chukitow
Created October 29, 2014 02:03
Show Gist options
  • Save chukitow/3f36d1e6295d438089a7 to your computer and use it in GitHub Desktop.
Save chukitow/3f36d1e6295d438089a7 to your computer and use it in GitHub Desktop.
int testLed = 13;
int dotDelay = 200;
int hertz = 500000;
int tonePin = 4;
String data;
//define the morse code for the alphabet and numbers
char* letters[] = {
".-", // 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
};
char* numbers[] = {
"-----", // 1
".----", // 2
"..---", // 3
"...--", // 4
"....-", // 5
".....", // 6
"-....", // 7
"--...", // 8
"---..", // 9
"----."
};
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("\n\nString Constructors:");
Serial.println();
pinMode(testLed, OUTPUT);
}
void loop()
{
squenceToMoserse("PENDEJO");
delay(500); // Wait for 1 second
}
//=================================================================
//
// Function: morseCodeSequence
//
// Input: Character Array of Dots and Dashes to be sent
//
// Description:
// This function takes as input an array or "." and "-" and
// calls dotOrDash for each item in the array.
//
// At the end of the sequence, there is a delay of 3 times
// the dot duration.
//=================================================================
void morseCodeSequence(char* sequence)
{
int i = 0;
// Loop for each element in the array
while (sequence[i] != NULL)
{
dotOrDash(sequence[i]); // Send out the dot or dash
i++; // Increment to the next element in the array
}
delay(dotDelay * 3); // gap between letters
}
//=================================================================
//
// Function: dorOrDash
//
// Input: Character that should be either a dot or a dash
//
// Description:
// This function first turns on the output then looks to see
// if the character is a "." and if so delays the dotDelay.
//
// If the character is not a "." then the routine assumes it
// is a "-" and keep the output high for 3 times the length of
// dotDelay. This could be improved by making sure the
// character is a "-" but for most cases it would not matter.
//
// After the delay time the pin is taken low turning off the
// LED.
//
// Then it delays for one dotDelay time so the dots and dashes
// do not run together.
//=================================================================
void dotOrDash(char dotOrDash)
{
//digitalWrite(testLed, HIGH);
if (dotOrDash == '.')
{
//delay(dotDelay);
tone(tonePin, hertz, dotDelay);
}
else // must be a -
{
//delay(dotDelay * 3);
tone(tonePin, hertz, dotDelay * 3);
}
//digitalWrite(testLed, LOW);
delay(dotDelay); // gap between flashes
}
void squenceToMoserse(char* sequence)
{
int i = 0;
// Loop for each element in the array
while (sequence[i] != NULL)
{
// Is it lowercase letter?
if (sequence[i] >= 'a' && sequence[i] <= 'z')
{
morseCodeSequence(letters[sequence[i] - 'a']);
}
else if (sequence[i] >= 'A' && sequence[i] <= 'Z') // Uppercase Letter
{
morseCodeSequence(letters[sequence[i] - 'A']);
}
else if (sequence[i] >= '0' && sequence[i] <= '9') // Number
{
morseCodeSequence(numbers[sequence[i] - '0']);
}
else if (sequence[i] == ' ') // Space (wait for 4 times dotDelay
{
delay(dotDelay * 4); // gap between words
}
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment