Skip to content

Instantly share code, notes, and snippets.

@krobro
Created March 24, 2015 22:02
Show Gist options
  • Save krobro/f5723bc186e91ea7e551 to your computer and use it in GitHub Desktop.
Save krobro/f5723bc186e91ea7e551 to your computer and use it in GitHub Desktop.
Use the LED to send out arbitrary messages in morse code.
//
// Morse_Blinker
//
// Use the LED to send out arbitrary messages in morse code.
//
// We use pin 13 as depending on the Arduino board
// (the Duinobot in our case), it has either:
// - a built-in LED or,
// - a built-in resistor so that you only need to add an LED.
//
// NOTES:
// - The Duinobot has an LED so we do not have to add anything.
// - See http://www.arduino.cc/en/Tutorial/Blink for the original
// tutorial.
//
int dot = 250; // time in milliseconds for a dot
int dash = 3*dot; // a dash is equal to 3 times a dot ('*' means multiply)
int ledPin = 13; // on board LED connected to digital pin 13
static char *_list[26] = {".-", // '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'
//
// show_a_dot
//
// Procedure to do all the work of blinking a dot
//
void show_a_dot()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(dot); // waits for a dot
digitalWrite(ledPin, LOW); // sets the LED off
delay(dot); // waits 3 dots
}
//
// show_a_dash
//
// Procedure to do all the work of blinking a dash
//
void show_a_dash()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(dash); // waits for a dash
digitalWrite(ledPin, LOW); // sets the LED off
delay(dot); // waits again for a dot
}
// show_char
//
// Do all the work of printing a character
//
// Arguments:
// c - the character to print
//
// Returns - true if successful
// false on failure
//
// NOTE: will not work for numbers
//
boolean show_char (char c)
{
int index;
if (isspace(c))
{
// this is a space character seperating words
delay(6*dot); // waits between words
return true;
}
index = tolower(c) - 'a';
if (index < 0 || index > 25 )
{
// index is out of range ... fail silently
return false;
}
for (int i=0; i<strlen(_list[index]); i++)
{
if (_list[index][i] == '.')
{
show_a_dot();
}
else if (_list[index][i] == '-')
{
show_a_dash();
}
}
return true;
}
// show_text
//
// Do all the work of printing any arbitrary text
// NOTE: will not work for numbers
//
void show_text (char *msg)
{
for (int i = 0; i < strlen(msg); i++)
{
show_char(msg[i]);
}
}
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
// We are going to say 'HELLO' in morse code
show_text("hello");
show_text("Look! It's moving. It's alive. It's alive... It's alive, it's moving, it's alive, it's alive, it's alive, it's alive, IT'S ALIVE! ... Oh, in the name of God! Now I know what it feels like to be God!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment