I've recently caught the Arduino bug. Having received my unit, I decided to write my first sketch. Since this is a first sketch, I decided it would be appropriate to do some variant of the "Hello, world" program. The following sketch uses an LED to transmit the message via International Morse Code.
The code:
/*
hello_world
Photos By Stephan
February 15, 2010
The following is an Arduino sketch that expresses the phrase "Hello World" in International Morse Code via an LED.
The code follows the IMC standard for timing. Timing is keyed to a base time unit of n microseconds. Dots are
keyed for a single unit. Dashes are keyed for three units. Dots and Dashes are always followed by intra-character
spacing of a single units. Characters are separated by a short gap of three units. Words are separated by a medium gap
of seven units.
Thanks to Martin Koch for providing the initial code fragment.
http://controlyourcamera.blogspot.com
*/
#define UNIT 100
#define LED 13
void setup() {
// assign the LED pin as an output
pinMode(LED, OUTPUT);
// annouce
annouce();
// send the message
sendthemessage();
}
void loop() {
}
void annouce() {
// light the LED for seconds to signal the beginning of the display
digitalWrite(LED, HIGH); // set the LED on
delay(2000); // wait for 2 seconds
digitalWrite(LED, LOW); // set the LED off
// pause for one second to differentiate between the IMC signal
delay(1000); // wait for a second
}
void sendthemessage() {
// Hello
hotel();
echo();
lima();
lima();
oscar();
// pause between words
mediumgap();
// World
whiskey();
oscar();
romeo();
lima();
delta();
// add trailing gaps in case we're looping
mediumgap();
mediumgap();
mediumgap();
}
void delta() {
// The letter D = -..
dah();
dit();
dit();
shortgap();
}
void echo() {
// The letter E = .
dit();
shortgap();
}
void hotel() {
// The letter H = ....
dit();
dit();
dit();
dit();
shortgap();
}
void lima() {
// The letter L = .-..
dit();
dah();
dit();
dit();
shortgap();
}
void oscar() {
// The letter O = ---
dah();
dah();
dah();
shortgap();
}
void romeo() {
// The letter R = .-.
dit();
dah();
dit();
shortgap();
}
void whiskey() {
// The letter W = .--
dit();
dah();
dah();
shortgap();
}
void dit() {
// short mark
digitalWrite(LED, HIGH); // set the LED on
delay(UNIT); // wait for a unit of time
digitalWrite(LED, LOW); // set the LED off
delay(UNIT); // intra-character gap
}
void dah() {
// long mark
digitalWrite(LED, HIGH); // set the LED on
delay(3 * UNIT); // wait for 3 units of time
digitalWrite(LED, LOW); // set the LED off
delay(UNIT); // intra-character gap
}
void shortgap() {
// A short gap is placed between each individual character. The standard short gap is three units of time. Since
// we've embedded an intra-character gap of one unit in both the long and short marks, we need only wait two additional
// units of time.
delay(2 * UNIT);
}
void mediumgap() {
// A medium gap is placed between each word. The standard medium gap is seven units of time. Since we've embedded both an
// intra-character gap and a short gap at the end of each character, we need only wait four additional units of time.
delay(4 * UNIT);
}
No comments:
Post a Comment