Slider disconnects Blynk

I have a slider on V1 that controls the overall motor speed of a quadcopter.
When moving the slider around, sometimes Blynk disconnects and it says “Connecting…” in the serial monitor.

Here is my code:

BLYNK_WRITE(V1) {
  if (isQuadTurnedOn == false) {
    Serial.println("Must turn quadcopter ON first!");
    resetAppControls();
  } else if (param.asInt() != 1 && param.asInt() != 2) { // SEPARATE IF STATEMENT USED TO AVOID SLIDER SETTING ITSELF TO 2 WHEN CONTROLLER DISCONNECTED
    currentSpeed = ((maxSpeed - minSpeed) / (maxSpeed)) * param.asInt() + minSpeed; // BETWEEN 0 AND 255
    Blynk.virtualWrite(V11, currentSpeed); // LED THAT DIMS USING THE VALUE OF currentSpeed
    if (currentSpeed > baseSpeed) {
      isLanded = false;
    } else if (currentSpeed < baseSpeed / 2) {
      isLanded = true;
    }
    if (currentSpeed > minSpeed) {
      shouldStabilize = true;
    } else {
      shouldStabilize = false;
    }
  }
}

the function “resetAppControls” which is also called in the above code looks like this:

void resetAppControls() {
  Blynk.virtualWrite(V0, isQuadTurnedOn);
  Blynk.virtualWrite(V10, isQuadTurnedOn * 255);
  Blynk.virtualWrite(V1, getEquivalentSliderVal(currentSpeed));
  Blynk.virtualWrite(V11, currentSpeed);
  Blynk.virtualWrite(V12, !isLanded * 255);
  Blynk.virtualWrite(V13, shouldStabilize * 255);
  Blynk.virtualWrite(V4, !isLanded);
}

What’s causing the disconnections?

As per the Troubleshooting section of the Documentation - http://docs.blynk.cc/#troubleshooting - you are probably flooding the connection with “Too much, too fast”.

Blynk works great for IoT control… but can be tricky to utilise in a real time, fast moving, vehicular control environment.

You might have to break some of the data bursts with short timers, even “every 10-100ms” is better than “thousands of times a second”.

http://docs.blynk.cc/#blynk-firmware-blynktimer

@andreig992 3 or 4 consecutive virtualWrite() commands is normally the maximum. Use setTimeout() function of BlynkTimer to call additional virtualWrite() commands. Test for duration but normally less than 1000ms will work.

I think I narrowed it down to the cause. All those “virtualWrite” commands don’t cause the disconnection when called alone.

here’s what explicitly causes it.

BLYNK_WRITE(V1) {
  currentSpeed = ((maxSpeed - minSpeed) / (maxSpeed)) * param.asInt() + minSpeed;
  Blynk.virtualWrite(V11, currentSpeed);
}

^^ This causes disconnections when slider is rapidly moved up and down
maxSpeed is 255. minSpeed is 20

BLYNK_WRITE(V1) {
  Blynk.virtualWrite(V11,  param.asInt());
}

^^ This doesn’t cause disconnections

Any idea why?

The “problematic” command is running calculations against the slider data, that despite being really fast, still takes time - particularly noticeable when scanning slider data in real time (as opposed to send on release), while the “working” command is just taking the raw slider data.

If all you want to do is limit the range of data from the slider, then set the range in the slider widget setting

I could probably change the way the code works, but the way I wanted to do it was to ultimately set the “minSpeed” programmatically based on the user’s preference.

Is there any way to set the range of the slider’s data once (probably in the setup function), programmatically?

You could use map() instead of relying on the slider’s range…

https://www.arduino.cc/en/Reference/Map

I think this should work?

BLYNK_WRITE(V1) {
  Blynk.virtualWrite(V11,  map(param.asInt(), 0, 1023, 20, 255));  // Take sliders full range and map down to 20-255
}

And perhaps even adjust the map values based on variables and/or widget settings??

Blynk.virtualWrite(V11,  map(param.asInt(), 0, 1023, MINval, MAXval));  // using variables for range settings
Blynk.virtualWrite(V11,  map(param.asInt(), 0, 1023, Vx, Vy);  // using virtual pins for range settings

I haven’t tried this particular syntax myself, so I don’t know if it will work as coded.