SunFounder 3in1 Kit
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
}
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
void loop() {
// put your main code here, to run repeatedly:
}
With the help of these sketches, we can summarize several features of setup-loop.
• loop() will be run repeatedly after the board is powered up.
• setup() will run only once after the board is powered up.
• After the board is powered up, setup() will run first, followed by loop().
• The code needs to be written within the {} scope of setup() or loop(), out of the framework will be an error.
Note: Statements such as digitalWrite(13,HIGH) are used to control the on-board LED, and we will talk about
their usage in detail in later chapters.
2.3.6 Sketch Writing Rule
If you ask a friend to turn on the lights for you, you can say “Turn on the lights.”, or “Lights on, bro.”, you can use any
tone of voice you want.
However, if you want the Arduino board to do something for you, you need to follow the Arduino program writing rules
to type in the commands.
This chapter contains the basic rules of the Arduino language and will help you understand how to translate natural
language into code.
Of course, this is a process that takes time to get familiar with, and it is also the most error-prone part of the process
for newbies, so if you make mistakes often, it’s okay, just try a few more times.
Semicolon ;
Just like writing a letter, where you write a period at the end of each sentence as the end, the Arduino language requires
you to use ; to tell the board the end of the command.
Take the familiar “onboard LED blinking” example. A healthy sketch should look like this.
Example:
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
}
(continues on next page)
2.3. How to build an Arduino Project 69