Introduction to Arduino
Floor 4 - Physic Lab
Outline
Outline
Arduino Exercises
1
Arduino Structure
Arduino Programming Blocks
void setup() {
}
void loop() {
}
Arduino Programming Blocks
pinMode( pin, mode )
Pin Number
Mode
digitalWrite( pin, signal )
Pin Number
Mode
delay( milliseconds )
Delay Milliseconds
One thousandth of a second
Input parameter 1000 represents a delay of one second
Arduino Programming Blocks
pinMode( pin, mode )
Pin Number
Mode
digitalWrite( pin, signal )
Pin Number
Mode
delay( milliseconds )
Delay Milliseconds
One thousandth of a second
Input parameter 1000 represents a delay of one second
Arduino Programming Blocks
pinMode( pin, mode )
Pin Number
Mode
digitalWrite( pin, signal )
Pin Number
Mode
delay( milliseconds )
Delay Milliseconds
One thousandth of a second
Input parameter 1000 represents a delay of one second
Arduino - white board
Button Control
digitalRead( pin )
Pin Number
bool ledState = false;
The code above shows how to define a true or false variable in Arduino
delay( milliseconds )
To prevent the code within the `loop()` function from repeatedly executing while a button is held down, a `delay()` function can be implemented after the button press is detected.
Button Control
digitalRead( pin )
Pin Number
bool ledState = false;
The code above shows how to define a true or false variable in Arduino
delay( milliseconds )
To prevent the code within the `loop()` function from repeatedly executing while a button is held down, a `delay()` function can be implemented after the button press is detected.
Ex01 - Multiple LEDs with Single Button Cycle
Goal: Pressing the button cycles through 3 LEDs one by one.
Use a counter variable that increments on button press.
Reset to 0 after reaching 3.
Ex01 - Multiple LEDs with Single Button Cycle
Goal: Pressing the button cycles through 3 LEDs one by one.
Use a counter variable that increments on button press.
Reset to 0 after reaching 3.
Ex02 - Multiple LEDs with Single Button
Updated description to reflect the new “ping-pong” sketch
Goal: The three LEDs run automatically in a back-and-forth sweep (LED 1 → LED 2 → LED 3 → LED 2 → LED 1 → …), and every button press halves the delay, making the sweep faster.
State variables:
ledIndex
holds the currently lit LED.
direction
is +1 or –1 and determines whether the next step moves forward or backward.
Edge handling: When ledIndex
reaches either end (0 or numLEDs − 1
), flip direction
so the sweep reverses direction on the next step.
Ex02 - Multiple LEDs with Single Button
Updated description to reflect the new “ping-pong” sketch
Goal: The three LEDs run automatically in a back-and-forth sweep (LED 1 → LED 2 → LED 3 → LED 2 → LED 1 → …), and every button press halves the delay, making the sweep faster.
State variables:
ledIndex
holds the currently lit LED.
direction
is +1 or –1 and determines whether the next step moves forward or backward.
Edge handling: When ledIndex
reaches either end (0 or numLEDs − 1
), flip direction
so the sweep reverses direction on the next step.