0
0
Arduinoprogramming~10 mins

Code organization with functions in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Code organization with functions
Start
Define function
Setup function runs once
Loop function runs repeatedly
Call function inside loop
Function executes code block
Return to loop
Loop function runs repeatedly
The program starts by defining functions, then runs setup once, then repeatedly runs loop, calling functions inside loop to organize code.
Execution Sample
Arduino
void blinkLED() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  blinkLED();
}
This code defines a function to blink the LED, sets the LED pin mode once, then repeatedly calls the blink function to blink the LED.
Execution Table
StepFunction CalledActionPin StateDelay(ms)
1setupSet LED pin as OUTPUTLED pin mode=OUTPUT0
2loopCall blinkLED()LED LOW0
3blinkLEDdigitalWrite HIGHLED ON0
4blinkLEDdelay(500)LED ON500
5blinkLEDdigitalWrite LOWLED OFF500
6blinkLEDdelay(500)LED OFF1000
7loopCall blinkLED() againLED OFF1000
8blinkLEDdigitalWrite HIGHLED ON1000
9blinkLEDdelay(500)LED ON1500
10blinkLEDdigitalWrite LOWLED OFF1500
11blinkLEDdelay(500)LED OFF2000
...loop continues.........
💡 Loop runs forever, repeatedly calling blinkLED to blink LED on and off.
Variable Tracker
VariableStartAfter setupAfter blinkLED 1After blinkLED 2...final
LED_BUILTIN stateLOWConfigured OUTPUTHIGH then LOWHIGH then LOWRepeating HIGH/LOW
Key Moments - 3 Insights
Why do we define blinkLED() before using it in loop()?
Arduino requires functions to be defined before they are called, so the compiler knows what blinkLED() means when loop() calls it (see execution_table step 2).
Does setup() run multiple times?
No, setup() runs only once at the start to initialize things like pin modes (see execution_table step 1). Loop() runs repeatedly.
What happens if we put the blink code directly inside loop() instead of a function?
The code would work the same, but using a function like blinkLED() keeps code organized and easier to read or reuse (see execution_table steps 2-6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED pin state right after step 3?
ALED is ON (HIGH)
BLED is OFF (LOW)
CLED pin mode is set
DLED is blinking
💡 Hint
Check step 3 in execution_table where digitalWrite HIGH is called.
At which step does the program first wait for 500 milliseconds?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for delay(500) in execution_table rows.
If we remove the blinkLED() function and put its code directly in loop(), what changes in the execution_table?
ADelay will be removed
BPin State will never change
CFunction Called column will show 'loop' only
DSetup will run multiple times
💡 Hint
Without blinkLED(), loop() contains all code, so only loop() is called.
Concept Snapshot
Functions organize code into reusable blocks.
Define functions before calling them.
setup() runs once; loop() runs repeatedly.
Call functions inside loop() to keep code clean.
Use digitalWrite and delay inside functions for actions.
Full Transcript
This example shows how Arduino code uses functions to organize tasks. First, a function blinkLED() is defined to turn the LED on and off with delays. The setup() function runs once to set the LED pin as output. Then the loop() function runs forever, calling blinkLED() each time to blink the LED. The execution table traces each step: setup configures the pin, loop calls blinkLED, which changes the LED state and waits. Variables track the LED pin state changing from LOW to HIGH and back. Key moments clarify why functions must be defined before use, that setup runs once, and how functions help keep code organized. The quiz checks understanding of LED states, timing, and function calls. This method helps beginners see how code flows and how functions improve readability and reuse.