I’d like to check the value of a button. As long as the button is pushed another function should run. But the following code doesn’t change my value “check” It just keeps printing me the value “1” in my Serial Monitor.
My plan is to run a RGB Strip with Blynk. With the while loop i’d like to run a light-simulations as long as my button is pressed. My plan is to write this function in a while loop that is executed as long as the button is pressed.
A while loop keeps code execution within that while statement until the while test becomes false.
This means that execution will never return to the void loop, because the change in status of the switch widget will never be seen by the code - because Blynk.run() is never executed in the while loop and this is what triggers the BLYNK_WRITE() function.
What you should be doing is using the BLYNK_WRITE() function to set a global variable as a flag.
You then use a BlynkTimer to call a function on a frequent basis, that checks this flag variable and takes the appropriate action.
I have seen this link that describes something similar. So I wrote this code but all I get is a fast blinking light. Just the first pixel of my RGB Strip…
When I press a button nothing happens.
//This is the Connection to your Blynk Setup
//See in Blynk.cloud
//It's important that these are at the top of your Code
#define BLYNK_TEMPLATE_ID "ID"
#define BLYNK_DEVICE_NAME "NAME"
#define BLYNK_AUTH_TOKEN "TOKEN"
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi data
char ssid[] = "NAME";
char pass[] = "PASSWORD";
// Start the Serial Connection
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <FastLED.h>
BlynkTimer timer;
#define PIN 14 //Led Pin
#define NUM_LEDS 16 //Number of Leds in the Strip
#define Brightness 50 //Brightness of your led Strip
CRGB leds[NUM_LEDS];
CRGBPalette16 purplePalette = CRGBPalette16 (
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::Magenta,
CRGB::Magenta,
CRGB::Linen,
CRGB::Linen,
CRGB::Magenta,
CRGB::Magenta,
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::DarkViolet,
CRGB::Linen,
CRGB::Linen
);
int value1;
BLYNK_WRITE(V0){
int value1 = param.asInt();
}
void setup()
{
FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(Brightness);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
timer.setInterval(100, Blitz);
}
void loop()
{
Blynk.run();
timer.run();
}
void Blitz(){
while(value1 == 1){
EVERY_N_MILLISECONDS(50){
leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(purplePalette, random8(), 255, LINEARBLEND);
fadeToBlackBy(leds, NUM_LEDS, 125);
FastLED.show();
}
}
FastLED.clear();
FastLED.show();
}
when I change the value of V0 (press the button) it forces to reboot my NodeMCU. So it shouts down and reboot after 3-5 seconds.
You have an idea whats wrong here?
Also when I print the value of value1 in the void loop() it just sending me 0 at least thats right