Blynk.syncall example help

Hello, I am having trouble understanding this example (HardwareSyncStateFromApp).

  You can synchronize the state of widgets with hardware states,
  even if hardware resets or looses connection temporarily
  Project setup in the Blynk app:
    Slider widget (0...100) on V0
    Slider widget (0...100) on V2
    Button widget on digital pin (connected to an LED)
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    // Request Blynk server to re-send latest values for all pins
    Blynk.syncAll();

    // You can also update individual virtual pins like this:
    //Blynk.syncVirtual(V0, V1, V4);

    isFirstConnect = false;
  }

  // Let's write your hardware uptime to Virtual Pin 2
  int value = millis() / 1000;
  Blynk.virtualWrite(V2, value);
}

BLYNK_WRITE(V0)
{
  int value = param.asInt();
  Blynk.virtualWrite(V2, value);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);
}

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

So I understand that Blynk.syncall() is supposed to call Blynk__Write function for ALL virtual pins. However in the example only BLYNK_WRITE(V0) is defined. So my question is shouldn’t BLYNK_WRITE(V2) also be in the code? Why only BLYNK_WRITE(V0) is defined? Am I misunderstanding the meaning of this example? Can you tell me what this example does.
Thanks!

Agree. Example is bad. I made few fixes. Please check again - https://github.com/blynkkk/blynk-library/blob/master/examples/More/Sync/HardwareSyncStateFromApp/HardwareSyncStateFromApp.ino

The BLYNK_WRITE() functions only need to be included in the sketch if you need a response from them. Any virtual pins you use are automatically controlled by the Blynk system. So in this case moving the V0 slider will set the V2 slider to an equal value with the Blynk.virtualWrite(V2, value).

The initial sync will recall the last value for V2 slider, then it is updated with uptime value in seconds and then any change to V0 slider. Although you should test the sketch to see how the exact values change because the initial sync will also recall V0 value from server which in turn updates V2. So V2 might be updated several times from the initial syncAll. Might have been better just to sync V0.

Just what I was thinking :slight_smile:

1 Like

Thank you Dmitriy and @Costas, this clarifies things.