Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Arduino Practice Problems

Test your Arduino skills with exercises on debouncing, map(), bit manipulation, timing, button patterns and sensor filtering, each with a hint and a runnable C stand-in solution.

Reading is not the same as doing. This page collects small problems that exercise the core ideas of the course: timing without delay(), debouncing, scaling values, bits and bytes, filtering and state. For each one try to write your own solution first (on a board, or in your head), then compare it with the hint and the solution.

Boards cannot run here, so each problem has a labelled C stand-in: the same logic using fake time and scripted inputs, which you can run, edit and extend. Where Arduino code is shown it is read-only.

Problem 1: Button debouncing

A mechanical button does not switch cleanly: for a few milliseconds the contact bounces between open and closed, so one press may look like five. Write a function that reports a stable state: the input must stay the same for 20 ms before it counts.

Hint: remember the last raw reading and when it last changed; only accept the reading once it has been unchanged for the debounce interval.

C++
const unsigned long DEBOUNCE_MS = 20;
int lastRaw = HIGH;
int stable = HIGH;
unsigned long lastChange = 0;

bool pressedEvent(int pin) {
  int raw = digitalRead(pin);
  if (raw != lastRaw) {
    lastRaw = raw;
    lastChange = millis();
  }
  if (millis() - lastChange >= DEBOUNCE_MS && raw != stable) {
    stable = raw;
    return stable == LOW;            // pressed (INPUT_PULLUP wiring)
  }
  return false;
}
C Solution stand-in: debouncing a bouncy press (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Problem 2: Reimplement map()

Arduino's map(x, in_min, in_max, out_min, out_max) rescales a number from one range to another using integer math. Write it, then find out what happens for input values outside the input range and why you might combine it with constrain().

Hint: (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min. Integer division truncates, so the output reaches out_max only when x equals in_max.

C Solution stand-in: map() and constrain() (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice 1500 maps to 373 and -100 maps to -24: map() does not clamp. This is the cause of many "my servo goes crazy" bugs.

Problem 3: Set, clear and toggle bits

An LED_STATE byte holds eight LED outputs, one per bit. Write functions to turn LED n on, off, toggle it and test it. Then count how many are on.

Hint: state |= (1 << n) sets, state &= ~(1 << n) clears, state ^= (1 << n) toggles, (state >> n) & 1 tests. Arduino also has the macros bitSet, bitClear, bitRead, bitWrite.

C Solution stand-in: bit operations on a status byte (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Blink three LEDs at 300 ms, 700 ms and 1100 ms, independently, with no delay(). Use a small array of timers, so adding a fourth LED needs one more table entry.

Hint: each LED stores its pin, interval, last toggle time and state. The loop checks them all. This is the pattern behind the Blinker class in writing your own library.

C++
struct Led {
  byte pin;
  unsigned long interval;
  unsigned long last;
  bool on;
};

Led leds[] = { {8, 300, 0, false}, {9, 700, 0, false}, {10, 1100, 0, false} };

void setup() {
  for (auto &l : leds) pinMode(l.pin, OUTPUT);
}

void loop() {
  unsigned long now = millis();
  for (auto &l : leds) {
    if (now - l.last >= l.interval) {
      l.last = now;
      l.on = !l.on;
      digitalWrite(l.pin, l.on);
    }
  }
}
C Solution stand-in: three independent timers (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Problem 5: Count presses and detect a long press

A button should do one thing on a short press (under 500 ms) and another on a long press (500 ms or more), decided when the button is released. Record the time at the press and compare at release.

Hint: keep the press timestamp; on the release edge compute held = now - pressedAt.

C Solution stand-in: short and long presses (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Problem 6: A fade with a triangle wave

Make an LED fade up and down smoothly using analogWrite without delay() (a duty that bounces between 0 and 255 in steps of 5 every 10 ms). Combine ideas from timing, PWM and a direction variable.

C Solution stand-in: bouncing duty cycle (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Problem 7: Median of three

Sensors sometimes glitch. A median-of-three filter returns the middle value of the last three readings and discards single-sample spikes. Write median3(a, b, c) without sorting an array.

Hint: the median is max(min(a,b), min(max(a,b), c)).

C Solution stand-in: median of three (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

More challenges to try on a real board

  • A reaction-time game: an LED lights after a random delay; measure how fast the button is pressed and print it.
  • A Morse code sender for a string typed into the serial monitor (see the command parser).
  • A dimmable lamp: a potentiometer sets brightness, a button toggles on and off, and the last brightness is stored in EEPROM.
  • A parking sensor: the ultrasonic sensor controls a buzzer; the closer, the faster it beeps.
  • A servo-based gauge showing a measured value with a smooth sweep (servo lesson).
  • A tiny menu on a 16x2 display navigated with two buttons.
  • A low-power sensor node that wakes every minute (sleep modes).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does map(1500, 0, 1023, 0, 255) return a value above 255?

    map() just applies the linear formula. Use constrain() to limit the result.
  2. 2. Which statement clears bit 3 of a byte called flags?

    AND with the inverted mask clears the bit and leaves the others unchanged.
  3. 3. A debounced input is accepted when it...

    The stable state is only updated when the reading has not changed for the debounce interval, typically 20 to 50 ms.
  4. 4. What does median-of-three do with a one-sample spike?

    A single spike is the maximum (or minimum) of the three, so it never becomes the middle value.

Summary

  • Debounce by requiring a stable reading for 20 ms or more.
  • map() does not clamp; combine it with constrain().
  • Bit operations (|=, &= ~, ^=) manage many flags in one byte.
  • Several independent timers share one loop with a table of intervals; filters such as median-of-three remove glitches.

Next: Back to the Arduino tutorial