SunFounder GalaxyRVR Kit for Arduino, Release 1.0
Time to uncover some cool secrets about Arduino programming!
• Code Magic: setup() and loop()
An Arduino sketch, or a piece of code, is like a two-act play:
– setup(): This is Act 1, the opening scene. It only happens once, when your Arduino board first
wakes up. It’s used to set the stage by preparing things like pin modes and libraries.
– loop(): After Act 1, we move onto Act 2 which repeats on a loop until the final curtain (which
only happens if we turn off the power or hit the reset button!). This part of the code is like the
main part of our play, where the action really happens.
But remember, even if there’s no magic (code) in the setup() or loop(), we still need to keep them.
They’re like the stage - even an empty stage is still a stage.
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the␣
˓→voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the␣
˓→voltage LOW
delay(1000); // wait for a second
}
// the loop function runs over and over again forever
void loop() {
}
• Punctuation Marks in Coding
Just like in a storybook, Arduino uses special punctuation marks to make sense of the code:
– Semicolons (;): These are like the full stops in a story. They tell the Arduino “Okay, I’m done
with this action. What’s next?”
– Curly Braces {}: These are like the beginning and the end of a chapter. They wrap up pieces
of code together, marking where a section starts and ends.
If you happen to forget some of these punctuation marks, don’t worry! The Arduino is like a friendly
teacher who will check your work, point out where the mistakes are, and show you how to fix them.
It’s all part of the learning adventure!
• About the Functions
Imagine these functions as magical spells. Each spell has a specific effect in our Arduino adventure:
– pinMode(): This spell decides whether a pin is an INPUT or an OUTPUT. It’s like deciding if
a character in our story speaks (OUTPUT) or listens (INPUT).
– digitalWrite(): This spell can turn a pin HIGH (on) or LOW (off), like switching a magic
light on and off.
– delay(): This spell makes the Arduino pause for a certain amount of time, like taking a short
nap in the middle of our story.
3.3. Lesson 3: Entering the World of Arduino and Coding 31