How to break loop in virtual pin code

i made this code for on off rgb lighting by virtual pin. once i on the V6 on blynk rgb light strip is on but i off V6 on my blynk then rgb lights don’t turned off and after some time my esp module will dissconnected from blynk.


BLYNK_WRITE(V6)  
{
  rgb:
  int pinvalue = param.asInt();
  if (pinvalue==1)
  {
    
 rgbColour[0] = 0;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  
  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;
      
    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
      
    
      
    }
    }
     int pinvalue = 0;
      if (digitalRead(pinvalue) == 0){
        goto rgb;
        
        }
  }

With just a snippet of your code it’s difficult to tell exactly what you’re doing, but it sounds like the fade operation that you’re doing is taking too long and blocking the Blynk heartbeat - leading to a disconnection.
What RGB library are you using for this?

Pete.

and if NOT? either snippet is incomplete (and in fact it lacks closing bracket :wink: ) or you forgot (?) to create that part.

And the other issue I see is the one already pointed by @PeteKnight. Let’s do some simple math:
5 miliseconds * 255 * 3 = 3.825 s. Almost 4 seconds when ESP is blocked doing same fading (right?). Try to do the same, but in non blocking manner. In BLYNK_WRITE(V6) do only some assignment, and the rest do outside. but without blocking loops. Instead using delay(5) you might want to try timer. That will simplify above tasks (as the needed loop will be “selfmade” by timer)

EDIT2:
and what is that?:

int pinvalue = 0;
      if (digitalRead(pinvalue) == 0){
        goto rgb;
        
        }

A physical button handling connected to GPIO0 inside blynk virtual pin handling routine?? No go!

1 Like

my whole code for my projct is…

    /*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Uno board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
//uncomment this line if using a Common Anode LED#define COMMON_ANODE


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "b7d6a71cdaef45b5844f5695f57badb8";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "rd";
char pass[] = "rohit2237424";
int D2, D3;
boolean buttonState = HIGH; //set the initial direction of the relays, see below for if(HIGH) logic
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);
const int redPin = 10;
const int greenPin = 11;
const int bluePin = 12;
unsigned int rgbColour[3];
void setup()
{
  // Debug console
  Serial.begin(9600);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
   pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  digitalWrite(12, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
 
    
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  // Start off with red.
  
}
BLYNK_WRITE(V1)  
{
  if (param.asInt()==1) {
     digitalWrite(7, HIGH);
digitalWrite(8, HIGH);//button presed

  } else {
      digitalWrite(7, LOW);
digitalWrite(8, LOW);//button released

  }
}
BLYNK_WRITE(V2)  
{
  if (param.asInt()==1) {
     digitalWrite(2, HIGH);
digitalWrite(6, HIGH);//button presed
digitalWrite(7, HIGH);

  } else {
      digitalWrite(2, LOW);
digitalWrite(6, LOW);//button released
digitalWrite(7, LOW);

  
  }
}
BLYNK_WRITE(V3)  
{
  if (param.asInt()==1) {
     digitalWrite(4, HIGH);
digitalWrite(5, HIGH);//button presed
 digitalWrite(6, HIGH);
 digitalWrite(7, HIGH);

  } else {
      digitalWrite(4, LOW);
digitalWrite(5, LOW);//button released
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);

  }
}
BLYNK_WRITE(V4)  
{
  if (param.asInt()==1) {
     digitalWrite(2, HIGH);
digitalWrite(3, HIGH);//button presed
  digitalWrite(4, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);

  } else {
      digitalWrite(2, LOW);
digitalWrite(3, LOW);//button released
  digitalWrite(4, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  }
}

BLYNK_WRITE(V6)  
{
  
  int pinvalue = param.asInt();
  if (pinvalue==1)
  {
    
 rgbColour[0] = 0;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  
  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;
      
    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
      
    
      
    }
  }
        
        }
  

else {
 digitalWrite(12, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}
}
 void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
 }

in this code the rgb light is on and off by my blynk app with virtual pin v6 . but i on v6 for some long time then the rgb colors are chaging once after all analog upto 255 it shows only white color it dont change it’s color.

and shown on my above snnipet of code i use goto function but by using the function i don’t able to off rgb lights .

and one more thing here i use common anode rgb led strip that’s why in else function i make all three pin high for off the led.

I realise that there’s a bit of a language barrier here, but I don’t understand exactly what you’re trying to achieve and seeing the whole code doesn’t really tell me that either.

The code snippet you posted has a very different BLYNK_WRITE(V6) function from the full code that you’ve posted above.

You’ve said twice that you want to turn the LED strip off with button V6, but judging by your code your code this isn’t just a straight on/off function but a fade up/down. There are references to cross fading between colors in your comments, but I don’t see what this has to do with turning each RGB channel on or off.

The first part of your V6 code (executed if pinvalue==1) is incrementing the brightness of each RGB channel from 0 to 255 over a period of 4 seconds (as calculated by @marvin7).
The second part of the V6 code (executed if pinvalue!=1) is writing your RG&B channels (referenced directly as pins 12, 10 & 11 rather than redPin, greenPin and bluePin) to HIGH.

Writing 255 via analogWrite to a pin and HIGH via digitalWrite will achieve the same thing, so as far as I can see the code turns the LED strip on, or on.

Pete.

  • So what are the results of this (a bit cleaner) V6 handling?
  • How the Button widget is set in app? (Switch or Push?).

I don’t get it clearly, but you should look at those lines:

for (int decColour = 0; decColour < 3; decColour += 1) {
	int incColour = decColour == 2 ? 0 : decColour + 1;

I’m not so fluent, but decColour is inremented in two locations here:
so it will be:
0, 1, 2 <-- that is the for() loop
and then we have this condition: decColour == 2 ? 0 : decColour + 1;
so, after reaching value 2 within for() loop, it will be once more increased to 3
is that intentional?

  • then we have this crossfading, but how do you choose the colours??:
    int incColour = decColour
    so “crossfade” red with red, green with green, etc ? Am I right?
  • But you try to crossfade it here:
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;

unfortunately, as the decColour= incColour, the result will be that the value of rgbColour[i] NOT changes a bit and will be still = 0 --> full brightness…

So how is it working right now?

@PeteKnight, our friend has written, that those are common anode, so it is somehow INVERSED, ie the LOW applied to a pin results in full brightenss

Actually i trying to glow my rgb lightstrip likes color are changing by cross fading and for thi i use pin 10,11,12.

Here i use common anode led strip that’s why here for the led strip turn off all three pin on the high output.

All this led strip glow like dancing area is on and off with my virtual pin v6.

Results : i on off rgb lights by virtual pin v6 but rge color changes on one time after it shows only white color.

Actually i do color changing led strip like dancing area amd it is on off by virtual pin v6

i use Blynk.syncVirtual(V6) outside the loop and its working.