Our First Program

Our first program looks like this:

There are two functions, called setup() and loop(). All of our Energia programs will have these two functions. The setup() function is run once. Then the loop() function is run over and over again, forever.

In this first program, we have a single line of code in the setup() function.

Computer chips like the one in our Launchpad have a number of little metal legs coming out of the plastic block that hold the chip. These legs are called pins, and they can be set to either listen to the outside world, or speak to the outside world. Listening is called INPUT, and speaking is called OUTPUT. Since we want to make an LED flash, we are speaking, so we set the mode of the pin that is connected to the red LED on the board to OUTPUT.

That is what the line

         pinMode( RED_LED, OUTPUT );

does in the setup() function.

In the loop() function, we turn the LED on by setting that pin to HIGH, using the digitalWrite() function.

Our little Launchpad computer is very fast. It can run millions of lines of code every second. If we simply turned the LED back off again right away, it would not appear to flash, because your eye can't detect flashes that happen millions of times per second. So we need to add a delay between turning the LED off and back on again.

The delay() function simply waits until some number of milliseconds have elapsed. A millisecond is one thousandth of a second, so 100 of them is a tenth of a second. If we delay for a tenth of a second after the LED comes on, and another tenth of a second after it turns off, we will get 5 flashes per second. Our eyes can easily detect that.

[Click here to see the Arduino version of this program, which needs an extra declaration for RED_LED.]