Original thread: Arduino + Wire = Morse Code AM Radio Beacon - Exhibition - Arduino Forum
Thought I would throw in my version:
/**
* Arduino + Wire = Morse Code AM Radio Beacon
* By EllisGL 2011-08-08
* Version 1.00 (Arduino version)
* Based on http://arduino.cc/forum/index.php/topic,8456.msg68164.html
*
* Change log:
* V1.00: Initial
*
* Known Issues:
* Harmonics
*/
// Beacon message
const char message[] = "hello from my arduino";
// Timings
const unsigned int dahLen = 1000; // 1000 ms - dah length
const unsigned int ditLen = 250; // 250 ms - dit length - same timeing between dots and dashes of the same letter
const unsigned int maxBuf = 80; // Max length for message buffer
const unsigned int lPause = 750; // 750 ms - length between charaters
const unsigned int wPause = 1750; // 1750 ms - length for spaces
const unsigned int ledPin = 13;
const unsigned int outPin = 11;
// Character mapping
const char chars[36][5] =
{
{0, 1 }, // a
{1, 0, 0, 0 }, // b
{1, 0, 1, 0 }, // c
{1, 0, 0 }, // d
{0 }, // e
{0, 0, 1, 0 }, // f
{1, 1, 0 }, // g
{0, 0, 0, 0 }, // h
{0, 0 }, // i
{0, 1, 1, 1 }, // j
{1, 0, 1 }, // k
{0, 1, 0, 0 }, // l
{1, 1 }, // m
{1, 0 }, // n
{1, 1, 1 }, // o
{0, 1, 1, 0 }, // p
{1, 1, 0, 1 }, // q
{0, 1, 0 }, // r
{0, 0, 0 }, // s
{1 }, // t
{0, 0, 1 }, // u
{0, 0, 0, 1 }, // v
{0, 1, 1 }, // w
{1, 0, 0, 1 }, // x
{1, 0, 1, 1 }, // y
{1, 1, 0, 0 }, // z
{0, 1, 1, 1, 1}, // 1
{0, 0, 1, 1, 1}, // 2
{0, 0, 0, 1, 1}, // 3
{0, 0, 0, 0, 1}, // 4
{0, 0, 0, 0, 0}, // 5
{1, 0, 0, 0, 0}, // 6
{1, 1, 0, 0, 0}, // 7
{1, 1, 1, 0, 0}, // 8
{1, 1, 1, 1, 0}, // 9
{1, 1, 1, 1, 1}, // 0
};
unsigned int msgLen, i, l, p, x;
char buffer, a, ch;
void dit(void);
void dah(void);
void pause(void);
void broadcast(int N_cycles);
void dontbroadcast(int N_cycles);
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT); // sets the digital pin as output
msgLen = sizeof(message);
}
void loop()
{
for(x = 0; x < msgLen; x++)
{
buffer = message[x];
switch(buffer)
{
case 'a':
l = 0;
p = 2;
a = 'a';
break;
case 'b':
l = 1;
p = 4;
a = 'b';
break;
case 'c':
l = 2;
p = 4;
a = 'c';
break;
case 'd':
l = 3;
p = 3;
a = 'd';
break;
case 'e':
l = 4;
p = 1;
a = 'e';
break;
case 'f':
l = 5;
p = 4;
a = 'f';
break;
case 'g':
l = 6;
p = 3;
a = 'g';
break;
case 'h':
l = 7;
p = 4;
a = 'h';
break;
case 'i':
l = 8;
p = 2;
a = 'i';
break;
case 'j':
l = 9;
p = 4;
a = 'j';
break;
case 'k':
l = 10;
p = 3;
a = 'k';
break;
case 'l':
l = 11;
p = 4;
a = 'l';
break;
case 'm':
l = 12;
p = 2;
a = 'm';
break;
case 'n':
l = 13;
p = 2;
a = 'n';
break;
case 'o':
l = 14;
p = 3;
a = 'o';
break;
case 'p':
l = 15;
p = 4;
a = 'p';
break;
case 'q':
l = 16;
p = 4;
a = 'q';
break;
case 'r':
l = 17;
p = 3;
a = 'r';
break;
case 's':
l = 18;
p = 3;
a = 's';
break;
case 't':
l = 19;
p = 1;
a = 't';
break;
case 'u':
l = 20;
p = 3;
a = 'u';
break;
case 'v':
l = 21;
p = 4;
a = 'v';
break;
case 'w':
l = 22;
p = 3;
a = 'w';
break;
case 'x':
l = 23;
p = 4;
a = 'x';
break;
case 'y':
l = 24;
p = 4;
a = 'y';
break;
case 'z':
l = 25;
p = 4;
a = 'z';
break;
case '1':
l = 26;
p = 5;
a = '1';
break;
case '2':
l = 27;
p = 5;
a = '2';
break;
case '3':
l = 28;
p = 5;
a = '3';
break;
case '4':
l = 29;
p = 5;
a = '4';
break;
case '5':
l = 30;
p = 5;
a = '5';
break;
case '6':
l = 31;
p = 5;
a = '6';
break;
case '7':
l = 32;
p = 5;
a = '7';
break;
case '8':
l = 33;
p = 5;
a = '8';
break;
case '9':
l = 34;
p = 5;
a = '9';
break;
case '0':
l = 35;
p = 5;
a = '0';
break;
default:
l = 36;
p = 1;
a = ' ';
}
Serial.print(a);
Serial.print(" ");
if(l == 36)
{
Serial.println("SPACE");
delay(wPause);
}
else
{
for(i = 0; i < p; i++)
{
if(chars[l][i] == 0)
{
dit();
}
else
{
dah();
}
}
delay(lPause);
Serial.println();
}
}
Serial.println();
delay(wPause);
}
void dah(void)
{
Serial.print("-");
digitalWrite(ledPin, HIGH);
analogWrite(outPin, 128);
delay(dahLen);
digitalWrite(ledPin, LOW);
analogWrite(outPin, 0);
delay(ditLen);
}
void dit(void)
{
Serial.print(".");
digitalWrite(ledPin, HIGH);
analogWrite(outPin, 128);
delay(ditLen);
digitalWrite(ledPin, LOW);
analogWrite(outPin, 0);
delay(ditLen);
}