Friday, 14 January 2011

Arduino Morse Code Sketch

Here's my first Arduino code sketch.  It uses the default "blink" circuit (just one LED connected to pin 13), and it blinks out "HELLO WORLD" in morse code.

This variation defines a function for a "dot" blink, and a "dash" blink (5 times longer than a dot), and then a function for each letter and a space.

Not the most exciting example, but since this is the first circuit you make when learning to use the Arduino, it's pretty cool writing a simple little program, uploading it, and seeing it working right there in front of you!

/*
Morse Code
by baz
http://nanobaz.blogspot.com

Uses blink circuit with an LED on pin 13

A    • −    
B    − • • •    
C    − • − •    
D    − • •    
E    •    
F    • • − •    
G    − − •    
H    • • • •    
I    • •    
J    • − − −    
K    − • −    
L    • − • •    
M    − −    
N    − •    
O    − − −    
P    • − − •    
Q    − − • −    
R    • − •    
S    • • •    
T    −    
U    • • −    
V    • • • −    
W    • − −    
X    − • • −    
Y    − • − −    
Z    − − • •
*/
int ledPin = 13, t=100;   // LED on pin 13
                          // time constant t=100ms

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  H();E();L();L();O();SPACE();W();O();R();L();D();

  delay(8*t);   // End of line pause
}

void dot()
{
  digitalWrite(ledPin, HIGH);
  delay(t);
  digitalWrite(ledPin, LOW);
  delay(1*t);
}

void dash()
{
  digitalWrite(ledPin, HIGH);
  delay(5*t);
  digitalWrite(ledPin, LOW);
  delay(1*t);
}

void A(){ dot(); dash();                 delay(2*t); }
void B(){ dash(); dot(); dot(); dot();   delay(2*t); }
void C(){ dash(); dot(); dash(); dot();  delay(2*t); }
void D(){ dash(); dot(); dot();          delay(2*t); }
void E(){ dot();                         delay(2*t); }
void F(){ dot(); dot(); dash(); dot();   delay(2*t); }
void G(){ dash(); dash(); dot();         delay(2*t); }
void H(){ dot(); dot(); dot(); dot()     delay(2*t); }
void I(){ dot(); dot();                  delay(2*t); }
void J(){ dot(); dash(); dash(); dash(); delay(2*t); }  
void K(){ dash(); dot(); dash();         delay(2*t); }
void L(){ dot(); dash(); dot(); dot();   delay(2*t); }
void M(){ dash(); dash();                delay(2*t); }
void N(){ dash(); dot();                 delay(2*t); }
void O(){ dash(); dash(); dash();        delay(2*t); }
void P(){ dot(); dash(); dash(); dot();  delay(2*t); }
void Q(){ dash(); dash(); dot(); dash(); delay(2*t); }  
void R(){ dot(); dash(); dot();          delay(2*t); }
void S(){ dot(); dot(); dot();           delay(2*t); }
void T(){ dash();                        delay(2*t); }
void U(){ dot(); dot(); dash();          delay(2*t); }
void V(){ dot(); dot(); dot(); dash();   delay(2*t); }
void W(){ dot(); dash(); dash();         delay(2*t); }
void X(){ dash(); dot(); dot(); dash();  delay(2*t); }
void Y(){ dash(); dot(); dash(); dash(); delay(2*t); }  
void Z(){ dash(); dash(); dot(); dot();  delay(2*t); }
void SPACE(){ delay(4*t); }

No comments:

Post a Comment