#include <MIDI.h>
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>
// MIDI setup
MIDI_CREATE_DEFAULT_INSTANCE();
// Neopixel setup
#define NUMPIXELS 8
#define PIN 6
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// Potentiometer and button setup
const int potPins[] = {34, 35, 32, 33, 36, 39, 25, 26}; // Analog pins for pots
const int buttonPins[] = {2, 4, 5, 18, 19, 21, 22, 23}; // Digital pins for buttons
Bounce debouncedButtons[8];
int potValues[8];
int lastPotValues[8];
void setup() {
Serial.begin(115200);
MIDI.begin(MIDI_CHANNEL_OMNI);
pixels.begin();
pixels.show(); // Initialize all pixels to 'off'
// Initialize potentiometer values
for (int i = 0; i < 8; i++) {
pinMode(potPins[i], INPUT);
potValues[i] = analogRead(potPins[i]);
lastPotValues[i] = potValues[i];
}
// Initialize buttons with debouncing
for (int i = 0; i < 8; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
debouncedButtons[i].attach(buttonPins[i]);
debouncedButtons[i].interval(5); // Debounce time in milliseconds
}
}
void loop() {
// Handle potentiometers
for (int i = 0; i < 8; i++) {
potValues[i] = analogRead(potPins[i]);
if (abs(potValues[i] - lastPotValues[i]) > 4) { // Only send if there's a significant change
MIDI.sendControlChange(i, map(potValues[i], 0, 4095, 0, 127), 1);
lastPotValues[i] = potValues[i];
}
}
// Handle buttons
for (int i = 0; i < 8; i++) {
debouncedButtons[i].update();
if (debouncedButtons[i].fell()) {
MIDI.sendNoteOn(60 + i, 127, 1); // Send Note On
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // Light up LED
pixels.show();
}
if (debouncedButtons[i].rose()) {
MIDI.sendNoteOff(60 + i, 0, 1); // Send Note Off
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Turn off LED
pixels.show();
}
}
}