#define NUM_PIXELS 16
#define NUM_PLAYERS 4
#define NUM_COLORS 7
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip
Bounce2::Button button = Bounce2::Button();
const byte ledPin = 6;
const byte buttonPin = 3;
const int potPin[NUM_PLAYERS] = {A0, A1, A2, A3};
int colorSelected[NUM_PLAYERS] = {0};
Adafruit_NeoPixel pixels(NUM_PIXELS, ledPin, NEO_GRB + NEO_KHZ800);
uint32_t red = pixels.Color(255, 0, 0);
uint32_t green = pixels.Color(0, 255, 0);
uint32_t blue = pixels.Color(0, 0, 255);
uint32_t yellow = pixels.Color(255, 255, 0);
uint32_t white = pixels.Color(255, 255, 255);
uint32_t pink = pixels.Color(255, 0, 100);
uint32_t cyan = pixels.Color(0, 255, 255);
uint32_t colors[NUM_COLORS] = {red, green, blue, yellow, white, pink, cyan};
int potValue[NUM_PLAYERS] = {0};
int previousPotValue[NUM_PLAYERS] = {0};
bool firstTime = true;
void setup() {
Serial.begin(9600);
button.attach(buttonPin, INPUT_PULLUP);
button.interval(5);
button.setPressedState(LOW);
for (byte i = 0; i < NUM_PLAYERS; i++)
{
pinMode(potPin[i], INPUT);
}
pixels.begin();
pixels.setBrightness(255);
pixels.show();
}
void loop() {
bool changed = false;
for (byte i = 0; i < NUM_PLAYERS; i++)
{
potValue[i] = analogRead(potPin[i]);
if (potValue[i] != previousPotValue[i])
{
previousPotValue[i] = potValue[i];
changed = true;
}
colorSelected[i] = map(potValue[i], 0, 1023, 0, NUM_COLORS - 1);
}
if (changed == true)
{
int offset = 0;
Serial.println("New colors:");
for (byte j = 0; j < NUM_PLAYERS; j++)
{
for (byte i = 0; i < (NUM_PIXELS / NUM_PLAYERS); i++)
{
pixels.setPixelColor(i + offset, colors[colorSelected[j]]);
}
Serial.print("Pixel ");
Serial.print(j);
Serial.print(" = ");
Serial.println(colors[colorSelected[j]]);
offset += 4;
}
Serial.println("");
pixels.show();
changed = false;
}
button.update();
if (button.pressed()) {
if (firstTime == true)
{
firstTime = false;
Serial.println("Applying colors!");
pixels.clear();
pixels.show();
int offset = 0;
for (byte j = 0; j < NUM_PLAYERS - 1; j++)
{
for (byte i = 0; i < NUM_PLAYERS; i++)
{
pixels.setPixelColor(i + offset + 4, colors[colorSelected[i]]);
Serial.print("Pixel ");
Serial.print(i + offset + 4);
Serial.print(" = ");
Serial.println(colors[colorSelected[i]]);
}
offset += NUM_PLAYERS;
Serial.println();
}
pixels.show();
}
else
{
firstTime = true;
pixels.clear();
pixels.show();
}
}
}