Created
May 7, 2012 11:23
-
-
Save neilang/2627314 to your computer and use it in GitHub Desktop.
Never gonna give you up - morse code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int ledPin = 13; | |
#define DOT_LENGTH 250 | |
// A dash is equal to three dots | |
#define DASH_LENGTH (DOT_LENGTH * 3) | |
// The space between parts of the same letter is equal to one dot | |
#define PART_PAUSE DOT_LENGTH | |
// The space between two letters is equal to three dots | |
#define LETTER_PAUSE ((DOT_LENGTH * 3) - PART_PAUSE) | |
// The space between two words is equal to seven dots | |
#define WORD_PAUSE ((DOT_LENGTH * 7) - LETTER_PAUSE) | |
#define MESSAGE_PAUSE ((WORD_PAUSE * 3) - WORD_PAUSE) | |
void setup(){ | |
pinMode(ledPin, OUTPUT); | |
} | |
void dot(){ | |
digitalWrite(ledPin, HIGH); | |
delay(DOT_LENGTH); | |
digitalWrite(ledPin, LOW); | |
delay(PART_PAUSE); | |
} | |
void dash(){ | |
digitalWrite(ledPin, HIGH); | |
delay(DASH_LENGTH); | |
digitalWrite(ledPin, LOW); | |
delay(PART_PAUSE); | |
} | |
void morse(char c){ | |
switch(toupper(c)){ | |
case 'A': | |
dot(); dash(); | |
break; | |
case 'B': | |
dash(); dot(); dot(); dot(); | |
break; | |
case 'C': | |
dash(); dot(); dash(); dot(); | |
break; | |
case 'D': | |
dash(); dot(); dot(); | |
break; | |
case 'E': | |
dot(); | |
break; | |
case 'F': | |
dot(); dot(); dash(); dot(); | |
break; | |
case 'G': | |
dash(); dash(); dot(); | |
break; | |
case 'H': | |
dot(); dot(); dot(); dot(); | |
break; | |
case 'I': | |
dot(); dot(); | |
break; | |
case 'J': | |
dot(); dash(); dash(); dash(); | |
break; | |
case 'K': | |
dash(); dot(); dash(); | |
break; | |
case 'L': | |
dot(); dash(); dot(); dot(); | |
break; | |
case 'M': | |
dash(); dash(); | |
break; | |
case 'N': | |
dash(); dot(); | |
break; | |
case 'O': | |
dash(); dash(); dash(); | |
break; | |
case 'P': | |
dot(); dash(); dash(); dot(); | |
break; | |
case 'Q': | |
dash(); dash(); dot(); dash(); | |
break; | |
case 'R': | |
dot(); dash(); dot(); | |
break; | |
case 'S': | |
dot(); dot(); dot(); | |
break; | |
case 'T': | |
dash(); | |
break; | |
case 'U': | |
dot(); dot(); dash(); | |
break; | |
case 'V': | |
dot(); dot(); dot(); dash(); | |
break; | |
case 'W': | |
dot(); dash(); dash(); | |
break; | |
case 'X': | |
dash(); dot(); dot(); dash(); | |
break; | |
case 'Y': | |
dash(); dot(); dash(); dash(); | |
break; | |
case 'Z': | |
dash(); dash(); dot(); dot(); | |
break; | |
case '1': | |
dot(); dash(); dash(); dash(); dash(); | |
break; | |
case '2': | |
dot(); dot(); dash(); dash(); dash(); | |
break; | |
case '3': | |
dot(); dot(); dot(); dash(); dash(); | |
break; | |
case '4': | |
dot(); dot(); dot(); dot(); dash(); | |
break; | |
case '5': | |
dot(); dot(); dot(); dot(); dot(); | |
break; | |
case '6': | |
dash(); dot(); dot(); dot(); dot(); | |
break; | |
case '7': | |
dash(); dash(); dot(); dot(); dot(); | |
break; | |
case '8': | |
dash(); dash(); dash(); dot(); dot(); | |
break; | |
case '9': | |
dash(); dash(); dash(); dash(); dot(); | |
break; | |
case '0': | |
dash(); dash(); dash(); dash(); dash(); | |
break; | |
case ' ': | |
delay(WORD_PAUSE); | |
break; | |
} | |
} | |
void output(char *str){ | |
size_t i; | |
for(i = 0; i < strlen(str); ++i){ | |
morse(str[i]); | |
delay(LETTER_PAUSE); | |
} | |
delay(MESSAGE_PAUSE); | |
} | |
void loop() { | |
output("Never gonna give you up"); | |
output("Never gonna let you down"); | |
output("Never gonna run around and desert you"); | |
output("Never gonna make you cry"); | |
output("Never gonna say goodbye"); | |
output("Never gonna tell a lie and hurt you"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment