Dance pad on a Pro Micro
Four FSR panels on A0 to A3 with a press and a release threshold each, lighting a panel lamp and sent as four gamepad buttons through Joystick.h. The whole build, a SparkFun Pro Micro and 12 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
What you'll learn
- Reading a force-sensing resistor as a voltage divider
- Using the internal pull-up as half of the divider
- Hysteresis: two thresholds to stop flicker
- Averaging several readings to steady a noisy sensor
- A USB gamepad report from the Joystick library
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: SparkFun Pro Micro
- Parts: 14
How it works
Each of the four arrows has a force-sensing resistor between an analog pin and ground. The pin's internal pull-up makes a voltage divider, so stepping on a panel pulls the reading down. The sketch flips it with 1023 minus the reading, so more force gives a bigger number.
Once a panel's pressure reaches PRESS, it counts as stepped on. The sketch sets that gamepad button and lights its LED. The Serial Monitor prints a step line with the pressure.
To let go, the pressure has to fall RELEASE_GAP below PRESS. That gap stops a foot resting near the line from flickering the arrow on and off.
Parts list
14 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
- 1 × SparkFun Pro MicroIn MIDI Controller Kit.
- 1 × Full-size breadboard
- 4 × Force-sensing resistor
- 2 × LED, blueIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 4 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 220 Ω (1) pin 2 | SparkFun Pro Micro pin GND |
| Resistor, 220 Ω (2) pin 2 | SparkFun Pro Micro pin GND |
| Resistor, 220 Ω (3) pin 2 | SparkFun Pro Micro pin GND |
| Resistor, 220 Ω (4) pin 2 | SparkFun Pro Micro pin GND |
| LED, blue (1) pin A | SparkFun Pro Micro pin 2 |
| LED, red (1) pin A | SparkFun Pro Micro pin 3 |
| LED, red (2) pin A | SparkFun Pro Micro pin 4 |
| LED, blue (2) pin A | SparkFun Pro Micro pin 5 |
| Force-sensing resistor (1) pin 1 | SparkFun Pro Micro pin GND |
| Force-sensing resistor (2) pin 1 | SparkFun Pro Micro pin GND |
| Force-sensing resistor (3) pin 1 | SparkFun Pro Micro pin GND |
| Force-sensing resistor (4) pin 1 | SparkFun Pro Micro pin GND |
| Force-sensing resistor (1) pin 2 | SparkFun Pro Micro pin A3 |
| Force-sensing resistor (2) pin 2 | SparkFun Pro Micro pin A2 |
| Force-sensing resistor (3) pin 2 | SparkFun Pro Micro pin A1 |
| Force-sensing resistor (4) pin 2 | SparkFun Pro Micro pin A0 |
| Resistor, 220 Ω (1) pin 1 | LED, blue (1) pin C |
| Resistor, 220 Ω (2) pin 1 | LED, red (1) pin C |
| Resistor, 220 Ω (3) pin 1 | LED, red (2) pin C |
| Resistor, 220 Ω (4) pin 1 | LED, blue (2) pin C |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Dance pad on a Pro Micro: four FSR panels, a lamp for each, and a USB
// gamepad report through the Arduino Joystick library's API (Joystick.h).
//
// The panels. Each arrow has a force sensing resistor (FSR) under it: thin,
// cheap and with no moving parts, which is why so many DIY pads and the open
// firmware written for them use FSRs. Here each FSR runs from its analog pin
// to ground, with the chip's internal pull-up as the top half of the
// divider. Standing on the panel drops the FSR's resistance and pulls the pin
// down, so the sketch turns the reading over: pressure = 1023 - reading.
//
// left A3 down A2 up A1 right A0 lamps on pins 2, 3, 4 and 5
//
// The internal pull-up is somewhere between 20k and 50k and differs from chip
// to chip, so thresholds tuned on one board can be off on another. Many pad
// builds use a fixed resistor instead (a 10k from each pin to 5 V does the
// same job); set USE_INTERNAL_PULLUP to false when you add them.
//
// In Mokxi: drag a pressure pad's force up to stand on that panel. The Serial
// Monitor shows each step and the pressure that set it off.
//
// Two thresholds per panel. A panel turns on when its pressure rises past
// PRESS and off only when it falls below PRESS minus RELEASE_GAP. With one
// threshold, a foot resting near the line would flicker the arrow on and off
// many times a second (in the game, a burst of steps). The gap is the cure.
//
// Not simulated: the USB connection to a PC. Mokxi models this chip's USB as
// a serial pipe, so each report Joystick.h would send prints as a [Joystick]
// line instead. Flash it to a real Pro Micro and the pad is a gamepad with
// four buttons, which a rhythm game such as StepMania can map to the arrows.
#include <Joystick.h>
// ---------------------------------------------------------------------------
// Make it yours
// ---------------------------------------------------------------------------
const uint8_t PANEL_PINS[4] = {A3, A2, A1, A0};
const uint8_t LAMP_PINS[4] = {2, 3, 4, 5};
const char *const PANEL_NAMES[4] = {"LEFT", "DOWN", "UP", "RIGHT"};
// The pressure (0 to 1023) at which each panel counts as stepped on. Lower is
// more sensitive. Tune each panel by itself: the FSRs, the panel's weight and
// how it sits all differ, which is why pads keep one number per arrow.
int PRESS[4] = {600, 600, 600, 600};
// How far below PRESS a panel has to fall to let go.
const int RELEASE_GAP = 80;
// False when each panel has its own pull-up resistor to 5 V.
const bool USE_INTERNAL_PULLUP = true;
// Readings averaged per panel per pass, to steady a noisy sensor.
const uint8_t SAMPLES = 4;
// Lamps: true lights a panel while it is stepped on, false leaves them off.
const bool LAMPS_FOLLOW_STEPS = true;
// Print every panel's pressure twice a second, to find PRESS values.
const bool SHOW_PRESSURE = false;
// ---------------------------------------------------------------------------
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 4, 0,
false, false, false, false, false, false,
false, false, false, false, false);
bool down[4];
unsigned long lastShown = 0;
int pressure(uint8_t p) {
long sum = 0;
for (uint8_t s = 0; s < SAMPLES; s++) {
sum += analogRead(PANEL_PINS[p]);
}
return 1023 - (int)(sum / SAMPLES);
}
void setup() {
for (uint8_t p = 0; p < 4; p++) {
pinMode(PANEL_PINS[p], USE_INTERNAL_PULLUP ? INPUT_PULLUP : INPUT);
pinMode(LAMP_PINS[p], OUTPUT);
digitalWrite(LAMP_PINS[p], LOW);
}
Serial.begin(115200);
Joystick.begin(false);
Serial.println("Dance pad ready: step on a panel.");
Joystick.sendState();
}
void loop() {
bool changed = false;
int force[4];
for (uint8_t p = 0; p < 4; p++) {
force[p] = pressure(p);
bool now = down[p] ? force[p] >= PRESS[p] - RELEASE_GAP : force[p] >= PRESS[p];
if (now != down[p]) {
down[p] = now;
changed = true;
Serial.print(PANEL_NAMES[p]);
Serial.print(now ? " step (" : " lift (");
Serial.print(force[p]);
Serial.println(")");
Joystick.setButton(p, now);
digitalWrite(LAMP_PINS[p], (now && LAMPS_FOLLOW_STEPS) ? HIGH : LOW);
}
}
if (changed) {
Joystick.sendState();
}
if (SHOW_PRESSURE && millis() - lastShown >= 500) {
lastShown = millis();
for (uint8_t p = 0; p < 4; p++) {
Serial.print(PANEL_NAMES[p]);
Serial.print(' ');
Serial.print(force[p]);
Serial.print(p < 3 ? " " : "\n");
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
pressure sketch.ino
Reads one panel SAMPLES times, 4 by default, and averages the readings to cut noise. Then it returns 1023 minus that average. The flip is needed because standing on the panel lowers the pin voltage.
int pressure(uint8_t p) {
long sum = 0;
for (uint8_t s = 0; s < SAMPLES; s++) {
sum += analogRead(PANEL_PINS[p]);
}
return 1023 - (int)(sum / SAMPLES);
}setup sketch.ino
Sets each panel pin to INPUT_PULLUP when USE_INTERNAL_PULLUP is true, and each lamp pin to an output that starts off. It starts serial and the joystick, then sends an initial empty state.
void setup() {
for (uint8_t p = 0; p < 4; p++) {
pinMode(PANEL_PINS[p], USE_INTERNAL_PULLUP ? INPUT_PULLUP : INPUT);
pinMode(LAMP_PINS[p], OUTPUT);
digitalWrite(LAMP_PINS[p], LOW);
}
Serial.begin(115200);
Joystick.begin(false);
Serial.println("Dance pad ready: step on a panel.");
Joystick.sendState();
}loop sketch.ino
For each panel it measures the pressure and picks the threshold: PRESS if the panel is up, PRESS minus RELEASE_GAP if it is already down. If the state changed, it prints a line, sets the gamepad button and switches the lamp. It sends one report only when something changed.
void loop() {
bool changed = false;
int force[4];
for (uint8_t p = 0; p < 4; p++) {
force[p] = pressure(p);
bool now = down[p] ? force[p] >= PRESS[p] - RELEASE_GAP : force[p] >= PRESS[p];
if (now != down[p]) {
down[p] = now;
changed = true;
Serial.print(PANEL_NAMES[p]);
Serial.print(now ? " step (" : " lift (");
Serial.print(force[p]);
Serial.println(")");
Joystick.setButton(p, now);
digitalWrite(LAMP_PINS[p], (now && LAMPS_FOLLOW_STEPS) ? HIGH : LOW);
}
}
if (changed) {
Joystick.sendState();
}
if (SHOW_PRESSURE && millis() - lastShown >= 500) {
lastShown = millis();
for (uint8_t p = 0; p < 4; p++) {
Serial.print(PANEL_NAMES[p]);
Serial.print(' ');
Serial.print(force[p]);
Serial.print(p < 3 ? " " : "\n");
}
// ... 2 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- A panel never registers or fires too easily This project
Tune that panel's number inPRESS. Lower values are more sensitive. Each FSR and panel differs, and the internal pull-up varies from chip to chip. - Arrows trigger on the wrong LEDs or buttons This project
Check the order. The panels go left A3, down A2, up A1, right A0, and the lamps go on pins 2, 3, 4 and 5 in that order. - Readings seem backward This project
The FSR runs from the analog pin to ground. If you add your own pull-up resistors to 5 V, setUSE_INTERNAL_PULLUPto false. - The analog reading is always near 0 or noisy. Force-sensing resistor
An FSR changes resistance, so build a divider. Put about 10k from the analog pin to ground and the FSR to 5 V. Without the fixed resistor the pin floats. - The force reading is not accurate in grams. Force-sensing resistor
FSRs are not precise and vary part to part. Use them to tell light from hard presses, and calibrate against known weights if you need numbers. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.