The Arduino Uno and derivative boards like the SparkFun RedBoard execute 16 million instructions per second; that’s really fast! It’s so fast, in fact, that without a delay, you’d never notice a change in the LED. The delay lets us control how long the LED stays on. In the example, delay(1000) instructs the Arduino to delay for 1,000 ms before executing the next command.
The next two lines of code are similar to the first two; they simply instruct the Arduino to turn the LED off and delay another 1,000 ms. After the last line, the loop() function repeats from the top and turns the LED back on.
HACK THE (HELLO) WORLD
One of the best ways to learn from example code is by changing what it does. Try decreasing the delays to 500 . Click Upload . How did the blink change? What if you pass delay() the number 5 instead? This is a 5 ms blink! Can you see it? What is the fastest blink rate that you
can
see?
Your First Piece of Hardware
With the LED on your board working and blinking away, the next step is to add your first piece of hardware: an external LED. As we mentioned, the pins on the Arduino are used for hooking up inputs and outputs to the microcontroller, and we can demonstrate that simply with an LED. Grab an LED and take a close look at it. It will look something like Figure 1-20 .
You’ll notice that the LED has a short leg and a long leg. If you look really closely, you’ll also see that the edge of the LED bulb has a flat surface on the same side as the short leg. These help youidentify the
polarity
of the legs; the LED’s long leg is the positive leg, and the short leg on the side of the flat bulb surface is the negative, or ground, leg.
FIGURE 1-20: An LED showing the long and short legs
Remember that LED_BUILTIN refers to pin 13 on the Arduino. So, adding your LED to the Arduino is as simple as plugging the long leg of the LED into pin 13 and the short leg of the LED into the GND (ground) pin right next to pin 13. Insert the LED now, with your board powered. If you plug it in correctly, as shown in Figure 1-21 , the LED will start blinking. If the LED doesn’t blink, you probably have it plugged in backward. Not to worry: pull it out and flip it around.
FIGURE 1-21: An LED added to pin 13 the quick and dirty way
GOING FURTHER
Each project in this book will have a “ Going Further ” section, which describes ways to take the concepts you learned in that project to the next level. These sections will include advice on using the existing project, hacking the code, and modifying the project physically.
Hack
For this project, we suggest you try to create some nifty blink patterns. First, copy and paste the four lines in the loop() function so that it repeats and you end up with eight lines of code. This gives you two blink sequences and more code to work with. You can create patterns by modifying the delay times to control when theLED lights. For example, we made a pattern that looks like a heartbeat; our modified Blink sketch is shown in Listing 1-4 .
LISTING 1-4: Example code of a heartbeat pattern
void setup () {
pinMode ( LED_BUILTIN , OUTPUT );
}
void loop () {
digitalWrite ( LED_BUILTIN , HIGH );
delay (200);
digitalWrite ( LED_BUILTIN , LOW );
delay (200);
digitalWrite ( LED_BUILTIN , HIGH );
delay (200);
digitalWrite ( LED_BUILTIN , LOW );
delay (800);
}
For a real challenge, try programming your Arduino to flash the letters of your name in Morse code with a series of short (dot) and long (dash) blinks. Figure 1-22 shows a Morse code cheat sheet to help you figure out the blink patterns. The classic message that most people start with is S-O-S, or . . . - - - . . . (dot dot dot, dash dash dash, dot dot dot).
FIGURE 1-22: Basic Morse code chart
Modify
A blinking light is a powerful thing! With your newfound superpower, you can physically add LEDs to a lot of things around the house. A Halloween costume is always a great place for some blinky
Jo Willow, Sharon Gurley-Headley