Friday, January 7, 2011

Arduino Hello World in Morse Code

Finally, it arrived! The Arduino kit and some additional hardware has just arrived today and as always, the all time favorite "Hello World" has to serve as a first experiment. I decided to spice it up a little bit though. I let the Arduino board "talk" in morse code through its onboard LED.

So here it goes. The first code snippet and a video of the Arduino board greeting all of you :).

/*
  Morse Code Hello World.
  One unit is equal to 250 ms in this example.
 */

void setup() {               
  // initialize the digital pin as an output.
  pinMode(13, OUTPUT);    
}

void loop() {
  //show H . . . .
  for(int x = 0; x < 4; x++) {
    dot();
  }

  //show E .
  dot();

  //show L . - . . (2 times)
  for(int x = 0; x < 2; x++) {
    dot();
    dot();
    dash();
    dot();
  }

  //show O - - -
  for(int x = 0; x < 3; x++) {
    dash();
  }
 
  //show space (waiting time between words is equal to seven units)
  digitalWrite(13, LOW);   
  delay(7 * 250);

  //show W . - -
  dot();
  dash();
  dash();

  //show O - - -
  for(int x = 0; x < 3; x++) {
    dash();
  }

  //show R . - .
  dot();
  dash();
  dot();
 
  //show L . - . .
  dot();
  dash();
  dot();
  dot();
 
  //show D - . .
  dash();
  dot();
  dot();
 
  //show space (waiting time between words is equal to seven units)
  digitalWrite(13, LOW);   
  delay(7 * 250);
}

//dot is equal to one unit
void dot() {
  digitalWrite(13, HIGH);  
  delay(250);             
  digitalWrite(13, LOW);
  //pause between next sign is one unit 
  delay(250);
}

//dash is equal to 3 units
void dash() {
  digitalWrite(13, HIGH);  
  delay(750);             
  digitalWrite(13, LOW);
  //pause between next sign is one unit   
  delay(250);
}

No comments:

Post a Comment