the setup() and loop() blocks
There are many different ways of writing sketches, but every sketch must have at least two parts: the setup() and loop() blocks. The setup() block conventionally appears in the sketch before the loop() block. A block is a section of code that runs together. To distinguish a block from the rest of the sketch, it will always start with a left-hand curly bracket { and end with a right-hand curly bracket }. The basic form of a sketch looks something like this:
void setup(){
do a task;
do another task;
}
void loop(){
do the main tasks;
and more tasks;
and more tasks;
.
.
.
}
The keyword void must appear before the block names. We discuss what it means later in this chapter where we cover functions.
The setup() block runs only once, right when the sketch begins. This means it will run right after a sketch is uploaded to the Arno. If a sketch it already on the Arno, the setup() block will run once when the Arno is powered up. We typically do “housekeeping” tasks in the setup() block to get things ready for the main part of the sketch. For example, we might set the modes of the input/output pins that we will need in the sketch, or get some initial input from the user or a sensor.
After the setup() block runs once, the sketch enters the loop() block. The lines of code in the loop() block are run one after another. When we hit the } at the bottom of the loop() block, the sketch returns to the top and runs the lines over again. This continues as long as the Arno is powered up.
void setup(){
do a task;
do another task;
}
void loop(){
do the main tasks;
and more tasks;
and more tasks;
.
.
.
}
The keyword void must appear before the block names. We discuss what it means later in this chapter where we cover functions.
The setup() block runs only once, right when the sketch begins. This means it will run right after a sketch is uploaded to the Arno. If a sketch it already on the Arno, the setup() block will run once when the Arno is powered up. We typically do “housekeeping” tasks in the setup() block to get things ready for the main part of the sketch. For example, we might set the modes of the input/output pins that we will need in the sketch, or get some initial input from the user or a sensor.
After the setup() block runs once, the sketch enters the loop() block. The lines of code in the loop() block are run one after another. When we hit the } at the bottom of the loop() block, the sketch returns to the top and runs the lines over again. This continues as long as the Arno is powered up.