#include <OneButton.h>
#include <RotaryEncoder.h>
#include <Adafruit_NeoPixel.h>
RotaryEncoder encoder(A2, A3, RotaryEncoder::LatchMode::TWO03);
int pin = 3;
int numPixels = 16; // Popular NeoPixel ring size
#define MAX_SLIDE_POT_ANALOG_READ_VALUE 500
#define selectSW1 9
int mode = 0;
const int modes = 4;
int analogSliderValues[modes];
const int Mute = 0;
bool Muted[modes];
static int pos[modes] = {250, 250, 250, 250};
int newPos[modes] = {250, 250, 250, 250};
OneButton button(9, true);
// setPos changes between 1 and 2 depending on whether line1() or line2() is called
int setPos = 0;
int pixelFormat = NEO_GRB + NEO_KHZ800;
Adafruit_NeoPixel *pixels;
void setup() {
button.attachClick(singleclick); // link the function to be called on a singleclick event.
button.attachLongPressStop(longclick);
Serial.begin(9600);
// You may have to modify the next 2 lines if using other pins than A2 and A3
PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCMSK1 |= (1 << PCINT10) | (1 << PCINT11);
pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat);
pixels->begin(); // This enables the interrupt for pin 2 and 3 of Port C.
} // setup()
ISR(PCINT1_vect) {
encoder.tick(); // just call tick() to check the state.
} // ISR
// Read the current position of the encoder and print out when changed.
void loop() {
for (int i = 0; i < modes - 1; i++) {
if (Muted[i]){
analogSliderValues[i] = Mute;
} else {
analogSliderValues[i] = pos[i];
}
}
button.tick();
if (mode == 0) {
Line1();
}
if (mode == 1) {
Line2();
}
if (mode == 2) {
Line3();
}
if (mode == 3) {
Line4();
}
String p1=":";
//Serial.println(newPos + p1 + newPos1 + p1 + newPos2 + p1 + newPos3 + p1 + mode);
switch(setPos){
case 0:{
int value_pot_a = pos[0];
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels);
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(150, 150, 150));
}
// LEDs are switched off which correspond to the area right of the slide knob
for (int i = num_leds_switchedon; i < numPixels; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 0));
}
pixels->show();
break;
}
case 1:{
int value_pot_a = pos[1];
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels);
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(150, 150, 0));
}
// LEDs are switched off which correspond to the area right of the slide knob
for (int i = num_leds_switchedon; i < numPixels; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 0));
}
pixels->show();
break;
}
case 2:{
int value_pot_a = pos[2];
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels);
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 150));
}
// LEDs are switched off which correspond to the area right of the slide knob
for (int i = num_leds_switchedon; i < numPixels; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 0));
}
pixels->show();
break;
}
case 3:{
int value_pot_a = pos[3];
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels);
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(150, 0, 0));
}
// LEDs are switched off which correspond to the area right of the slide knob
for (int i = num_leds_switchedon; i < numPixels; ++i) {
pixels->setPixelColor(i, pixels->Color(0, 0, 0));
}
pixels->show();
break;
}
}
//pixels->show();
sendSliderValues();
printSliderValues();
} // loop ()
/**** Print 1st setting on Line1 ***/
void Line1() {
if (setPos != 0) {
setPos = 0;
encoder.setPosition(pos[0]);
}
newPos[0] = encoder.getPosition();
if (pos[0] != newPos[0]) {
pos[0] = newPos[0];
}
} // Line1()
/**** Print 2nd setting on Line2 ***/
void Line2() {
if (setPos != 1) {
setPos = 1;
encoder.setPosition(pos[1]);
}
newPos[1] = encoder.getPosition();
if (pos[1] != newPos[1]) {
pos[1] = newPos[1];
}
} // Line2()
void Line3() {
if (setPos != 2) {
setPos = 2;
encoder.setPosition(pos[2]);
}
newPos[2] = encoder.getPosition();
if (pos[2] != newPos[2]) {
pos[2] = newPos[2];
}
} // Line3()
void Line4() {
if (setPos != 3) {
setPos = 3;
encoder.setPosition(pos[3]);
}
newPos[3] = encoder.getPosition();
if (pos[3] != newPos[3]) {
pos[3] = newPos[3];
}
} // Line4()
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i <= modes - 1; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < mode) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void printSliderValues() {
for (int i = 0; i <= modes - 1; i++) {
String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
Serial.write(printedString.c_str());
if (i < mode) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
void singleclick(){
mode++;
if (mode > modes) {
mode = 0;
} // what happens when the button is clicked // turn off the gren led
}
void longclick(){
// what happens when buton is long-pressed
switch(mode){
case 0:{
Muted[0] = !Muted[0];
break;
}
case 1:{
Muted[1] = !Muted[1];
break;
}
case 2:{
Muted[2] = !Muted[2];
break;
}
case 3:{}
Muted[3] = !Muted[3];
break;
}
}