Community project

ESP32 Interactive Prototype

Photo of ESP32 Interactive Prototype
Generated with AI

nethulanimnal82

Published November 28, 2026

This interactive prototype uses an ESP32 to drive six LEDs through animated patterns, control two piezo buzzers for audio feedback, and respond to user input from two push buttons and two potentiometers. The guide includes a complete wiring diagram showing how to connect all components to the microcontroller, a detailed parts list with recommended component values, and the full firmware with debounced button handling and pattern animation logic.

Builders will learn how to implement smooth LED animations using bit-packed pattern arrays, handle reliable button input with software debouncing, and create interactive modes that respond to user controls. The assembly steps walk through connecting the LEDs with current-limiting resistors, wiring the push buttons and potentiometers to analog inputs, and setting up the buzzer outputs for sound generation.

Wiring diagram

Wiring diagram for ESP32 Interactive Prototype
Loading diagram…

Gather all the parts

QtyComponent
1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

Push Button

Momentary push button switch

1

10kΩ Potentiometer

10 kΩ linear (B10K) rotary potentiometer in the common 16 mm panel style: metal can, M7 threaded bushing for mounting through a panel, a 6 mm knurled split shaft for push-on knobs, and three PCB pins in a row 5 mm apart. Connect the outer pins across 3.3 V or 5 V and ground; the middle wiper pin then gives an adjustable voltage for an ADC input over about 300° of rotation.

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

Push Button

Momentary push button switch

1

10kΩ Potentiometer

10 kΩ linear (B10K) rotary potentiometer in the common 16 mm panel style: metal can, M7 threaded bushing for mounting through a panel, a 6 mm knurled split shaft for push-on knobs, and three PCB pins in a row 5 mm apart. Connect the outer pins across 3.3 V or 5 V and ground; the middle wiper pin then gives an adjustable voltage for an ADC input over about 300° of rotation.

Assemble it in 5 steps

1. Connect the six lights

Unplug USB first. Line up the LEDs in order. Connect D2 through D7 to the long legs of LEDs 1 through 6, with one 330-ohm resistor between each pin and its LED. Connect every short leg to Arduino GND. A shared breadboard ground rail makes this easier.

2. Connect the two buzzers

Use passive piezo buzzers. Connect their marked positive leads to D9 and D10 and their other leads to GND.

3. Connect the two buttons

Wire button 1 between D11 and GND and button 2 between D12 and GND. For a four-leg button, use two legs that only connect when pressed.

4. Connect the two knobs

On each knob, connect the two outside terminals to 5V and GND, one to each. Connect the middle terminal of the speed knob to A0 and the middle terminal of the pitch knob to A1.

5. Switch modes with both buttons

Press both buttons together and release both to switch modes. In chaser mode, tap button 1 to pause or resume and button 2 to skip songs. In car mode, tap button 1 to start or stop the engine. While running, hold button 1 to change engines, tap button 2 to shift up, or hold button 2 to shift down. Knob 1 is the throttle and knob 2 adjusts pitch. The six LEDs show revs and flash at redline.

Review all connections

1. Connections between "led1" and "Arduino"

Functionled1Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

2. Connections between "r1" and "Arduino"

Functionr1Arduino
digitalP1GPIO 2

3. Connections between "led2" and "Arduino"

Functionled2Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

4. Connections between "r2" and "Arduino"

Functionr2Arduino
digitalP1GPIO 3

5. Connections between "led3" and "Arduino"

Functionled3Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

6. Connections between "r3" and "Arduino"

Functionr3Arduino
digitalP1GPIO 4

7. Connections between "led4" and "Arduino"

Functionled4Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

8. Connections between "r4" and "Arduino"

Functionr4Arduino
digitalP1GPIO 5

9. Connections between "led5" and "Arduino"

Functionled5Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

10. Connections between "r5" and "Arduino"

Functionr5Arduino
digitalP1GPIO 6

11. Connections between "led6" and "Arduino"

Functionled6Arduino
digitalANODE → Resistor P2EXT
groundGNDGND

12. Connections between "r6" and "Arduino"

Functionr6Arduino
digitalP1GPIO 7

13. Connections between "buzz1" and "Arduino"

Functionbuzz1Arduino
pwmSIGGPIO 9
groundGNDGND

14. Connections between "button1" and "Arduino"

Functionbutton1Arduino
digitalSIGNALGPIO 11
groundGNDGND

15. Connections between "knob1" and "Arduino"

Functionknob1Arduino
powerVCC5V
groundGNDGND
adcWIPERGPIO 14

16. Connections between "buzz2" and "Arduino"

Functionbuzz2Arduino
pwmSIGGPIO 10
groundGNDGND

17. Connections between "button2" and "Arduino"

Functionbutton2Arduino
digitalSIGNALGPIO 12
groundGNDGND

18. Connections between "knob2" and "Arduino"

Functionknob2Arduino
powerVCC5V
groundGNDGND
adcWIPERGPIO 15

Deploy the firmware

#include <Arduino.h>
struct Button {
  uint8_t pin;
  bool raw = HIGH, stable = HIGH;
  unsigned long changed = 0;
  explicit Button(uint8_t p): pin(p) {}
  void update(unsigned long now) {
    bool value = digitalRead(pin);
    if (value != raw) { raw = value; changed = now; }
    if (raw == stable || now - changed < 25) return;
    stable = raw;
  }
};
Button pauseButton(11), nextSongButton(12);
const uint8_t buzz[] = {9, 10};
// Each row is one six-frame animation. Bit 0 drives LED 1 (D2).
const uint8_t patterns[][6] PROGMEM = {
  {1,2,4,8,16,32}, {32,16,8,4,2,1},
  {1,4,16,32,8,2}, {1,2,4,8,4,2}, {32,16,8,4,8,16},
  {3,6,12,24,48,33}, {33,48,24,12,6,3},
  {7,14,28,56,49,35}, {35,49,56,28,14,7},
  {15,30,60,57,51,39}, {62,61,59,55,47,31},
  {33,18,12,18,33,0}, {12,18,33,18,12,0},
  {33,51,63,30,12,0}, {12,30,63,51,33,0},
  {1,3,7,15,31,63}, {32,48,56,60,62,63},
  {63,62,60,56,48,32}, {63,31,15,7,3,1},
  {21,42,21,42,0,63}, {7,56,7,56,0,63},
  {9,18,36,18,9,0}, {36,18,9,18,36,0},
  {5,10,20,40,17,34}, {34,17,40,20,10,5},
  {1,33,2,18,4,12}, {8,12,16,18,32,33},
  {3,12,48,12,3,0}, {48,12,3,12,48,0},
  {1,6,24,32,24,6}, {32,24,6,1,6,24},
  {63,0,33,0,12,0}, {63,21,0,63,42,0},
  {12,30,63,30,12,0}, {33,51,63,51,33,0},
  {1,3,2,6,4,12}, {32,48,16,24,8,12}, {63,30,33,12,51,0},
  {1,8,2,16,4,32}, {32,4,16,2,8,1}, {9,27,63,54,36,0},
  {21,63,42,0,42,63}, {3,48,6,24,12,0}, {12,0,18,0,33,63},
  {31,15,7,56,60,62}, {5,21,17,34,42,40}, {1,12,32,12,18,0},
  {63,55,59,47,61,62}, {7,14,28,56,28,14}, {33,0,18,63,12,0}
};
constexpr uint8_t patternCount = sizeof(patterns) / sizeof(patterns[0]);
static_assert(patternCount == 50, "Expected 50 patterns");
uint8_t activeBuzz = 0, pattern = 0, frame = 0, repeats = 0;
struct Note { uint16_t hz; uint8_t units; };
// Neon Circuit
const Note song0[] PROGMEM = {{220,1},{330,1},{440,1},{523,1},{659,1},{523,1},{440,1},{330,1},{175,1},{262,1},{349,1},{440,1},{523,1},{440,1},{349,1},{262,1},{196,1},{294,1},{392,1},{494,1},{587,1},{494,1},{392,1},{294,1},{165,1},{247,1},{330,1},{415,1},{494,1},{587,1},{659,2}};
// Garage Voltage
const Note song1[] PROGMEM = {{330,2},{0,1},{330,1},{392,1},{330,1},{440,1},{392,1},{330,2},{0,1},{294,1},{330,3},{0,1},{330,2},{0,1},{330,1},{392,1},{440,1},{466,1},{440,1},{392,1},{330,2},{294,1},{392,1},{330,3},{0,1}};
// Midnight Sneak
const Note song2[] PROGMEM = {{294,2},{0,1},{349,1},{415,1},{440,2},{523,1},{440,1},{0,1},{415,1},{349,1},{294,2},{0,1},{294,1},{349,1},{392,1},{415,2},{392,1},{349,1},{330,2},{262,1},{294,3},{0,1}};
// Final Boss
const Note song3[] PROGMEM = {{294,1},{294,1},{440,1},{294,1},{466,1},{294,1},{523,1},{466,1},{294,1},{294,1},{440,1},{294,1},{698,2},{659,2},{233,1},{233,1},{349,1},{233,1},{587,1},{523,1},{466,1},{349,1},{220,1},{220,1},{330,1},{220,1},{554,1},{659,1},{587,2}};
// Afterburn
const Note song4[] PROGMEM = {{165,1},{165,1},{196,1},{220,2},{247,1},{294,1},{247,1},{220,1},{196,1},{165,3},{0,1}};
// Glass Skyline
const Note song5[] PROGMEM = {{262,1},{330,1},{392,1},{494,2},{392,1},{330,1},{294,1},{349,1},{440,1},{523,2},{494,1},{392,3}};
// Rust Drive
const Note song6[] PROGMEM = {{220,2},{0,1},{220,1},{262,1},{294,1},{311,1},{330,2},{294,1},{262,1},{196,1},{220,3},{0,1}};
// Moon Terminal
const Note song7[] PROGMEM = {{175,1},{262,1},{330,1},{440,3},{392,1},{330,1},{294,1},{220,1},{262,1},{349,3},{0,1}};
// Redline
const Note song8[] PROGMEM = {{196,1},{294,1},{392,1},{294,1},{466,1},{392,1},{349,1},{294,1},{311,1},{349,1},{294,1},{233,1},{262,1},{294,1},{196,2}};
// Signal Lost
const Note song9[] PROGMEM = {{247,2},{370,1},{294,1},{0,1},{277,1},{330,1},{392,2},{370,1},{277,1},{247,4},{0,1}};
// Concrete Swing
const Note song10[] PROGMEM = {{349,2},{440,1},{523,1},{440,2},{415,1},{392,2},{311,1},{349,3},{0,1},{262,1},{311,1},{349,2}};
// Ice Sector
const Note song11[] PROGMEM = {{294,1},{440,1},{523,1},{349,1},{440,1},{523,1},{659,2},{587,1},{440,1},{392,1},{330,1},{349,1},{294,3}};
// Desert Radio
const Note song12[] PROGMEM = {{330,2},{349,1},{415,1},{440,2},{415,1},{349,1},{330,1},{294,1},{262,1},{294,1},{349,1},{330,3},{0,1}};
// Copper Coil
const Note song13[] PROGMEM = {{131,2},{262,1},{196,1},{233,1},{262,2},{0,1},{233,1},{196,1},{175,1},{208,1},{233,1},{262,3}};
// Night Ferry
const Note song14[] PROGMEM = {{220,1},{330,1},{440,1},{523,3},{494,1},{392,1},{330,1},{349,1},{440,1},{294,1},{330,1},{220,3}};
// Pixel Pursuit
const Note song15[] PROGMEM = {{262,1},{523,1},{392,1},{330,1},{294,1},{587,1},{440,1},{349,1},{330,1},{659,1},{494,1},{392,1},{440,1},{392,1},{523,2},{0,1}};
// Blacktop
const Note song16[] PROGMEM = {{147,2},{294,1},{349,1},{392,1},{349,1},{294,1},{262,1},{220,2},{262,1},{294,1},{349,1},{330,1},{294,3}};
// Apollo Exit
const Note song17[] PROGMEM = {{196,1},{247,1},{294,1},{392,2},{330,1},{494,1},{440,2},{370,1},{294,1},{330,1},{392,3}};
// Blue Static
const Note song18[] PROGMEM = {{233,1},{294,1},{349,1},{415,2},{349,1},{0,1},{277,1},{311,1},{330,1},{349,2},{311,1},{233,3}};
// Last Elevator
const Note song19[] PROGMEM = {{277,1},{415,1},{330,1},{494,2},{370,1},{440,1},{554,2},{494,1},{415,1},{330,1},{311,1},{277,3},{0,1}};
const Note song20[] PROGMEM = {{196,2},{196,1},{165,1},{233,2},{0,1},{220,1},{294,1},{247,1},{233,2},{220,1},{196,1},{196,3},{165,2},{0,1}};
const Note song21[] PROGMEM = {{165,1},{220,1},{247,2},{220,1},{165,2},{0,1},{247,1},{233,2},{294,1},{233,1},{220,1},{196,3},{165,2},{0,1}};
const Note song22[] PROGMEM = {{165,1},{233,2},{196,1},{233,1},{196,1},{165,2},{0,1},{247,1},{330,1},{294,2},{165,1},{220,3},{165,2},{0,1}};
const Note song23[] PROGMEM = {{220,1},{165,1},{220,1},{233,2},{0,1},{247,1},{220,1},{294,1},{247,1},{233,2},{220,1},{196,3},{165,2},{0,1}};
const Note song24[] PROGMEM = {{165,2},{165,1},{233,2},{247,1},{196,1},{0,1},{247,1},{330,2},{294,1},{233,1},{220,1},{196,3},{165,2},{0,1}};
const Note song25[] PROGMEM = {{220,2},{294,1},{330,1},{311,2},{0,1},{392,1},{330,1},{294,1},{262,2},{311,1},{294,1},{262,3},{220,2},{0,1}};
const Note song26[] PROGMEM = {{440,1},{392,1},{330,2},{220,1},{311,2},{0,1},{294,1},{311,2},{330,1},{311,1},{294,1},{262,3},{220,2},{0,1}};
const Note song27[] PROGMEM = {{262,1},{311,2},{392,1},{330,1},{311,1},{392,2},{0,1},{440,1},{311,1},{294,2},{220,1},{262,3},{220,2},{0,1}};
const Note song28[] PROGMEM = {{220,1},{262,1},{311,1},{330,2},{0,1},{311,1},{330,1},{440,1},{523,1},{440,2},{392,1},{330,3},{220,2},{0,1}};
const Note song29[] PROGMEM = {{330,2},{294,1},{294,2},{311,1},{330,1},{0,1},{392,1},{330,2},{311,1},{294,1},{262,1},{294,3},{220,2},{0,1}};
const Note song30[] PROGMEM = {{175,2},{262,1},{349,1},{262,2},{0,1},{220,1},{294,1},{220,1},{349,2},{262,1},{330,1},{220,3},{175,2},{0,1}};
const Note song31[] PROGMEM = {{196,1},{294,1},{440,2},{294,1},{196,2},{0,1},{294,1},{262,2},{330,1},{262,1},{220,1},{196,3},{175,2},{0,1}};
const Note song32[] PROGMEM = {{220,1},{349,2},{330,1},{262,1},{294,1},{220,2},{0,1},{294,1},{440,1},{330,2},{262,1},{196,3},{175,2},{0,1}};
const Note song33[] PROGMEM = {{175,1},{220,1},{262,1},{349,2},{0,1},{175,1},{330,1},{196,1},{294,1},{349,2},{262,1},{220,3},{175,2},{0,1}};
const Note song34[] PROGMEM = {{262,2},{440,1},{330,2},{262,1},{220,1},{0,1},{294,1},{196,2},{262,1},{330,1},{220,1},{196,3},{175,2},{0,1}};
const Note song35[] PROGMEM = {{262,2},{392,1},{523,1},{392,2},{0,1},{494,1},{330,1},{440,1},{349,2},{294,1},{392,1},{330,3},{262,2},{0,1}};
const Note song36[] PROGMEM = {{523,1},{294,1},{523,2},{262,1},{494,2},{0,1},{392,1},{330,2},{440,1},{349,1},{392,1},{294,3},{262,2},{0,1}};
const Note song37[] PROGMEM = {{330,1},{392,2},{494,1},{262,1},{523,1},{440,2},{0,1},{349,1},{294,1},{494,2},{392,1},{330,3},{262,2},{0,1}};
const Note song38[] PROGMEM = {{440,1},{440,1},{262,1},{392,2},{0,1},{349,1},{330,1},{392,1},{494,1},{440,2},{523,1},{294,3},{262,2},{0,1}};
const Note song39[] PROGMEM = {{294,2},{330,1},{349,2},{392,1},{494,1},{0,1},{392,1},{330,2},{262,1},{523,1},{440,1},{392,3},{262,2},{0,1}};
const Note song40[] PROGMEM = {{196,2},{196,1},{294,1},{233,2},{0,1},{262,1},{294,1},{196,1},{311,2},{196,1},{262,1},{294,3},{196,2},{0,1}};
const Note song41[] PROGMEM = {{220,1},{262,1},{220,2},{349,1},{294,2},{0,1},{233,1},{294,2},{196,1},{294,1},{262,1},{220,3},{196,2},{0,1}};
const Note song42[] PROGMEM = {{294,1},{262,2},{294,1},{220,1},{349,1},{220,2},{0,1},{196,1},{262,1},{311,2},{233,1},{220,3},{196,2},{0,1}};
const Note song43[] PROGMEM = {{233,1},{196,1},{233,1},{311,2},{0,1},{262,1},{294,1},{262,1},{233,1},{220,2},{294,1},{233,3},{196,2},{0,1}};
const Note song44[] PROGMEM = {{196,2},{196,1},{311,2},{262,1},{196,1},{0,1},{294,1},{311,2},{349,1},{196,1},{233,1},{262,3},{196,2},{0,1}};
const Note song45[] PROGMEM = {{147,2},{220,1},{294,1},{262,2},{0,1},{330,1},{220,1},{196,1},{294,2},{175,1},{262,1},{196,3},{147,2},{0,1}};
const Note song46[] PROGMEM = {{175,1},{262,1},{330,2},{262,1},{349,2},{0,1},{294,1},{330,2},{220,1},{196,1},{220,1},{175,3},{147,2},{0,1}};
const Note song47[] PROGMEM = {{196,1},{294,2},{175,1},{220,1},{330,1},{262,2},{0,1},{294,1},{220,1},{175,2},{196,1},{175,3},{147,2},{0,1}};
const Note song48[] PROGMEM = {{220,1},{330,1},{196,1},{294,2},{0,1},{349,1},{262,1},{330,1},{220,1},{175,2},{147,1},{196,3},{147,2},{0,1}};
const Note song49[] PROGMEM = {{147,2},{262,1},{349,2},{294,1},{196,1},{0,1},{262,1},{220,2},{294,1},{175,1},{330,1},{196,3},{147,2},{0,1}};
struct Song { const Note *notes; uint8_t count; uint16_t unitMs; };
const Song songs[] PROGMEM = {
  {song0, sizeof(song0) / sizeof(Note), 105},
  {song1, sizeof(song1) / sizeof(Note), 132},
  {song2, sizeof(song2) / sizeof(Note), 151},
  {song3, sizeof(song3) / sizeof(Note), 93},
  {song4, sizeof(song4) / sizeof(Note), 110},
  {song5, sizeof(song5) / sizeof(Note), 140},
  {song6, sizeof(song6) / sizeof(Note), 124},
  {song7, sizeof(song7) / sizeof(Note), 176},
  {song8, sizeof(song8) / sizeof(Note), 88},
  {song9, sizeof(song9) / sizeof(Note), 155},
  {song10, sizeof(song10) / sizeof(Note), 130},
  {song11, sizeof(song11) / sizeof(Note), 107},
  {song12, sizeof(song12) / sizeof(Note), 150},
  {song13, sizeof(song13) / sizeof(Note), 115},
  {song14, sizeof(song14) / sizeof(Note), 168},
  {song15, sizeof(song15) / sizeof(Note), 94},
  {song16, sizeof(song16) / sizeof(Note), 127},
  {song17, sizeof(song17) / sizeof(Note), 136},
  {song18, sizeof(song18) / sizeof(Note), 145},
  {song19, sizeof(song19) / sizeof(Note), 160},
  {song20, sizeof(song20) / sizeof(Note), 112},
  {song21, sizeof(song21) / sizeof(Note), 115},
  {song22, sizeof(song22) / sizeof(Note), 118},
  {song23, sizeof(song23) / sizeof(Note), 121},
  {song24, sizeof(song24) / sizeof(Note), 124},
  {song25, sizeof(song25) / sizeof(Note), 148},
  {song26, sizeof(song26) / sizeof(Note), 151},
  {song27, sizeof(song27) / sizeof(Note), 154},
  {song28, sizeof(song28) / sizeof(Note), 157},
  {song29, sizeof(song29) / sizeof(Note), 160},
  {song30, sizeof(song30) / sizeof(Note), 104},
  {song31, sizeof(song31) / sizeof(Note), 107},
  {song32, sizeof(song32) / sizeof(Note), 110},
  {song33, sizeof(song33) / sizeof(Note), 113},
  {song34, sizeof(song34) / sizeof(Note), 116},
  {song35, sizeof(song35) / sizeof(Note), 87},
  {song36, sizeof(song36) / sizeof(Note), 90},
  {song37, sizeof(song37) / sizeof(Note), 93},
  {song38, sizeof(song38) / sizeof(Note), 96},
  {song39, sizeof(song39) / sizeof(Note), 99},
  {song40, sizeof(song40) / sizeof(Note), 120},
  {song41, sizeof(song41) / sizeof(Note), 123},
  {song42, sizeof(song42) / sizeof(Note), 126},
  {song43, sizeof(song43) / sizeof(Note), 129},
  {song44, sizeof(song44) / sizeof(Note), 132},
  {song45, sizeof(song45) / sizeof(Note), 182},
  {song46, sizeof(song46) / sizeof(Note), 185},
  {song47, sizeof(song47) / sizeof(Note), 188},
  {song48, sizeof(song48) / sizeof(Note), 191},
  {song49, sizeof(song49) / sizeof(Note), 194},
};
constexpr uint8_t songCount = sizeof(songs) / sizeof(songs[0]);
static_assert(songCount == 50, "Expected 50 songs");
uint8_t song = 0, noteIndex = 0;
bool running = true, startNote = true;
unsigned long lastStep, lastNote, noteInterval;

void showFrame() {
  uint8_t mask = pgm_read_byte(&patterns[pattern][frame]);
  for (uint8_t i = 0; i < 6; ++i) digitalWrite(2 + i, (mask >> i) & 1);
}

void setup() {
  for (uint8_t pin = 2; pin <= 7; ++pin) pinMode(pin, OUTPUT);
  for (auto pin : buzz) pinMode(pin, OUTPUT);
  pinMode(pauseButton.pin, INPUT_PULLUP);
  pinMode(nextSongButton.pin, INPUT_PULLUP);
  showFrame();
  lastStep = millis();
}

void playMelody(unsigned long now) {
  if (!startNote && now - lastNote < noteInterval) return;
  startNote = false;
  lastNote = now;
  Song current;
  memcpy_P(&current, &songs[song], sizeof(current));
  const Note &note = current.notes[noteIndex];
  noteInterval = (unsigned long)current.unitMs * pgm_read_byte(&note.units);
  unsigned hz = pgm_read_word(&note.hz);
  noTone(buzz[activeBuzz]);
  if (hz) {
    activeBuzz ^= 1;
    unsigned scale = map(analogRead(A1), 0, 1023, 75, 150);
    tone(buzz[activeBuzz], hz * (unsigned long)scale / 100, noteInterval * 9 / 10);
  }
  noteIndex = (noteIndex + 1) % current.count;
}

// Car mode uses the existing throttle/pitch knobs and first piezo.
bool carMode, engineOn, chord, held;
uint8_t engine, gear = 1, firingStep;
uint16_t rpm, previousThrottle, lastHz;
unsigned long gestureAt, carTick, effectAt;
uint8_t gesture, effect; // 1 = startup, 2 = turbo flutter

void silence() {
  for (auto pin : buzz) noTone(pin);
  lastHz = 0;
}

void runCar(unsigned long now) {
  if (!engineOn || now - carTick < 8) return;
  carTick = now;
  int throttle = analogRead(A0);
  if (previousThrottle > 900 && throttle < 400 && effect != 1) {
    effect = 2;
    effectAt = now;
  }
  previousThrottle = throttle;
  int delta = throttle - int(rpm);
  rpm += delta / (delta > 0 ? 6 : 11);
  constexpr uint8_t ratio[] = {100, 75, 55, 42, 33, 26};
  int base = constrain(map(rpm, 0, 1023, 80, 800) * ratio[gear-1] / 100
                       + map(analogRead(A1), 0, 1023, -30, 120), 32, 1000);
  uint8_t phase = firingStep++;
  unsigned hz;
  switch (engine) {
    case 0: hz = 2 * base + (phase % 10) * 5; break; // V10
    case 1: hz = base * (phase % 2 ? 16 : 15) / 10; break; // Flat-6
    case 2: hz = base * (phase % 4 == 2 ? 8 : 12) / 10; break; // V8
    default: hz = base * (phase % 5 == 4 ? 13 : phase % 2 ? 7 : 10) / 10;
  }
  unsigned long elapsed = now - effectAt;
  if (effect == 1 && elapsed < 625) {
    hz = elapsed < 375 ? 100 + 20 * (elapsed / 25) : 950;
  } else if (effect == 2 && elapsed < 540) {
    uint8_t burst = elapsed / 180;
    unsigned t = elapsed % 180;
    hz = t < 105 ? 600 - 90 * burst - 3 * t : 0;
  } else effect = 0;
  if (hz != lastHz) {
    if (hz) tone(buzz[0], hz);
    else noTone(buzz[0]);
    lastHz = hz;
  }
  for (uint8_t i = 0; i < 6; ++i)
    digitalWrite(2+i, rpm >= 850 ? now % 300 < 150 : i < 1 + rpm / 170);
}

void carButton(uint8_t button, bool longPress, unsigned long now) {
  if (!longPress && button == 1) {
    engineOn = !engineOn;
    if (engineOn) { effect = 1; effectAt = now; }
    else {
      silence();
      rpm = previousThrottle = 0;
      gear = 1;
      for (uint8_t pin = 2; pin <= 7; ++pin) digitalWrite(pin, LOW);
    }
  } else if (engineOn && button == 1) {
    engine = (engine + 1) % 4;
    effect = 1;
    effectAt = now;
  } else if (engineOn && longPress && gear > 1) --gear;
  else if (engineOn && !longPress && gear < 6) ++gear;
}

void loop() {
  unsigned long now = millis();
  pauseButton.update(now);
  nextSongButton.update(now);
  uint8_t buttons = (pauseButton.stable == LOW) | ((nextSongButton.stable == LOW) << 1);
  if (buttons == 3 && !chord) {
    chord = true;
    gesture = 0;
    carMode = !carMode;
    silence();
    if (carMode) {
      engineOn = false;
      rpm = previousThrottle = 0;
      gear = 1;
      for (uint8_t pin = 2; pin <= 7; ++pin) digitalWrite(pin, LOW);
    } else {
      showFrame();
      lastStep = now;
      startNote = true;
    }
  }
  if (chord) {
    if (!buttons) chord = false; // Ignore both buttons until fully released.
  } else if (buttons) {
    if (!gesture) { gesture = buttons; gestureAt = now; held = false; }
    if (carMode && !held && now - gestureAt >= 800 && engineOn
        && pauseButton.raw == pauseButton.stable && nextSongButton.raw == nextSongButton.stable) {
      carButton(gesture, true, now);
      held = true;
    }
  } else if (gesture) {
    if (carMode) { if (!held) carButton(gesture, false, now); }
    else if (gesture == 2) {
      song = (song + 1) % songCount;
      noteIndex = 0;
      startNote = true;
    } else {
      running = !running;
      lastStep = now;
      startNote = true;
      silence();
    }
    gesture = 0;
  }
  if (carMode) { runCar(now); return; }
  if (!running) return;
  playMelody(now);
  unsigned long interval = map(analogRead(A0), 0, 1023, 800, 80);
  if (now - lastStep < interval) return;
  lastStep = now;
  if (++frame == 6) {
    frame = 0;
    if (++repeats == 5) {
      repeats = 0;
      pattern = (pattern + 1) % patternCount;
    }
  }
  showFrame();
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik