Added support for input switch

Support one input switch for ON/OFF the LED strip. Also possible to program three modes (short / medium / long press)
This commit is contained in:
Szép Norbert 2018-01-18 12:21:26 +01:00 committed by GitHub
parent 970c790558
commit 84bf3fe255
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 14 deletions

View file

@ -60,7 +60,16 @@ WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800);
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
/// Button ////
#ifdef ENABLE_BUTTON
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte mediumKeyPressCountMin = 20; // 20 * 25 = 500 ms
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
boolean buttonState = false;
#endif
// ***************************************************************************
// Load library "ticker" for blinking status led
// ***************************************************************************
@ -175,6 +184,10 @@ void setup() {
// set builtin led pin as output
pinMode(BUILTIN_LED, OUTPUT);
// button pin setup
#ifdef ENABLE_BUTTON
pinMode(BUTTON,INPUT_PULLUP);
#endif
// start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.5, tick);
@ -184,7 +197,6 @@ void setup() {
// Setup: Neopixel
// ***************************************************************************
strip.init();
strip.setBrightness(brightness);
strip.setSpeed(convertSpeed(ws2812fx_speed));
//strip.setMode(FX_MODE_RAINBOW_CYCLE);
strip.setColor(main_color.red, main_color.green, main_color.blue);
@ -605,6 +617,9 @@ void setup() {
void loop() {
#ifdef ENABLE_BUTTON
button();
#endif
server.handleClient();
webSocket.loop();
@ -656,6 +671,11 @@ void loop() {
strip.setMode(FX_MODE_THEATER_CHASE);
mode = HOLD;
}
if (mode == TWINKLERANDOM) {
strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_TWINKLE_RANDOM);
mode = HOLD;
}
if (mode == THEATERCHASERAINBOW) {
strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
mode = HOLD;
@ -692,4 +712,4 @@ void loop() {
EEPROM.commit();
}
#endif
}
}

View file

@ -2,6 +2,8 @@
// Color modes
// ***************************************************************************
///////////////////////
int dipInterval = 10;
int darkTime = 250;
unsigned long currentDipTime;
@ -100,4 +102,4 @@ void tv() {

View file

@ -1,14 +1,17 @@
// Neopixel
#define PIN 5 // PIN where neopixel / WS2811 strip is attached
#define NUMLEDS 24 // Number of leds in the strip
//#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define PIN D2 // PIN where neopixel / WS2811 strip is attached
#define NUMLEDS 68 // Number of leds in the strip
#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define BUTTON D1 // Input pin for switching the LED strip on / off
const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname
const char HOSTNAME[] = "McLighting"; // Friedly hostname
#define ENABLE_OTA // If defined, enable Arduino OTA code.
#define ENABLE_MQTT // If defined, enable MQTT client code.
#define ENABLE_BUTTON // If defined, enable button handling code.
// parameters for automatically cycling favorite patterns
uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
{0xff0000, 200, 1, 5.0}, // blink red for 5 seconds
@ -26,7 +29,7 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
char mqtt_intopic[strlen(HOSTNAME) + 4]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 5]; // Topic out will be: <HOSTNAME>/out
const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID
const char mqtt_clientid[] = "McLighting"; // MQTT ClientID
char mqtt_host[64] = "";
char mqtt_port[6] = "";
@ -41,18 +44,18 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
#define DBG_OUTPUT_PORT Serial // Set debug output port
// List of all color modes
enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, THEATERCHASERAINBOW, TV, CUSTOM };
enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, TWINKLERANDOM, THEATERCHASERAINBOW, TV, CUSTOM };
MODE mode = RAINBOW; // Standard mode that is active when software starts
int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster
int brightness = 196; // Global variable for storing the brightness (255 == 100%)
int brightness = 255; // Global variable for storing the brightness (255 == 100%)
int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes
bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
bool shouldSaveConfig = false; // For WiFiManger custom config
bool shouldSaveConfig = true; // For WiFiManger custom config
struct ledstate // Data structure to store a state of a single led
{
@ -72,4 +75,4 @@ LEDState main_color = { 255, 0, 0 }; // Store the "main color" of the strip use
unsigned long time_statechange = 0; // Time when the state last changed
int timeout_statechange_save = 5000; // Timeout in ms to wait before state is saved
bool state_save_requested = false; // State has to be saved after timeout
#endif
#endif

View file

@ -1,6 +1,9 @@
// ***************************************************************************
// Request handlers
// ***************************************************************************
////////////////////////////
void getArgs() {
if (server.arg("rgb") != "") {
uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16);
@ -178,6 +181,9 @@ void handleSetNamedMode(String str_mode) {
if (str_mode.startsWith("=theaterchase")) {
mode = THEATERCHASE;
}
if (str_mode.startsWith("=twinkleRandom")) {
mode = TWINKLERANDOM;
}
if (str_mode.startsWith("=theaterchaseRainbow")) {
mode = THEATERCHASERAINBOW;
}
@ -554,3 +560,72 @@ void checkForRequests() {
}
}
#endif
/// Button management /////
#ifdef ENABLE_BUTTON
void shortKeyPress() {
if (buttonState == false) {
main_color.red = 255;
main_color.green = 255;
main_color.blue = 255;
strip.setColor(main_color.red, main_color.green, main_color.blue);
mode = HOLD;
buttonState = true;
} else {
mode = OFF;
buttonState = false;
}
}
// called when button is kept pressed for more than 2 seconds
void mediumKeyPress() {
mode = TWINKLERANDOM;
}
// called when button is kept pressed for 2 seconds or more
void longKeyPress() {
//Serial.println("hosszu lenyomas");
}
// called when key goes from not pressed to pressed
void keyPress() {
KeyPressCount = 0;
}
// called when key goes from pressed to not pressed
void keyRelease() {
if (KeyPressCount < longKeyPressCountMax && KeyPressCount >= mediumKeyPressCountMin) {
mediumKeyPress();
}
else {
if (KeyPressCount < mediumKeyPressCountMin) {
shortKeyPress();
}
}
}
void button() {
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(BUTTON);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keyPress();
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
else if (currKeyState == LOW) {
KeyPressCount++;
if (KeyPressCount >= longKeyPressCountMax) {
longKeyPress();
}
}
prevKeyState = currKeyState;
}
}
#endif

View file

@ -155,4 +155,4 @@ void handleFileList() {
output += "]";
server.send(200, "text/json", output);
}