Skip to content

Instantly share code, notes, and snippets.

@andypiper
Last active December 31, 2015 03:38
Show Gist options
  • Select an option

  • Save andypiper/7928440 to your computer and use it in GitHub Desktop.

Select an option

Save andypiper/7928440 to your computer and use it in GitHub Desktop.
"use mqtt" morse code flasher
// Morse Code - USE MQTT!
// based on a project from 30 Arduino Projects for the Evil Genius
// thanks to Simon Monk
// Andy Piper @andypiper, Nov 2013
int ledPin = 12;
char* letters[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};
char* numbers[] = {"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
int dotDelay = 200;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // quick blinks to acknowledge setup
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW); // don't want LED on immediately
delay(dotDelay * 20); // before we get started
}
void loop()
{
char ch;
ch = (char)'u';
flashSequence(letters[ch - 'a']);
ch = (char)'s';
flashSequence(letters[ch - 'a']);
ch = (char)'e';
flashSequence(letters[ch - 'a']);
delay(dotDelay * 7); // gap between words
ch = (char)'m';
flashSequence(letters[ch - 'a']);
ch = (char)'q';
flashSequence(letters[ch - 'a']);
ch = (char)'t';
flashSequence(letters[ch - 'a']);
ch = (char)'t';
flashSequence(letters[ch - 'a']);
delay(dotDelay * 14); // end message
}
void flashSequence(char* sequence)
{
int i = 0;
while (sequence[i] != NULL)
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); // gap between letters
}
void flashDotOrDash(char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == '.')
{
delay(dotDelay);
}
else // must be a -
{
delay(dotDelay * 3);
}
digitalWrite(ledPin, LOW);
delay(dotDelay); // gap between flashes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment