Docs
Slider
The Slider widget sends a number in a range to a virtual pin.
Use it for anything with a level instead of two states: LED brightness, motor speed, servo position, volume, or a setpoint for your own logic.
Drag the slider above. V3 sends the value under your finger, and that is what
your board receives.
Example
This example dims an LED from a Slider on V3 set to a range of 0 to 255.
#include <PlynxSimpleEsp32.h>
#define LED_PIN 2
#define PWM_FREQ 5000
#define PWM_BITS 8 // 8 bits gives a 0 to 255 duty range
// Runs whenever Plynx sends a value to V3.
PLYNX_WRITE(V3)
{
int value = param.asInt(); // 0 to 255, matching the widget range
ledcWrite(LED_PIN, value);
}
void setup()
{
Serial.begin(115200);
ledcAttach(LED_PIN, PWM_FREQ, PWM_BITS);
ledcWrite(LED_PIN, 0);
// Auto setup: WiFi and the Plynx token are configured from the app.
//
// To provide them directly in the sketch instead, use:
// Plynx.begin(auth, ssid, pass);
Plynx.begin();
}
void loop()
{
Plynx.run();
} ledcAttach() and ledcWrite() are the ESP32 PWM functions in Arduino core 3.x.
Older sketches use ledcSetup() and ledcAttachPin(), which were removed. If
your sketch does not compile, check which core version you are on.
Set the same range in the app that your code expects. A Slider set to 0 to
100 sends 100 at full travel, and ledcWrite() with 8 bits will look dim
because it expects 255.
Values arrive continuously
While you drag, the Slider sends values as the position changes, not just when
you let go. Your PLYNX_WRITE() may run many times per second.
Keep the handler short. If you need to drive something slow, such as a servo sweep or a display refresh, store the value and act on it from a timer:
volatile int target = 0;
PLYNX_WRITE(V3)
{
target = param.asInt();
} The app also has a Send on release option. With it on, the value is sent once when you lift your finger, which suits actuators that should not be driven continuously.
Restore the state after a reboot
After a reboot the output starts at zero, even if the Slider still shows the old position. Ask Plynx for the last value when the board reconnects:
PLYNX_CONNECTED()
{
Plynx.syncVirtual(V3);
} The stored value is sent back to the board and handled by the same
PLYNX_WRITE(V3) callback.
Troubleshooting
| Problem | Check |
|---|---|
| Slider does nothing | make sure the widget and PLYNX_WRITE() use the same virtual pin |
| Output only reaches part of its range | the widget range and the code range do not match |
ledcSetup was not declared | Arduino ESP32 core 3.x removed it; use ledcAttach() as above |
| Movement is jerky or the board disconnects | the handler is doing too much work; store the value and act from a timer |