Exiting a “WHILE” loop from a switch widget

Hello Forum,

I am trying make a fading effect on my RGB LED strip by using a switch widget.
I want that when my switch is set on “on” my fading loop start and whent it’s on “off” it stop.
Im doing my fading loop with a while function.
When my switch is on my loop start but when it’s off it won’t go off.

Can you please help me fix this ?

BLYNK_WRITE(V3){
 int pinvalue = param.asInt();
 while (pinvalue == 1){

 #define delayTime 30

for(int i = 0; i < 255; i += 1)
{
  redValue -= 1;
  greenValue+= 1;
  analogWrite(RedPin, redValue);
  analogWrite(GreenPin, greenValue);
  delay(delayTime);
}

redValue = 0;
greenValue= 255;
blueValue=0;

for(int i = 0; i < 255; i += 1)
{
  greenValue -= 1;
  blueValue += 1;
  analogWrite(GreenPin, greenValue);
  analogWrite(BluePin, blueValue);
  delay(delayTime);
}
 redValue = 0;
greenValue= 0;
blueValue=255;

for(int i = 0; i < 255; i += 1)
{
  blueValue -= 1;
  redValue += 1;
  analogWrite(BluePin, blueValue);
  analogWrite(RedPin, redValue);
  delay(delayTime);
}
}
}

Using while loops with Blynk can be very problematic, and my general advice is that they should be avoided.

The BLYNK_WRITE(V3) callback is triggered when the code encounters a Blynk.run() command. This command effectively asks the Blynk server if any widget values have changed, and if they have then it calls the appropriate BLYNK_WRITE() callback.

In your case, the Blynk.run in the void loop is executed, it sees that the widget attached to V3 has changed from 0 to 1, and the BLYNK_WRITE(V3) callback is triggered. Because this has a while loop in it that traps the code execution within that callback function, the Blynk.run() in the void loop is never executed again, so the value of pinvalue never changes.

Even if you put a Blynk.run() command inside the while loop, the value of pinvalue would only change when the BLYNK_WRITE(V3) callback function is executed again, and that can’t happen until the while loop is broken - which can never happen.

Steer clear of while loops, and if you need to use any type of loop don’t do it inside a BLYNK_WRITE() callback function.

Pete.

Thanks for your answer.
But what do I do now ?

Impossible to say based just on the code snippet you’ve provided and with none of the information that is requested when you create a new “ Ed help…” topic.

Pete.

I’m using an esp8266
And this is my full code

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLWJgjQOLR"
#define BLYNK_DEVICE_NAME "Neon Planeur"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"

int BluePin = 12;
int RedPin = 13;
int GreenPin = 15;

int blueValue;
int redValue;
int greenValue;

BLYNK_WRITE(V0){
  int pinvalue = param.asInt();
  analogWrite(BluePin,pinvalue);
}
BLYNK_WRITE(V1){
  int pinvalue = param.asInt();
  analogWrite(RedPin,pinvalue);
}
BLYNK_WRITE(V2){
  int pinvalue = param.asInt();
  analogWrite(GreenPin,pinvalue); 
}
BLYNK_WRITE(V3){
 int pinvalue = param.asInt();

 #define delayTime 30

for(int i = 0; i < 255; i += 1)
{
  redValue -= 1;
  greenValue+= 1;
  analogWrite(RedPin, redValue);
  analogWrite(GreenPin, greenValue);
  delay(delayTime);
}

redValue = 0;
greenValue= 255;
blueValue=0;

for(int i = 0; i < 255; i += 1)
{
  greenValue -= 1;
  blueValue += 1;
  analogWrite(GreenPin, greenValue);
  analogWrite(BluePin, blueValue);
  delay(delayTime);
}
 redValue = 0;
greenValue= 0;
blueValue=255;

for(int i = 0; i < 255; i += 1)
{
  blueValue -= 1;
  redValue += 1;
  analogWrite(BluePin, blueValue);
  analogWrite(RedPin, redValue);
  delay(delayTime);
}
pinvalue = 0;
}


void setup()
{
  pinMode(BluePin,OUTPUT);
  pinMode(RedPin,OUTPUT);
  pinMode(GreenPin,OUTPUT);
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
}
``

What widget types do you have attached to the various virtual pins, and what function do they perform? (always a good idea to document this in your sketch).

How are the datastreams that these widgets are attached to configured? (Data types, min/max values etc)?

Is there any reason why you aren’t updating your global variables such as blueValue when the RGB value for that colour is changed via a Widget?

Pete.

I use sliders to control the brightness off every color on my led strip so the min value is 0 and the max is 255.
The blueValue variable is only used for the color fading program.

The thing I want to do is to do an Infinite loop when button is ON and break it when button is OFF.

That’s not really a very complete answer.

I could spend time explaining how I would approach the issue, but the problem is that I see some flaws in the existing process that I think need to be addressed before designing a solution - otherwise the solution may need to change once you recognise the flaws and want to fix them.

If you don’t want to spend time answering the questions in detail and articulating your overall requirements fully then don’t expect people to want to donate their time to help you.

The short answer from me is therefore that you should probably to use a BlynkTimer or a library such as FastLED.

Pete.

Sorry it’s just that I’m a beginer and I don’t understand what you want me to tell you .