Hello everybody, first i’m sorry for my bad english
I have an esp8266 and a led strip WS2812. I use the adafruit neopixel library.
I have an switch case with an push button for change colors mode of the strip (1. static color, 2. control with blynk “wifi”, 3. rainbow). I want to change modes too with blynk. i’ve created an push button in blynk with the digital pin of my hardware push button. I can change from static to “wifi” and from “wifi” to “rainbow” but after i can’t change with blynk push button but i can change it with hardware button. So i want to change modes with pushbutton or switch (from 1 to 3). can you help me ? This is my code :
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#define LED_PIN 4
#define LED_COUNT 14
#define BUTTON_LEAD 2
#define NUMBER_OF_PIXELS 56
#define NUMBER_OF_MODES 3
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
char auth[] = "token";
char ssid[] = "ssid";
char pass[] = "password";
int data=255;
int r,g,b;
bool buttonState = LOW;
bool lastButtonState = LOW;
const int PIXEL_FLASH_INTERVAL = 100;
const long interval = 15;
unsigned long previousMillis = millis();
int mode = 1;
void setup() {
Blynk.begin(auth, ssid, pass);
pinMode(BUTTON_LEAD, INPUT);
strip.begin();
}
BLYNK_WRITE(V3)
{
r = param[0].asInt();
b = param[1].asInt();
g = param[2].asInt();
static1(r, g, b,data);
}
void loop() {
Blynk.run();
switch(mode) { // Start the new animation...
case 1:
staticcolor();
break;
case 2:
static1(r, g, b, data);
break;
case 3:
rainbow(15);
break;
default:
mode = 1;
break;
}
}
BLYNK_WRITE(V2)
{
data = param.asInt();
static1(r, g, b,data);
}
void rainbow(int wait) {
if(millis() - previousMillis >= PIXEL_FLASH_INTERVAL) {
previousMillis = millis();
}
strip.setBrightness(255);
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) {
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
if(buttonListener()) { return; }
delay(wait);
}
}
void staticcolor() {
if(millis() - previousMillis >= PIXEL_FLASH_INTERVAL) {
previousMillis = millis();
}
strip.setBrightness(255);
for(int i = 0; i < LED_COUNT; i++ ) {
strip.setPixelColor(i, strip.Color(0, 255, 255));
if(buttonListener()) { return; }
}
strip.show();
}
void static1(int r, int g, int b,int brightness)
{
strip.setBrightness(brightness);
for (int i = 0; i < LED_COUNT; i++ )
{
strip.setPixelColor(i, strip.Color(r, g, b));
if(buttonListener()) { return; }
}
strip.show();
}
bool buttonListener() {
bool modeChanged = false;
buttonState = digitalRead(BUTTON_LEAD);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
mode++;
modeChanged = true;
delay(250);
}
}
lastButtonState = buttonState;
return modeChanged;
}