// LED WS2812 based on https://wokwi.com/projects/378096487919096833
// Button debounce based on library https://github.com/samopal-pro/SLib/
#include <Adafruit_NeoPixel.h>
#include "SButton.h"
#define PIN_WS2812B 26 // The ESP32 pin GPIO connected to WS2812B
#define NUM_PIXELS 12 // The number of LEDs (pixels) on WS2812B LED strip
#define PIN_BUTTON 34 // GPIO with button connected
int ledState = LOW; // Store currents stae of LED strip
int longButtonCount = 0; // Store counter for long clicks (LED mode)
// Create instance of LED object
Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
// Create instance of Button object
SButton my_button( PIN_BUTTON );
void setup() {
pinMode (PIN_WS2812B, OUTPUT); // set LED PIN for output
pinMode(PIN_BUTTON, INPUT_PULLUP); // Устанавливаем пин кнопки на вход с подтягивающим резистором
ws2812b.begin(); // initialize WS2812B strip object (REQUIRED)
my_button.SetLongClick(1000);
// my_button.SetMultiClick(500);
my_button.SetAutoClick(5000,500);
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
}
void loop()
{
ws2812b.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called
switch(my_button.Loop())
{
case SB_CLICK:
Serial.println("On Click");
switch(ledState) // check if LED is tuned on or not
{
case LOW: // if LOW turn on LED strip
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {ws2812b.setPixelColor(pixel, ws2812b.Color(0, 255, 0));}
ws2812b.show();
ledState = HIGH;
Serial.println("LED turned on");
break;
case HIGH:
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {ws2812b.setPixelColor(pixel, ws2812b.Color(0, 0, 0));}
ws2812b.show();
ledState = LOW;
Serial.println("Led turned off");
break;
}
break;
case SB_LONG_CLICK:
Serial.print("On Long Click: ");
Serial.println(longButtonCount, DEC);
switch(longButtonCount) // check number of long presses
{
case 0:
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {ws2812b.setPixelColor(pixel, ws2812b.Color(255, 0, 0));}
ws2812b.show();
ledState = HIGH; // at this entry point set LED on to be correctly switched off with short click
longButtonCount++;
break;
case 1:
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {ws2812b.setPixelColor(pixel, ws2812b.Color(0, 0, 255));}
ws2812b.show();
longButtonCount++;
break;
case 2:
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {ws2812b.setPixelColor(pixel, ws2812b.Color(255, 255, 255));}
ws2812b.show();
longButtonCount = 0;
break;
}
break;
}
}
void set_color_temp_in_kelvin(int pixel, std::string color_temp)
{
// define array for temp in Kelvin
int kelvin [60] =
{2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900,
3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900,
4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900,
5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900,
6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900,
};
// define array for RGB components
int color_rgb [60] [3] =
{
{255, 138, 18}, {255, 142, 33}, {255, 147, 44},
{255, 152, 54}, {255, 157, 63}, {255, 161, 72},
{255, 165, 79}, {255, 169, 87}, {255, 173, 94},
{255, 177, 101}, {255, 180, 107}, {255, 184, 114},
{255, 187, 120}, {255, 190, 126}, {255, 193, 132},
{255, 196, 137}, {255, 199, 143}, {255, 201, 148},
{255, 204, 153}, {255, 206, 159}, {255, 209, 163},
{255, 211, 168}, {255, 213, 173}, {255, 215, 177},
{255, 217, 182}, {255, 219, 186}, {255, 221, 190},
{255, 223, 194}, {255, 225, 198}, {255, 227, 202},
{255, 228, 206}, {255, 230, 210}, {255, 232, 213},
{255, 233, 217}, {255, 235, 220}, {255, 236, 224},
{255, 238, 227}, {255, 239, 230}, {255, 240, 233},
{255, 242, 236}, {255, 243, 239}, {255, 244, 242},
{255, 245, 245}, {255, 246, 247}, {255, 248, 251},
{255, 249, 253}, {254, 249, 255}, {252, 247, 255},
{249, 246, 255}, {247, 245, 255}, {245, 243, 255}
};
// convert input temp in Kelvin to number
int k {std::stoi(color_temp)};
// check borders and correct
if (k > 6900) {k = 6900;};
if (k < 2000) {k = 2000;};
// shift array index to beginning of array
k = (k / 100) - 20;
// get components by index k from array of RGB components
int r=color_rgb[k][0];
int g=color_rgb[k][1];
int b=color_rgb[k][0];
ws2812b.setPixelColor(pixel, ws2812b.Color(r, g, b)); // it only takes effect if pixels.show() is called
ws2812b.show(); // update to the WS2812B Led Strip
// delay(50); // 500ms pause between each pixel
//std::cout << "K:" << kelvin[k] << " comp R:" << color_rgb[k][0] << " G:" << color_rgb[k][1] << " B:" << color_rgb[k][2];
}