#include <OneButton.h>
#include <RotaryEncoder.h>
#include <Adafruit_NeoPixel.h>
RotaryEncoder encoder(A3, A2, RotaryEncoder::LatchMode::TWO03);
int pin = 3;
int numPixels = 24; // Popular NeoPixel ring size
#define MAX_SLIDE_POT_ANALOG_READ_VALUE 1024
//#define BUTTON_PIN 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);
int setPos = 1;
int pixelFormat = NEO_GRB + NEO_KHZ800;
Adafruit_NeoPixel *pixels;
void setup() {
//pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
button.attachClick(singleclick); // link the function to be called on a singleclick event.
button.attachLongPressStop(longclick);
// 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();
//rainbow(5);
} // 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() {
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);
pixels->setBrightness(50);
switch(setPos){
case 1:{
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(50, 50, 50));
}
// 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[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(50, 30, 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 3:{
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, 50));
}
// 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 4:{
int value_pot_a = pos[3];
int num_leds_switchedon = map(value_pot_a, 0, MAX_SLIDE_POT_ANALOG_READ_VALUE, 0, numPixels);
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
//for(int i=0; i<numPixels; i++) { // For each pixel...
// pixels->Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
//pixels->setPixelColor(i, pixels->Color(255, 255, 0));
for (int i = 0; i < num_leds_switchedon; ++i) {
pixels->setPixelColor(i, pixels->Color(50, 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 != 1){
setPos = 1;
encoder.setPosition(pos[0]);
}
newPos[0] = encoder.getPosition();
if (pos[0] != newPos[0]) {
//printPos(newPos,0);
//newPos = map(newPos, 0, 128, 0, 1024);
pos[0] = newPos[0];
}
} //Line1 ()
/**** Print 2nd setting on Line2 ***/
void Line2() {
if (setPos != 2){
setPos = 2;
encoder.setPosition(pos[1]);
}
newPos[1] = encoder.getPosition();
if (pos[1] != newPos[1]) {
//printPos(newPos,0);
//newPos = map(newPos, 0, 128, 0, 1024);
pos[1] = newPos[1];
}
} //Line2 ()
void Line3() {
if (setPos != 3){
setPos = 3;
encoder.setPosition(pos[2]);
}
newPos[2] = encoder.getPosition();
if (pos[2] != newPos[2]) {
//printPos(newPos,0);
//newPos = map(newPos, 0, 128, 0, 1024);
pos[2] = newPos[2];
}
} //Line2 ()
void Line4() {
if (setPos != 4){
setPos = 4;
encoder.setPosition(pos[3]);
}
newPos[3] = encoder.getPosition();
if (pos[3] != newPos[3]) {
//printPos(newPos,0);
//newPos = map(newPos, 0, 128, 0, 1024);
pos[3] = newPos[3];
}
} //Line2 ()
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < modes; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < mode) {
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 < mode) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
for(int i=0; i<pixels->numPixels(); i++) {
int pixelHue = firstPixelHue + (i * 65536L / pixels->numPixels());
pixels->setPixelColor(i, pixels->gamma32(pixels->ColorHSV(pixelHue)));
}
pixels->setBrightness(10);
pixels->show();
delay(wait);
}
}
void singleclick(){
mode++;
if (mode >= modes - 1) {
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;
}
}
/////////////////////////////////////////////////////////////////////////