Skip to content

Instantly share code, notes, and snippets.

@spacelatte
Created September 28, 2014 19:08
Show Gist options
  • Save spacelatte/1b663d2d725651e2ae98 to your computer and use it in GitHub Desktop.
Save spacelatte/1b663d2d725651e2ae98 to your computer and use it in GitHub Desktop.
// author: mert akengin
short led = 13;
unsigned morse[256];
typedef enum time
{
unit = 100,
sh = 1*unit,
ln = 3*unit,
br = (int)(2*unit),
lt = 3*unit,
sp = 7*unit,
msize = 6
} ledtime;
void init_morse()
{
for(int i=0;i<256;i++)
morse[i] = 0;
morse['A'] = 1*3 + 2*9 + 0*27 + 0*81;
morse['B'] = 2*3 + 1*9 + 1*27 + 1*81;
morse['C'] = 2*3 + 1*9 + 2*27 + 1*81;
morse['D'] = 2*3 + 1*9 + 1*27 + 0*81;
morse['E'] = 1*3 + 0*9 + 0*27 + 0*81;
morse['F'] = 1*3 + 1*9 + 2*27 + 1*81;
morse['G'] = 2*3 + 2*9 + 1*27 + 0*81;
morse['H'] = 1*3 + 1*9 + 1*27 + 1*81;
morse['I'] = 1*3 + 1*9 + 0*27 + 0*81;
morse['J'] = 1*3 + 2*9 + 2*27 + 2*81;
morse['K'] = 2*3 + 1*9 + 2*27 + 0*81;
morse['L'] = 1*3 + 2*9 + 1*27 + 1*81;
morse['M'] = 2*3 + 2*9 + 0*27 + 0*81;
morse['N'] = 2*3 + 1*9 + 0*27 + 0*81;
morse['O'] = 2*3 + 2*9 + 2*27 + 0*81;
morse['P'] = 1*3 + 2*9 + 2*27 + 1*81;
morse['Q'] = 2*3 + 2*9 + 1*27 + 2*81;
morse['R'] = 1*3 + 2*9 + 1*27 + 0*81;
morse['S'] = 1*3 + 1*9 + 1*27 + 0*81;
morse['T'] = 2*3 + 0*9 + 0*27 + 0*81;
morse['U'] = 1*3 + 1*9 + 2*27 + 0*81;
morse['V'] = 1*3 + 1*9 + 1*27 + 2*81;
morse['W'] = 1*3 + 2*9 + 2*27 + 0*81;
morse['X'] = 2*3 + 1*9 + 1*27 + 2*81;
morse['Y'] = 2*3 + 1*9 + 2*27 + 2*81;
morse['Z'] = 2*3 + 2*9 + 1*27 + 1*81;
morse['0'] = 2*3 + 2*9 + 2*27 + 2*81 + 2*243;
morse['1'] = 1*3 + 2*9 + 2*27 + 2*81 + 2*243;
morse['2'] = 1*3 + 1*9 + 2*27 + 2*81 + 2*243;
morse['3'] = 1*3 + 1*9 + 1*27 + 2*81 + 2*243;
morse['4'] = 1*3 + 1*9 + 1*27 + 1*81 + 2*243;
morse['5'] = 1*3 + 1*9 + 1*27 + 1*81 + 1*243;
morse['6'] = 2*3 + 1*9 + 1*27 + 1*81 + 1*243;
morse['7'] = 2*3 + 2*9 + 1*27 + 1*81 + 1*243;
morse['8'] = 2*3 + 2*9 + 2*27 + 1*81 + 1*243;
morse['9'] = 2*3 + 2*9 + 2*27 + 2*81 + 1*243;
morse['/'] = 2*3 + 1*9 + 1*27 + 2*81 + 1*243;
morse['('] = 2*3 + 1*9 + 2*27 + 2*81 + 1*243;
morse['&'] = 1*3 + 2*9 + 1*27 + 1*81 + 1*243;
morse['='] = 2*3 + 1*9 + 1*27 + 1*81 + 2*243;
morse['+'] = 1*3 + 2*9 + 1*27 + 2*81 + 1*243;
morse['.'] = 1*3 + 2*9 + 1*27 + 2*81 + 1*243 + 2*729;
morse[','] = 2*3 + 2*9 + 1*27 + 1*81 + 2*243 + 2*729;
morse['?'] = 1*3 + 1*9 + 2*27 + 2*81 + 1*243 + 1*729;
morse['`'] = 1*3 + 2*9 + 2*27 + 2*81 + 2*243 + 1*729;
morse['!'] = 2*3 + 1*9 + 2*27 + 1*81 + 2*243 + 2*729;
morse[')'] = 2*3 + 1*9 + 2*27 + 2*81 + 1*243 + 2*729;
morse[':'] = 2*3 + 2*9 + 2*27 + 1*81 + 1*243 + 1*729;
morse[';'] = 2*3 + 1*9 + 2*27 + 1*81 + 2*243 + 1*729;
morse['-'] = 2*3 + 1*9 + 1*27 + 1*81 + 1*243 + 2*729;
morse['_'] = 1*3 + 1*9 + 2*27 + 2*81 + 1*243 + 2*729;
morse['"'] = 1*3 + 2*9 + 1*27 + 1*81 + 2*243 + 1*729;
morse['@'] = 1*3 + 2*9 + 2*27 + 1*81 + 2*243 + 1*729;
morse['\''] = 1*3 + 2*9 + 2*27 + 2*81 + 2*243 + 1*729;
return;
}
void ledshort()
{
digitalWrite(led,HIGH);
delay(sh);
digitalWrite(led,LOW);
return;
}
void ledlong()
{
digitalWrite(led,HIGH);
delay(ln);
digitalWrite(led,LOW);
return;
}
void letter(char c)
{
switch(c)
{
case '\0':
case '\n':
Serial.write("\n");
case '\r':
Serial.write("\r");
case ' ':
if(c == ' ')
Serial.write(" ");
delay(sp);
return;
}
if(c < '!' || c > 'z')
return;
if(c >= 'a' && c <= 'z')
c = c-('a'-'A');
short dig[msize],div[msize+1]={3,9,27,81,243,729,2187};
for(int i=msize-1;i>0;i--)
dig[i-1] = (morse[c]%div[i])/div[i-1];
Serial.write(c);
for(int i=0;i<msize-1;i++)
{
switch(dig[i])
{
case 1:
ledshort();
break;
case 2:
ledlong();
break;
default:
delay(lt);
return;
}
delay(br);
}
}
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
init_morse();
}
String input;
void loop()
{
while(!Serial.available());
input = Serial.readString();
//Serial.write(" ");
for(int i=0;i<input.length();i++)
letter(input[i]);
Serial.write("\n");
delay(25*unit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment