#include <OneButton.h>
#include <RotaryEncoder.h>
#include <Adafruit_NeoPixel.h>
RotaryEncoder encoder(A3, A2, RotaryEncoder::LatchMode::TWO03);
int pin = 3;
int numPixels = 16; // Popular NeoPixel ring size
#define MAX_SLIDE_POT_ANALOG_READ_VALUE 1024
int mode;
const int modes = 4;
int analogSliderValues[modes];
bool Muted[modes] = {false, false, false, false};
const int Mute = 0;
static int pos = 250;
int newPos = 250;
static int pos1 = 250;
int newPos1 = 250;
static int pos2 = 250;
int newPos2 = 250;
static int pos3 = 250;
int newPos3 = 250;
OneButton button(9, true);
byte setPos = 0;
int pixelFormat = NEO_GRB + NEO_KHZ800;
Adafruit_NeoPixel *pixels;
void setup() {
button.attachClick(singleclick); // Link the function to be called on a single click event.
button.attachLongPressStop(longclick);
Serial.begin(9600);
// Enable Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT10) | (1 << PCINT11);
pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat);
pixels->begin();
pixels->setBrightness(30); // Set brightness to a low value.
} // setup()
ISR(PCINT1_vect) {
encoder.tick(); // Just call tick() to check the state.
} // ISR
void loop() {
button.tick();
switch (mode) {
case 1: Line1(); break;
case 2: Line2(); break;
case 3: Line3(); break;
case 4: Line4(); break;
}
switch (mode) {
case 1:
analogSliderValues[0] = !Muted[0] ? newPos : Mute;
break;
case 2:
analogSliderValues[1] = !Muted[1] ? newPos1 : Mute;
break;
case 3:
analogSliderValues[2] = !Muted[2] ? newPos2 : Mute;
break;
case 4:
analogSliderValues[3] = !Muted[3] ? newPos3 : Mute;
break;
}
pixels->show();
switch (setPos) {
case 1: updatePixels(pos, 150, 150, 150, Muted[0]); break;
case 2: updatePixels(pos1, 150, 100, 0, Muted[1]); break;
case 3: updatePixels(pos2, 0, 0, 150, Muted[2]); break;
case 4: updatePixels(pos3, 150, 0, 0, Muted[3]); break;
}
sendSliderValues();
printSliderValues();
} // loop()
void updatePixels(int position, int r, int g, int b, bool muteStatus) {
int value_pot_a = position;
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels - 1);
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(r, g, b));
}
for (int i = num_leds_switchedon; i < numPixels - 1; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 0));
}
pixels->setPixelColor(23, muteStatus ? pixels->Color(255, 0, 0) : pixels->Color(0, 0, 0));
pixels->show();
}
void Line1() {
if (setPos != 1) {
setPos = 1;
encoder.setPosition(pos);
}
newPos = encoder.getPosition();
if (pos != newPos) {
pos = newPos;
}
} // Line1()
void Line2() {
if (setPos != 2) {
setPos = 2;
encoder.setPosition(pos1);
}
newPos1 = encoder.getPosition();
if (pos1 != newPos1) {
pos1 = newPos1;
}
} // Line2()
void Line3() {
if (setPos != 3) {
setPos = 3;
encoder.setPosition(pos2);
}
newPos2 = encoder.getPosition();
if (pos2 != newPos2) {
pos2 = newPos2;
}
} // Line3()
void Line4() {
if (setPos != 4) {
setPos = 4;
encoder.setPosition(pos3);
}
newPos3 = encoder.getPosition();
if (pos3 != newPos3) {
pos3 = newPos3;
}
} // Line4()
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < mode; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < mode - 1) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void printSliderValues() {
for (int i = 0; i < modes; i++) {
String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
Serial.write(printedString.c_str());
if (i < modes - 1) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
void singleclick() {
mode++;
if (mode == 5) {
mode = 1;
}
}
void longclick() {
switch (mode) {
case 1: Muted[0] = !Muted[0]; break;
case 2: Muted[1] = !Muted[1]; break;
case 3: Muted[2] = !Muted[2]; break;
case 4: Muted[3] = !Muted[3]; break;
}
}