// #include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define NUM_SLIDERS 5 // Number of sliders
// #define NUM_PIXELS 12 // Number of neopixels per strip
#define NUM_BUTTONS NUM_SLIDERS // Number of buttons
#define MAX_POT_VALUE 4095 // Max value of the potentiometer
#define BAUD_RATE 9600
#define DEBUG 0 // Set to 1 to debug the inputs and outputs on the serial port
// Pins for the potentiometers, neopixels, and buttons
const int analogInputs[NUM_SLIDERS] = {A0, A1, A2, A3, A4};
// const int stripPin[NUM_SLIDERS] = {3, 4, 5, 6, 7};
const int digitalInputs[NUM_BUTTONS] = {12, 11, 10, 9, 8};
// For storing slider values and button states
int analogSliderValues[NUM_SLIDERS];
int buttonState[NUM_BUTTONS];
// For button debouncing
int oldButtonState[NUM_BUTTONS];
int mute[NUM_BUTTONS];
// Adafruit_NeoPixel strips[NUM_SLIDERS]; // Neopixel strips
// Colors for the neopixels
// uint32_t colors[NUM_SLIDERS] = {
// strips[0].Color(221, 160, 221), //purple
// strips[1].Color(255, 69, 0), //orange
// strips[2].Color(50, 205, 50), //green
// strips[3].Color(255, 255, 51), //yellow
// strips[4].Color(0, 191, 255) //blue
// };
void setup() {
for(int i = 0; i < NUM_SLIDERS; i++) {
// Initialize inputs
pinMode(analogInputs[i], INPUT); // Potentiometers
pinMode(digitalInputs[i], INPUT_PULLUP); // Buttons
// Initialize button states
oldButtonState[i] = 0;
mute[i] = 1;
// Initialize neopixels
// strips[i] = Adafruit_NeoPixel(NUM_PIXELS, stripPin[i], NEO_GRB + NEO_KHZ800);
// strips[i].setBrightness(5);
// strips[i].begin();
}
Serial.begin(BAUD_RATE);
}
void loop() {
updateSliderValues();
sendSliderValues(); // Actually send data (all the time)
buttonActions(); // Check for button presses
// neopixel();
delay(10);
}
void updateSliderValues() {
// Read the potentiometers and store the values in an array
for(int i = 0; i < NUM_SLIDERS; i++)
analogSliderValues[i] = analogRead(analogInputs[i]) * mute[i];
}
void sendSliderValues() {
// Send the slider values to the serial port
String builtString = String("");
for(int i = 0; i < NUM_SLIDERS; i++) {
builtString += String((int)analogSliderValues[i]);
if(i < NUM_SLIDERS -1)
builtString += String("|");
}
if(DEBUG)
Serial.print("Slider: ");
Serial.println(builtString);
}
void buttonActions() {
for(int i = 0; i < NUM_BUTTONS; i++) {
oldButtonState[i] = buttonState[i];
buttonState[i] = digitalRead(digitalInputs[i]);
if(buttonState[i] == LOW && oldButtonState[i] == HIGH)
mute[i] = !mute[i];
}
if(DEBUG)
sendButtonStates();
}
void sendButtonStates() {
// Send the button states to the serial port
String builtString = String("");
for(int i = 0; i < NUM_BUTTONS; i++) {
builtString += String((int)buttonState[i]);
if(i < NUM_BUTTONS -1)
builtString += String("|");
}
Serial.print("Button: ");
Serial.println(builtString);
}
// void neopixel() {
// int pixelValue[NUM_SLIDERS];
// for(int i = 0; i < NUM_SLIDERS; i++) {
// pixelValue[i] = map(analogSliderValues[i], 0, MAX_POT_VALUE, 0, NUM_PIXELS + 1);
// if(pixelValue[i] != 0) {
// strips[i].clear();
// strips[i].fill(colors[i], 0, pixelValue[i]);
// strips[i].show();
// } else {
// strips[i].clear();
// strips[i].show();
// }
// }
// if(DEBUG)
// sendNeopixelValues(pixelValue);
// }
// void sendNeopixelValues(int *pixelValue) {
// // Send the neopixel values to the serial port
// String builtString = String("");
// for(int i = 0; i < NUM_SLIDERS; i++) {
// builtString += String((int)pixelValue[i]);
// if(i < NUM_SLIDERS -1)
// builtString += String("|");
// }
// Serial.print("Neopixel: ");
// Serial.println(builtString);
// }
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1