Skip to content

Instantly share code, notes, and snippets.

@linnil1
Last active July 5, 2017 08:33
Show Gist options
  • Save linnil1/c0e7e6e91b599bb053ee58e2f7a5021f to your computer and use it in GitHub Desktop.
Save linnil1/c0e7e6e91b599bb053ee58e2f7a5021f to your computer and use it in GitHub Desktop.
#include <string.h>
const char *letter = "**ETIANMSURWDKGOHVF?L?PJBXCYZQ??";
int letterlen = strlen(letter);
const char *want = "FOOT";
char morse[1000];
int morses = 0;
const int light = 5;
const int beepin = 6;
void setup()
{
pinMode(light, OUTPUT);
pinMode(beepin, OUTPUT);
// decode
int wantlen = strlen(want);
for(int i=wantlen-1; i>=0; --i){
int where = 0;
for(where=0; where < letterlen; ++where)
if(want[i] == letter[where])
break;
// if(where == letterlen)
// printf("NO");
while( letter[where] != '*'){
morse[morses++] = where & 1 ? '-' : '.';
where >>= 1;
}
if(i)
morse[morses++] = ' ';
}
// reversed
for(int i=0; i<morses/2; ++i){
char t = morse[i];
morse[i] = morse[morses-1-i];
morse[morses-1-i] = t;
}
morse[morses++] = '\0';
}
void beep(unsigned char delayms){
analogWrite(beepin, 40); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(beepin, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
void loop()
{
const int timeunit = 500;
for(int i=0; i<morses; i++) {
if(morse[i] == '-') {
digitalWrite(light, HIGH);
delay(timeunit * 3);
digitalWrite(light, LOW);
delay(timeunit);
}
else if(morse[i] == '.') {
digitalWrite(light, HIGH);
delay(timeunit);
digitalWrite(light, LOW);
delay(timeunit);
}
else {
beep(200);
delay(timeunit * 3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment