Love it or hate it, every programmer typically wets their feet by writing a “hello world” program in their new programming environment. Since unlike with the type of code I’m used to writing the Arduino board doesn’t come with a CRT or LCD screen to output this text, we have to find a more creative way to do this. There are many options, such as LEDs, peizo buzzers, small LCD pannels and speakers that could be used to output a message. The Hello World sketchbook found in the examples outputs the text “Hello World” to the serial port monitor, we will be building upon this example and using an LED to output this same message. The Duemilanove comes with an LED build into the board and is attached to PIN 13. This is very convenient as the experimentation kit did not come with even a single LED to play with. I did, however, order a couple different types of LEDs with my order as I knew I wanted them to play around with. For this example we will use the onboard LED as it requires no wiring and gives you a good way to get comfortable with uploading your program to the board and the basic structure of the code.
Start off by loading the Arduino development environment and on the main menu select File->Examples->Stubs->HelloWorld. The following project will be loaded into your sketchbook:
void setup() { Serial.begin(9600); } void loop() { Serial.println("Hello World!"); }
This is the basic structure of all Ardino programs. These two functions are the heart of every program you will write, so it doesn’t hurt to take a moment and discuss them. Both functions must be declared outside of the scope of all other functions. They are declared as void so they are not expected to return any value and they do not require any parameters. The setup funciton is called once when your board first powers up, and is used to initialize any devices or pins that are going to be used in your program. After the setup function executes the loop function begins to run, and it will continue to run endlessly until you power off your board. When first beginning to work with microcontrollers it takes a bit of time to wrap your head around the way the execute code, in an infinite loop, as this is somewhat different from the way any programs would run on your desktop or server computer.
In the setup function you will see that the board is configured for serial communications with the command Serial.begin(9600);. It is set to communicate at 9600 baud and it is important to note that even though we are using an emulated serial port over USB that pins 0 (Rx) and 1 (Tx) are wired to the FTDI USB-to-TTL Serial chip so they will be unavailable whenever you are performing serial communication over USB.
In the loop function Serial.println(“Hello World!”); is used to echo Hello World back to the computer over the serial connection. In order to see this message you will have to hit the Serial Monitor button on your toolbar. The serial monitor provides us with one of our only ways to debug our code, by echoing debug information back to our computer. And of course the serial communication ports on the Arduino can be used for more advanced purposes.
So how can we make this example a little more interesting? How about programming the onboard LED to flash Hello World in morse code? If you have a pack of LEDs and transistors you could also set up an array of LEDs to spell it out in binary. Since the Experiementation Kit did not include these, I will stick with using the LED connected to pin 13. The first step was to find a reference for morse code, a quick trip to Wikipedia gave me everything I needed. After that you will need a quick intro to working with LEDs, the best way to do that is to select File->Examples->Digital->Blink from the menu and take a look at the code there.
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. The circuit: * LED connected from digital pin 13 to ground. * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example. Created 1 June 2005 By David Cuartielles http://arduino.cc/en/Tutorial/Blink based on an orginal by H. Barragan for the Wiring i/o board */ int ledPin = 13; // LED connected to digital pin 13 void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(1000); // wait for a second }
The command pinMode in the setup function is used to tell the micro that pin 13 is going to be used for output. In your main loop you are simply telling it to turn on (HIGH) and off (LOW) by passing the pin number and mode to the digitalWrite command. You could also use the analogWrite command but since pin 13 does not support PWM you would only be able to set it to on or off, rather than adjust the brightness. Pins 3, 5, 6, 9, 10, and 11 on the Duemilanove support PWM and if you need to create a pulsing effect with your LEDs you will have to to hook up an LED and 1K Ohm resistor to one of those pins. At this point, stick with pin 13, and hit the upload button again and watch what happens. From there I would suggest tweaking the delays to create different blinking patterns if you feel you need to get a bit more comfortable with LEDs before moving on.
When you’re ready, we’ll begin splicing these two examples together and creating our morse code flasher. If you have an extra LED sitting around feel free to plug the long leg into pin 13 and the short leg into the ground (GND) beside it. Since pin 13 has a resistor built in we to not need to do anything more than that. At the top of your project declare some new variables. We’re going to do this before any of the core functions as they need to be accissible by all of them.
int delayDot = 200; int delayDash = 500; int delayLetter = 250; int delayWord = 500;
These variables store the delay time in milliseconds for the two different “characters” and the spacing between them. These can be tweaked to your liking, I personally don’t know Morse Code so I’m not sure how acturate the timings are.
Next you will be adding some code to the setup() and loop() functions.
void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { print_morse_code("h"); print_morse_code("e"); print_morse_code("l"); print_morse_code("l"); print_morse_code("o"); print_space(1); print_morse_code("w"); print_morse_code("o"); print_morse_code("r"); print_morse_code("l"); print_morse_code("d"); delay(1000); }
The setup() function tells the board it will be outputting over the FTDI bus and prepares Pin 13 for output. The loop calls a function called print_morse_code() which is a very basic character map for the differenet pulses, it simply masses the character passed in with its list of options and outputs the proper sequence for each. At the end of the main loop is a 1 second delay before it starts over from the beginning.
Before we look at the print_morse_code() function lets look at the functions that create the characters.
void print_dot() { digitalWrite(ledPin, HIGH); // set the LED on delay(delayDot); // wait for a second } void print_dash() { digitalWrite(ledPin, HIGH); // set the LED on delay(delayDot); // wait for a second } // Turn off LED // 0 = Letter // 1 = Word void print_space(byte len) { digitalWrite(ledPin, LOW); // set LED off if(len) { delay(delayWord); } else { delay(delayLetter); } }
They’re quite simple functions. The LED is set to HIGH and the module waits 250ms for a dot and 500ms for a dash before it moves on to the next digit. The next function, print_space(), is a bit more intricate. It requires a parameter to be passed in to determine which delay is going to be used after turning the LED off. We want a 200ms space between each letter and a 500ms space between each word.
The last function we need is print_more_code() which ties it all together. It’s not pretty, but it works.
// Convert letters to to morse code via LED and Serial echos void print_morse_code(char* letter) { // Hello // **** * *-** *-** --- // World // *-- --- *-* *-** -** if(letter=="h") { Serial.println("****"); print_dot(); print_dot(); print_dot(); print_dot(); } else if (letter=="e") { Serial.println("*"); print_dot(); } else if (letter=="l") { Serial.println("*-**"); print_dot(); print_dash(); print_dash(); print_dash(); } else if (letter=="o") { Serial.println("---"); print_dash(); print_dash(); print_dash(); } else if (letter=="w") { Serial.println("*--"); print_dot(); print_dash(); print_dash(); } else if (letter=="r") { Serial.println("*-*"); print_dot(); print_dash(); print_dot(); } else if (letter=="d") { Serial.println("-**"); print_dash(); print_dot(); print_dot(); } print_space(0); }
A character is passed in and matched to a sequence of calls to print_dot() and print_dash() to create the desired letter. At the end a letter space is added for padding making it easier to understand the transmission. And just to help out with decrypting the message I output the currect character to the serial port so you can follow along on your computer.
The code for this entire project is available on Google Code for reference. I encourage anyone to build off of it or optimize it. Just keep in mind I’m not a C programmer 😉
A seasoned Senior Solutions Architect with 20 years of experience in technology design and implementation. Renowned for innovative solutions and strategic insights, he excels in driving complex projects to success. Outside work, he is a passionate fisherman and fish keeper, specializing in planted tanks.