Arduino Programming Guide
Posted: 03 Oct 2023, 05:09
Setting Up and Running Functions
Functions are code segments created to execute specific tasks. In each Arduino sketch, there are two crucial functions: setup() and loop(). To comprehend these functions better, let's inspect the pre-loaded Blink sketch on the Arduino closely.
Within the sketch, you'll see some lines of text that start with // characters. These symbols show that any text following the // and ending at the line's end is a comment. These comments don't count as program code; they only help readers understand how the code works.
According to comments, the code lines within the setup() function operate only once - precisely when the Arduino is powered or reset. Therefore, the setup() function is solely responsible for executing tasks required at program initiation. In the Blink demonstration, setup() primarily configures the LED pin as an output.
Conversely, the commands inside the loop() function repeat regularly. Simply put, after executing the last line, the loop() function immediately returns to the first line.
We won't go into the specifics of the setup() and loop() functions' commands in the Blink sketch here but will discuss them in greater detail shortly.
Functions are code segments created to execute specific tasks. In each Arduino sketch, there are two crucial functions: setup() and loop(). To comprehend these functions better, let's inspect the pre-loaded Blink sketch on the Arduino closely.
Code: Select all
int led - 13;
// the start procedure is executed once
// after you press the reset button:
void setup(){
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// the cyclic procedure repeats over and over again,
// and so on ad infinitum:
void loop(){
digitalWrite(led, HIGH); // turn on the LED
// (HIGH is the voltage level)
delay(1000); // wait 1 second
digitalWrite(led, LOW); // turn off the LED,
// by switching the voltage to the LOW level
delay(1000); // wait 1 second
Translated with www.DeepL.com/Translator (free version)
According to comments, the code lines within the setup() function operate only once - precisely when the Arduino is powered or reset. Therefore, the setup() function is solely responsible for executing tasks required at program initiation. In the Blink demonstration, setup() primarily configures the LED pin as an output.
Conversely, the commands inside the loop() function repeat regularly. Simply put, after executing the last line, the loop() function immediately returns to the first line.
We won't go into the specifics of the setup() and loop() functions' commands in the Blink sketch here but will discuss them in greater detail shortly.