Programming Arduino (1) Pages 46
Programming Arduino (1) Pages 46
The reason that Arduino has the two functions setup and loop is to separate the
things that only need to be done once, when the Arduino starts running its
sketch, from the things that have to keep happening continuously.
The function setup will just be run once when the sketch starts. Let’s add
some code to it that will blink the LED built onto the board. Add the lines to
your sketch so that it appears as follows and then upload them to your board:
void setup()
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
void loop()
The setup function itself calls two built-in functions, pinMode and
digitalWrite . You already know about digitalWrite , but pinMode is new. The
function pinMode sets a particular pin to be either an input or an output. So,
turning the LED on is actually a two-stage process. First, you have to set pin 13
to be an output, and second, you need to set that output to be high (5V).
When you run this sketch, on your board you will see that the L LED comes
on and stays on. This is not very exciting, so let’s at least try to make it flash by
turning it on and off in the loop function rather than in the setup function.
You can leave the pinMode call in the setup function because you only need