Learning Arduino code basics
Here we begin with the basics of coding for Arduino. Writing code for Arduino and other embedded platforms is a little different from writing code for a computer. But don't fear—the differences are small.
Getting ready
To execute this recipe, we need just one ingredient: the Arduino IDE running on a computer.
How to do it…
These are the two mandatory functions in the Arduino coding environment:
void setup() { // Only execute once when the Arduino boots } void loop(){ // Code executes top-down and repeats continuously }
How it works…
Each Arduino sketch has two mandatory functions: the setup()
function and the loop()
function. The setup()
function only executes once: either when we apply power to the Arduino or when it resets. Usually, we use this function to configure the pins of the Arduino, to start communication protocols, such as serial communication, or to perform actions we only want to perform once when the Arduino boots.
The loop()
function executes continuously. Code in this function is executed top-down; when it reaches the end of the function, it jumps back to the start and runs again. This happens forever until the Arduino is switched off. In here, we write the code we want to run continuously.
See also
Continue the Arduino code basics with the following recipe, Code basics: Arduino C.