AI agents & screen readers: for a machine-readable, text-only catalogue, start at /llms.txt. Products are available as Markdown (/products.md, /products/{handle}.md) and JSON (/products.json, /products/{handle}.json).
Store
Press to light
Beginner 25 minutes

Read the shield’s button and make an output follow your hand.

View Markdown

Step 1 — What you will discover

Distinguish a momentary input from stored state and connect a true-or-false decision to an output.

Predict: Will the light stay on after you release the button?

Before you begin

Recall INPUT on shield D7; a high level lasts throughout a hold.

Your goal: Distinguish levels from events. Show this with a prediction, a tested change and an explanation using the program’s names.

Retrieve one idea: what input, state or output did you change in the previous project?

Need a reminder? Build an automatic night light.

Step 2 — Meet this circuit

Use an Arduino Uno with the Crack the Code shield. The shield already connects its LEDs, knob, light sensor and buttons; this activity needs no jumper wires or breadboard.

If the shield is not fitted, disconnect USB power, align every shield pin with the Uno sockets, and press evenly without bending the pins. Reconnect the Uno with a USB data cable. Leave the traffic-light module disconnected for this activity.

Step 3 — Upload your program

Open the example below. Read the English plan, then choose Arduino C++ from the language selector and Arduino Uno from the board selector. Select Verify to compile. Select Pair board…, choose the Uno’s serial port, then select Upload. Use Chrome or Edge on a desktop computer with this HTTPS page for browser upload. If browser upload is unavailable, copy the complete C++ sketch into Arduino IDE, choose Arduino Uno and its port, and upload there.

Each upload replaces the previous program. Edit the supplied C++ when trying the challenges, then verify and upload again.

After upload, select Open Serial Monitor and set 9600 baud. Close other serial programs first so only one application owns the port. The Plot tab can display the labelled numeric readings.

Step 4 — Read, edit and run

The English plan and complete Arduino sketch describe the same program. Keep one working copy before making changes.

Press to lightOpen in English ↗

Step 5 — Run it and collect evidence

Press the button labelled D7. D12 should stay on while you hold it and go off when you release it. The console reports 1 when pressed and 0 when released. Use D7, not the reset button or the second D6 button.

Step 6 — Explain what happened

Pressing the D7 button makes its input HIGH and turns D12 on. Releasing makes the input LOW and turns D12 off.

The shield supplies the button’s bias circuit. Its D7 reading is HIGH when pressed, so the program uses INPUT and compares with HIGH. This is the shield’s wiring convention; a loose button circuit may work differently.

The pressed variable is a Boolean: true or false. Every loop reads it again. Because the output follows the current reading, releasing the button immediately removes the condition that lights the LED.

Read this part of the actual starter

void loop() {
  bool pressed = digitalRead(buttonPin) == HIGH;
  digitalWrite(ledPin, pressed ? HIGH : LOW);
  Serial.print("pressed:"); Serial.println(pressed ? 1 : 0);
  delay(20);
}

The shield button is active-HIGH and has its own bias circuit, so this sketch uses INPUT. digitalRead(buttonPin) == HIGH compares the level and stores a Boolean in local pressed. The ternary expression maps true to HIGH and false to LOW.

Work through one case

Released → pressed → held → released gives false → true → true → false. A held button remains true over many loop passes.

Your turn: complete the trace

Released, held, held, released gives pressed=false, ___, ___, ___.

Compare your trace after trying

true, true, false.

If your answer differs

Count loop passes, not just finger presses.

Step 7 — Change one thing

Make the LED normally on and turn off while pressed by swapping HIGH and LOW in the output expression. Then return to the original and choose a different LED from D8–D13.

Run a controlled experiment

Make the LED indicate released instead of pressed. Explain both the code change and why this is not a toggle.

  1. Save a copy of the working starter. Reset the board so stored state begins from the declared values.
  2. Write the expected result before editing. Change only the named factor; keep wiring and other settings fixed.
  3. Edit the C++ in the editor, Verify, then Upload to the connected Uno. The starter simulation does not execute your edited C++.
  4. Repeat the same input sequence. Record input, expected output, observed output and an explanation. Use labelled serial values where the sketch provides them.
  5. If the result differs, inspect the relevant condition and pin before changing another factor. Restore and upload the saved starter to recover.

Core task: explain one changed case. Optional extension: choose a boundary or timing case and justify the extra test. Use a paper trace or annotated screenshot when physical manipulation is inaccessible; distinguish predictions from measurements.

Step 8 — Check your understanding

Explain the difference between this momentary switch and a lamp that stays on after one press. If it works backwards, compare the condition and output states with the supplied code; do not enable a pull-up to hide the mistake.

If nothing changes: check the power light, successful upload and the selected Uno port. Disconnect power before reseating a shield. Read the first compiler error before changing several lines at once.

Independent check — try before revealing

Explain why placing ledOn = !ledOn under if(pressed) would not produce one reliable toggle per press.

Hint

Count loop passes, not just finger presses.

Reasoning and feedback

Every loop during the hold would invert the LED again. A toggle needs a new-press event, not the continuing level.

Do not substitute INPUT_PULLUP without checking the actual circuit. A physical level is not one new event per press.

If your explanation missed a condition or stored value, add that column to your trace and try a new input. A working upload alone does not answer this check.

Step 9 — Look inside the push button

The sketch reads D7 and immediately copies its pressed/released state to the D12 LED command.

Choose Cutaway. Move Button travel from released to fully pressed, hold it there, then release it. Watch the spring dome meet and leave the contact. Compare the D7 state and voltage at each point; on this shield a pressed button reads HIGH.

Predict and explain: At full press, what voltage and digital state do you expect? What happens when you release the contact?

Check your explanation

The illustrated active-HIGH input reads about 5 V / HIGH while pressed and about 0 V / LOW when released. This sketch follows that level, so its LED turns off on release. The switch is momentary: its contact opens on release. Any remembered output belongs to the program. The cutaway shows a clean contact transition; real switches may bounce, which is why some sketches debounce their input.

Maddy, co-founder of Little Bird

Need help? We're here for you!

Hi, I'm Maddy. My team and I are ready to help with your order or any questions.