#include <Arduino.h>
#include <Bounce2.h>
static const uint8_t NUM_SLIDERS = 5;
static const uint8_t NUM_BUTTONS = NUM_SLIDERS;
// hardware configuration
const uint8_t analogInputs[NUM_SLIDERS] = {A0, A1, A2, A3, A4};
const uint8_t digitalInputs[NUM_BUTTONS] = {5, 6, 7, 8, 9};
// state variables
int16_t analogSliderValues[NUM_SLIDERS] = {0};
bool muteStates[NUM_BUTTONS] = {false};
// bounce objects array
Bounce debouncers[NUM_BUTTONS];
// timing
unsigned long lastSendTime = 0;
const unsigned long SEND_INTERVAL = 10; // 10ms interval for sending data
void setup() {
// configure input pins
for (uint8_t i = 0; i < NUM_SLIDERS; i++)
pinMode(analogInputs[i], INPUT);
// initialize buttons with debouncing
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
pinMode(digitalInputs[i], INPUT_PULLUP);
debouncers[i].attach(digitalInputs[i]); // attach to bounce object
debouncers[i].interval(25); // 25ms debounce interval
}
Serial.begin(115200);
}
void loop() {
const unsigned long currentTime = millis();
// update all debouncers
updateButtons();
// update and send at regular intervals
if (currentTime - lastSendTime >= SEND_INTERVAL) {
lastSendTime = currentTime;
updateSliders();
sendSliderValues();
}
}
void updateSliders() {
const uint8_t SAMPLES = 4; // number of samples for averaging
for (uint8_t i = 0; i < NUM_SLIDERS; i++) {
int32_t sum = 0;
// average multiple readings to reduce noise
for (uint8_t j = 0; j < SAMPLES; j++) {
sum += analogRead(analogInputs[i]);
delayMicroseconds(100); // brief delay between samples
}
analogSliderValues[i] = (sum / SAMPLES) * (muteStates[i] ? 0 : 1);
}
}
void sendSliderValues() {
for (uint8_t i = 0; i < NUM_SLIDERS; i++) {
Serial.print(analogSliderValues[i]);
if (i < NUM_SLIDERS - 1) Serial.print('|');
}
Serial.println();
}
void updateButtons() {
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
debouncers[i].update(); // update the debouncer
// check for falling edge (button pressed)
if (debouncers[i].fell())
muteStates[i] = !muteStates[i]; // toggle mute state
}
}