Checking an Button Value in a while loop

Hey,

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.

Does somebody have the same issue here?

I am using the Blynk Libary 1.0.1 with an NodeMCU

BLYNK_WRITE(V4){
  int check = param.asInt();
  while(check =! 0){
    Serial.println(check);
    check = param.asInt();
  } 
}

Greets J

It should be like this

BLYNK_WRITE(V4){
  int check = param.asInt();
  Serial.printIn(check);
}

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.

J

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.

Pete.

2 Likes

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();
} 

J

You’re still using a while loop rather an an if test.

Pete.

I did it like here:

#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[] = "SSID";
char pass[] = "!PASSWORD";

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

int value1;

void setup()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(100, Blink);
}


void loop()
{
  Blynk.run();
  Blitz();
  timer.run();
  Serial.println(value1);
}

BLYNK_WRITE(V0){
  value1 = param.asInt();
}

void Blink(){
  if(value1 == 1){
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  }else{
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

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 :grinning:

J

First of all you must read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Yes you are right. Actually I don’t know why there ist Blitz(); in it. Shouldn’t be there.

The ideal blynk void loop should look like this

void loop()
{
  Blynk.run();
  timer.run();
}

These shouldn’t be there either…

Pete.

After deleting this everything works like a charm! Thank you @PeteKnight and @John93 !!

2 Likes