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); }

Intro

So, this is my blog.  Well, more of a place for me to nerd out.  My nerdblog.

I've got a couple of goals for this year, and I'd like to write a little about how I get on with them.  I've been reading Make: Online for I-don't-know-how-long, and now I'm finally getting my finger out and making some stuff.
Arduino Uno Microcontroller
I got the Sparkfun Inventor's Kit for Christmas (thanks dad!), which of course includes the all-important blogger's friend, the Arduino, as well as a bunch of components.  There are some interesting starter circuit layouts, so that should get me comfortable with the basics, plus with the Arduino's open-source development environment, it allows me to finally put my limited C knowledge to use!

There are a couple of things I have in mind to do with my Arduino, but more on that later...  (Hint: they may involve facebook integration and/or musical instruments)

Starting new projects is all well and good, but I've got a few half-finished things sitting on my desk that I'm going to get to in the near future too.   Honest.

Hopefully if I blog some of what I'm doing it'll make me document it properly and I can write a couple of instructables for them, to pay that community back for a couple of awesome past projects!

Well anyway that's the main purpose of this blog, though I might even talk about my SCIENCE and whatever music catches my fancy from time to time too.  I'll do my best to not go all emo and start writing about my feelings and stuff, but I can't promise anything.  Oh no wait, I have facebook for that.