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
5 changed files with 114 additions and 14 deletions
+22 -2
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 // and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first. // 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 // Load library "ticker" for blinking status led
// *************************************************************************** // ***************************************************************************
@@ -175,6 +184,10 @@ void setup() {
// set builtin led pin as output // set builtin led pin as output
pinMode(BUILTIN_LED, 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 // start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.5, tick); ticker.attach(0.5, tick);
@@ -184,7 +197,6 @@ void setup() {
// Setup: Neopixel // Setup: Neopixel
// *************************************************************************** // ***************************************************************************
strip.init(); strip.init();
strip.setBrightness(brightness);
strip.setSpeed(convertSpeed(ws2812fx_speed)); strip.setSpeed(convertSpeed(ws2812fx_speed));
//strip.setMode(FX_MODE_RAINBOW_CYCLE); //strip.setMode(FX_MODE_RAINBOW_CYCLE);
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
@@ -605,6 +617,9 @@ void setup() {
void loop() { void loop() {
#ifdef ENABLE_BUTTON
button();
#endif
server.handleClient(); server.handleClient();
webSocket.loop(); webSocket.loop();
@@ -656,6 +671,11 @@ void loop() {
strip.setMode(FX_MODE_THEATER_CHASE); strip.setMode(FX_MODE_THEATER_CHASE);
mode = HOLD; 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) { if (mode == THEATERCHASERAINBOW) {
strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW); strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
mode = HOLD; mode = HOLD;
+2
View File
@@ -2,6 +2,8 @@
// Color modes // Color modes
// *************************************************************************** // ***************************************************************************
///////////////////////
int dipInterval = 10; int dipInterval = 10;
int darkTime = 250; int darkTime = 250;
unsigned long currentDipTime; unsigned long currentDipTime;
+11 -8
View File
@@ -1,14 +1,17 @@
// Neopixel // Neopixel
#define PIN 5 // PIN where neopixel / WS2811 strip is attached #define PIN D2 // PIN where neopixel / WS2811 strip is attached
#define NUMLEDS 24 // Number of leds in the strip #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 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_OTA // If defined, enable Arduino OTA code.
#define ENABLE_MQTT // If defined, enable MQTT client 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 // parameters for automatically cycling favorite patterns
uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds) uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
{0xff0000, 200, 1, 5.0}, // blink red for 5 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_intopic[strlen(HOSTNAME) + 4]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 5]; // Topic out will be: <HOSTNAME>/out 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_host[64] = "";
char mqtt_port[6] = ""; 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 #define DBG_OUTPUT_PORT Serial // Set debug output port
// List of all color modes // 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 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 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 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 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 struct ledstate // Data structure to store a state of a single led
{ {
+75
View File
@@ -1,6 +1,9 @@
// *************************************************************************** // ***************************************************************************
// Request handlers // Request handlers
// *************************************************************************** // ***************************************************************************
////////////////////////////
void getArgs() { void getArgs() {
if (server.arg("rgb") != "") { if (server.arg("rgb") != "") {
uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16); 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")) { if (str_mode.startsWith("=theaterchase")) {
mode = THEATERCHASE; mode = THEATERCHASE;
} }
if (str_mode.startsWith("=twinkleRandom")) {
mode = TWINKLERANDOM;
}
if (str_mode.startsWith("=theaterchaseRainbow")) { if (str_mode.startsWith("=theaterchaseRainbow")) {
mode = THEATERCHASERAINBOW; mode = THEATERCHASERAINBOW;
} }
@@ -554,3 +560,72 @@ void checkForRequests() {
} }
} }
#endif #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