#include <Arduino.h>
#include "FastLED.h"
// constants for the pin numbers of the push buttons and potentiometer
const int NUM_BUTTONS = 6;
const int BUTTON_PINS[NUM_BUTTONS] = {13, 12, 8, 7, 4, 2};
const int MODE_BUTTON_PIN = A1;
const int POT_PIN = A0;
// constants for the pin numbers of the NeoPixels
const int NUM_NEOPIXELS = 3;
const int NEOPIXEL_PINS[NUM_NEOPIXELS] = {A2, A3, A4};
// constants for the pin numbers of the LED pairs
const int NUM_LED_PAIRS = 3;
const int LED_PAIR_PINS[NUM_LED_PAIRS][2] = {{3, 5}, {6, 9}, {10, 11}};
// variables to store the state of the push buttons
int buttonStates[NUM_BUTTONS];
bool modeButtonState;
bool lastModeButtonState = LOW;
// variable to store the current mode
bool mode = 0;
// variables to store the color and brightness values for the LED pairs
int LEDKelvins[NUM_LED_PAIRS] = {512, 512, 512}; // default to medium color temperature (512)
int LEDBrightness[NUM_LED_PAIRS] = {512, 512, 512}; // default to medium brightness (512)
// array to store the colors and brightness for each NeoPixel
CRGB neoPixels[NUM_NEOPIXELS] = {CRGB::Red, CRGB::Blue, CRGB::Green}; // default to red, blue, green colors
int neoPixelBrightness[NUM_NEOPIXELS] = {512, 512, 512}; // default to medium brightness (512)
// variables to store the PWM values for the two smaller LEDs in each LED pair
int LEDCold[NUM_LED_PAIRS] = {0};
int LEDHot[NUM_LED_PAIRS] = {0};
//=========================================================================================================
void mapPotToRGB(int potValue, CRGB &color)
{
// map the potentiometer value to an RGB color value
// use a color wheel to smoothly transition between colors
color = CHSV((potValue / 4) % 256, 255, 255);
}
void updateLEDs(int kelvin, int brightness, int &cold, int &hot)
{
// map the color temperature value to a PWM value for the "cold" LED
cold = map(kelvin, 0, 1024, 0, 255);
// map the color temperature value to a PWM value for the "hot" LED
hot = map(kelvin, 0, 1024, 255, 0);
// modify the PWM values based on the brightness value
cold = map(brightness, 0, 1024, 0, cold);
hot = map(brightness, 0, 1024, 0, hot);
}
//==========================================================================================================
void setup()
{
// set the pin modes for the push buttons and potentiometer
for (int i = 0; i < NUM_BUTTONS; i++)
{
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
pinMode(MODE_BUTTON_PIN, INPUT);
pinMode(POT_PIN, INPUT);
// set the pin modes for the LED pairs
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
pinMode(LED_PAIR_PINS[i][0], OUTPUT);
pinMode(LED_PAIR_PINS[i][1], OUTPUT);
}
// initialize the NeoPixels and set their pin modes to OUTPUT
FastLED.addLeds<WS2812B, 15, GRB>(&neoPixels[0], NUM_NEOPIXELS);
FastLED.addLeds<WS2812B, 16, GRB>(&neoPixels[0], NUM_NEOPIXELS);
FastLED.addLeds<WS2812B, 17, GRB>(&neoPixels[0], NUM_NEOPIXELS);
for (int i = 0; i < NUM_NEOPIXELS; i++)
{
pinMode(NEOPIXEL_PINS[i], OUTPUT);
}
// initialize the serial port for debugging
Serial.begin(9600);
}
//===================================================================================================
void loop()
{
// read the state of the push buttons and potentiometer
for (int i = 0; i < NUM_BUTTONS; i++)
{
buttonStates[i] = digitalRead(BUTTON_PINS[i]);
}
modeButtonState = digitalRead(MODE_BUTTON_PIN);
int potValue = analogRead(POT_PIN);
// process the input data and update the LED pair and NeoPixel arrays
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
if (buttonStates[i])
{
LEDKelvins[i] = potValue;
LEDBrightness[i] = 1024 - potValue;
}
}
for (int i = 0; i < NUM_NEOPIXELS; i++)
{
if (buttonStates[i + NUM_LED_PAIRS])
{
mapPotToRGB(potValue, neoPixels[i]);
neoPixelBrightness[i] = 1024 - potValue;
}
}
Serial.print("Button M state: ");
Serial.println(modeButtonState);
Serial.print("analog m read: ");
Serial.println( analogRead(A1));
// print the current state of all the push buttons
for (int i = 0; i < NUM_BUTTONS; i++)
{
Serial.print("Button ");
Serial.print(i);
Serial.print(" state: ");
Serial.println(buttonStates[i]);
}
// check the state of the mode button
if (modeButtonState != lastModeButtonState)
{
// the mode has changed
if (modeButtonState == HIGH)
{
// print the current mode
Serial.print("Current mode: ");
Serial.println(mode);
}
mode = !mode;
lastModeButtonState = modeButtonState;
}
// update the PWM values for the LED pairs based on the color temperature and brightness values
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
updateLEDs(LEDKelvins[i], LEDBrightness[i], LEDCold[i], LEDHot[i]);
}
// update the LED pairs and NeoPixels based on the mode
switch (mode)
{
case 0:
// update the LED pairs
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
analogWrite(LED_PAIR_PINS[i][0], LEDCold[i]);
analogWrite(LED_PAIR_PINS[i][1], LEDHot[i]);
}
// update the NeoPixels
for (int i = 0; i < NUM_NEOPIXELS; i++)
{
neoPixels[i] = neoPixels[i];
neoPixels[i].nscale8(neoPixelBrightness[i]);
}
FastLED.show();
break;
case 1:
// update the LED pairs
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
analogWrite(LED_PAIR_PINS[i][0], LEDBrightness[i]);
analogWrite(LED_PAIR_PINS[i][1], LEDBrightness[i]);
}
// update the NeoPixels
for (int i = 0; i < NUM_NEOPIXELS; i++)
{
neoPixels[i] = CRGB::White;
neoPixels[i].nscale8(neoPixelBrightness[i]);
}
FastLED.show();
break;
case 2:
// update the LED pairs
for (int i = 0; i < NUM_LED_PAIRS; i++)
{
analogWrite(LED_PAIR_PINS[i][0], 0);
analogWrite(LED_PAIR_PINS[i][1], 0);
}
// update the NeoPixels
for (int i = 0; i < NUM_NEOPIXELS; i++)
{
neoPixels[i] = CRGB::Black;
}
FastLED.show();
break;
}
delay(1500);
}