Teach lesson
STM32 with Arduino API (2/4): sequence and buttons
Students extend an STM32 Arduino sketch with multiple LEDs and button input, then explain how sequence timing and button state affect output.
New to LabsLand? Create your teacher account
Learning Outcomes
Create an output sequence using a list of pins.
Read an active-LOW button and use it to change behavior.
Record two test states and explain the program decision.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
An active-LOW button
8 min
In this setup, the button we will use is connected to pin PC5. It is an
active-LOW button: when it is pressed, digitalRead(PC5) returns LOW;
when it is not pressed, it returns HIGH.
In the lab interface, use the control labelled Button 1 or B1. Press and
hold it while observing at least one full sequence, then release it before the
unpressed test. A quick click may finish before the program reads it.
When button 1 is pressed, which comparison represents the active-LOW input?
Sequence with fast mode
27 min
The program uses three RGB outputs already wired in the remote setup. setup()
configures the button as an input and each RGB pin as an output. loop() reads
the button once at the start of each three-color cycle, selects a wait in
milliseconds, and turns on each channel in order. A press during a cycle takes
effect on the next cycle, so hold B1 until the new cycle begins.
The helper turnOffAll() groups three repeated off actions under one clear name.
The button sets timing for the next RGB cycle
The program samples B1 at the start of loop(), chooses one waitTime, and
uses that value for the three RGB channels in that cycle.
Launch the STM32 Arduino lab from this block and open the
main.inoeditortab in CodeIDE.
Paste the starter program below. Replace TODO 1 with the comparison level
that means "pressed" and TODO 2 with a visibly faster wait from
100 msto300 ms. The normal wait is500 ms.Search the file for
TODO; replace every TODO before compiling. Save,compile, and wait for a successful build. Then upload and wait for success.
Test 1 - B1 released: observe one full three-color sequence and fill
the unpressed row.
Test 2 - B1 held: press and hold B1 through one full sequence, then release it
and fill the pressed row. Record the actual wait values in
ms.Do not edit the sequence until both initial rows and the explanation question
immediately below the table are complete.
#include <Arduino.h>
const int RGB_RED = PA8;
const int RGB_GREEN = PA9;
const int RGB_BLUE = PA10;
const int BUTTON_1 = PC5; // Active LOW: pressed == LOW
void turnOffAll() {
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 0);
}
void setup() {
pinMode(BUTTON_1, INPUT);
pinMode(RGB_RED, OUTPUT);
pinMode(RGB_GREEN, OUTPUT);
pinMode(RGB_BLUE, OUTPUT);
}
void loop() {
bool pressed = digitalRead(BUTTON_1) == TODO_PRESSED_LEVEL; // TODO 1: comparison level for a pressed active-LOW button
int waitTime = 500;
if (pressed) {
waitTime = TODO_FAST_WAIT_MS; // TODO 2: visible fast interval, in ms
}
turnOffAll();
analogWrite(RGB_RED, 180);
delay(waitTime);
turnOffAll();
analogWrite(RGB_GREEN, 180);
delay(waitTime);
turnOffAll();
analogWrite(RGB_BLUE, 180);
delay(waitTime);
}
Fill in the table after uploading the program and testing the button. Use only
two rows and leave the others blank if Teach shows more:
- Row 1: button not pressed.
- Row 2: button pressed.
- In Expected digitalRead value, write HIGH or LOW according to the
active-LOW logic.
- In Wait used, record the value in ms that the program applies.
- In Brief explanation, connect the button state with the observed speed.
| Button state | Expected digitalRead value | Wait used ms | Observed sequence | Brief explanation |
|---|---|---|---|---|
Explain how you know the button changed the program behavior. Use your two table rows as evidence.
Modify the sequence after the initial tests
10 min
Only after the two initial rows are complete, swap the first and last color
blocks so the sequence starts with blue and ends with red. Do not change either
wait value. Save, compile, upload, and repeat both button states. This separate
challenge checks that sequence order and button-controlled timing are different
rules.
Which two code blocks did you move? State the new color order and confirm, with
one observation for B1 released and one for B1 held, whether the timing rule
still worked.
Submit your code
10 min
Submit your final main.ino
Save main.ino before attaching it. The snapshot must show the final version,
the changed color order, and no remaining TODO token.
Why can an active-LOW button be confusing at first? Write one short rule that would help you avoid mistakes.