#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 6
#define LED_COUNT 5
#define BRIGHTNESS 255 // Set BRIGHTNESS to about 1/5 (max = 255)
//Change NEO_GRBW to what ever LED Strip you are using
//
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
const int NUM_SLIDERS = 5;
enum Color{
blue,
purple,
red,
brown,
green
}iconColors[NUM_SLIDERS] = {blue, purple, red, brown, green};
const int analogInputs[NUM_SLIDERS] = {A0, A1, A2, A3, A4};
int analogSliderValues[NUM_SLIDERS];
int lastAnalogSliderValues[NUM_SLIDERS];
bool ignore[NUM_SLIDERS];
uint32_t ledValues[NUM_SLIDERS];
void setup() {
for (int i = 0; i < NUM_SLIDERS; i++) {
pinMode(analogInputs[i], INPUT);
}
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS);
Serial.begin(9600);
}
static int counter = 0;
void loop() {
updateSliderValues();
sendSliderValues(); // Actually send data (all the time)
// printSliderValues(); // For debug
sliderToLed();
// for (int i = 0; i < NUM_SLIDERS; i++) {
// if (analogSliderValues[i] == 0) {
// // Slider at 0, set LED to red
// ledValues[i] = strip.Color(255, 0, 0); // Red
// } else if (analogSliderValues[i] == 1023) {
// // Slider at max, set LED to light blue
// ledValues[i] = strip.ColorHSV(32736); // Light blue
// } else {
// // Set LED color based on rainbow effect
// // ledValues[i] = Wheel((i * 256 / strip.numPixels()) + analogSliderValues[i]);
// }
// }
for(int i=0; i < NUM_SLIDERS; i++) {
strip.setPixelColor(i, ledValues[i]);
}
// if(counter == 5){
// counter = 0;
// rainbow();
// }
// counter++;
rainbow();
strip.show();
delay(30);
}
void updateSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++) {
lastAnalogSliderValues[i] = analogSliderValues[i];
analogSliderValues[i] = analogRead(analogInputs[i]);
}
}
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < NUM_SLIDERS; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < NUM_SLIDERS - 1) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void printSliderValues() {
for (int i = 0; i < NUM_SLIDERS; i++) {
String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
Serial.write(printedString.c_str());
if (i < NUM_SLIDERS - 1) {
Serial.write(" | ");
} else {
Serial.write("\n");
}
}
}
//This is the function that controlls the "Volume to Color transition"
//It uses the HSV color space function of the Neopixel libary
//Here the H element which is Hue is set to a value proportional to the Slider position.
//By changeing the multiplier at the end (default 32) you can set which color is maximum Volume (default light Blue)
//A multiplier of 64 would cover the whole color spectrum which is not really usefull, since then 0 Volume and max volume would have the same color
void sliderToLed() {
for(int i=0; i< NUM_SLIDERS; i++) {
if(analogSliderValues[i] == 0){
ignore[i] = true;
ledValues[i] = strip.ColorHSV(0);
}
else if(analogSliderValues[i] == 1023){
ignore[i] = true;
ledValues[i] = strip.ColorHSV(1023*32);
}
else {
ignore[i] = false;
ledValues[i] = strip.ColorHSV(analogSliderValues[i] * 32);
}
}
}
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void rainbow() {
static uint16_t j = 0;
for (uint16_t i = 0; i < strip.numPixels(); i++) {
if(ignore[i] == false)
strip.setPixelColor(i, Wheel((i * 256 / strip.numPixels() + j) & 255));
}
strip.show();
j++;
}