ESP 8266, Sync commands in while loops

I’m beginning to construct a project and am slowly testing the capabilities of the Blynk library on the ESP8266. For this portion, the goal is to read virtual pin values from the app and continue a loop while the value is still 1 (I.E. true or false bool). I wrote a simple sketch to test this, but am finding that my board (ESP8266 adafruit huzzah breakout) gets caught in the while loop indefinitely, despite the loop containing a sync command. See the code below;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "sdjfdsfh";
int pin1 = 0;
int v1int = 0;
int blinkrate = 500;

void setup()
{
//  Serial.begin(9600);
  Blynk.begin(auth, "sdfsdht", "");
  pinMode(pin1,OUTPUT);
}

void loop()
{
  //digitalWrite(pin1,HIGH);
  Blynk.run();
  
  while(v1int){
  digitalWrite(pin1,LOW);
  delay(blinkrate);
  digitalWrite(pin1,HIGH);
  delay(blinkrate);
  Blynk.syncVirtual(V1);
  }
}

BLYNK_WRITE(V30){ //this just pulls down and applies strip brightness
  blinkrate = (param.asInt()+1)*10;  
  
}

BLYNK_WRITE(V1)
{
  v1int = param.asInt(); //pulls the data from the BLYNK app for virtual pin V1  
    }

For some reason this code is getting caught up in the while loop, despite the call for syncing the pin. The board becomes non-responsive to button presses in the app.

Why would you be syncing in the main loop as it is normally a setup or periodic thing not a looping event but I haven’t checked your code?
Suggest you add the debug stuff to your sketch and this will probably show you in Serial Monitor why your Huzzah is crashing.

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial

The syncing happens in a sub-loop, which is only active once the V1 button has been pressed in the app. The goal here is to have the 8266 run separate While loops for each button in the app (there may be 4 or 5 different buttons). In order to have the while loops stop running when the button is switched off, i have to have some way of changing the while condition within the loop. i.e.
`while (v1 is ON)
{Run code here
check if v1 is still on
break loop if v1 is off
}

presumably the easiest way to do that is to sync the virtual pin in the loop, but i’m open to other suggestions.`

I see you don’t have simple timer in your sketch. I would be setting a flag for on and off and then asking simple timer to read the status of the flag at intervals and then give the appropriate instruction.

I can’t seem to find any documentation in simple timer. What is its purpose? Can it be used to poll the state of all the widget buttons, and then initate/break the while loops associated? The documentation seems a little sparse.

take a look at my last post in this thread How to connect existing project with blynk app?

Ah yes ok, SimpleTimer isn’t blynk specific. So, it looks like it’s useful for setting a delay between polling for button changes, but despite this it still doesn’t quite solve the issue of breaking the while loops. Is there not a simpler way to check the status of the widget button within a While loop? I can probably make this work but it would require very different code structure. BTW thanks for the help Costas.

Simple Timer is one of the 5 libraries provided by Blynk and is well recommended. I wouldn’t recommend While loops in the main loop with Blynk. Unless you are very careful and keep and keen eye on the debug information you will be flooding the Blynk server and be disconnected. Internet control of devices is not like working local to the devices so users need to think quite differently when coding for ioT.

You are probably trying to flash a LED to test your method right? I’d still use the SimpleTimer recommend bij @Costas to turn it off/on. You can enable/disable timers too. It’s a very nice piece of code and it saves you a lot of headache. I can’t overcome the feeling you are overthinking what you want to accomplish.

I.e.

enable timer at startup to light up led every x-time
disable timer when button V1 pressed

I don’t even see why you want to use the Sync option at this points. Does it work without it?

The functionality i’m pursuing is still somewhat nebulous, but i can say that the ESP8266 will be controlling an LED strip, and each button press will control a different pattern along the strip. The patterns are somewhat complex and shift back and forth along the strip, which is why something as straightforward as (If V1 ON){LED1 ON} doesn’t seem like it’ll quite cover it. Any time a button is pressed the current pattern should immediately repopulate with the new pattern defined by the new button. I think i may have found a way to resolve this without while loops, but the structure is still in progress.

I’m very much intrigued by this, what LED strips are we talking about? It sounds like a really fun project to work on. Can you elaborate a bit more on this? :slight_smile:

Sure can. I’m still working out the details, but fundamentally it is a single string of LED’s attached to a single pin on the ESP8266. Each button on the app is tied to a certain pattern for the LED’s (along with a slider for brightness and an RGB selector for solid colors).

@tsnealon are these neopixel leds or are they the 4-wire type (3 colors + ground)?

initially it’ll be neopixels, but i don’t see any reason why this couldn’t be expanded to allow for more varieties of LED’s.