Anniversary Sale · up to 80% off all librariesEnds Nov 9Shop now
SOFTTDocs
Cards
You are reading the documentation for ESP-DASH v4.x.x, which is no longer the latest release. View the v5.x.x documentation.

Slider Card

Select integer or float values within a defined range from the ESP-DASH v4 dashboard with the slider card.

The slider card lets the user select a numeric value within a defined range. Use it for adjustable parameters such as brightness, speed or setpoints.

Slider card

Constructor

Pass the min and max of the range, and optionally the step size:

Card slider(&dashboard, SLIDER_CARD, "Brightness", "", 0, 255, 1);

For a fractional range, use the float overload. This overload has no default arguments: pass symbol explicitly, and pass min, max and step as float values.

Card slider(&dashboard, SLIDER_CARD, "Setpoint", "°C", 15.0f, 30.0f, 0.5f);

Prop

Type

Callback

void attachCallback(std::function<void(int value)> cb);
void attachCallbackF(std::function<void(float value)> cb);

The callback fires when the user moves the slider on the dashboard. value is the newly selected value.

  • For integer sliders, use attachCallback(). The value is passed as int.
  • For sliders created with the float constructor, use attachCallbackF(). The value is passed as float.

Call update() with the received value and then dashboard.sendUpdates() inside the callback. Otherwise the dashboard does not register the new value.

slider.attachCallback([&](int value) {
  Serial.println("Slider: " + String(value));
  slider.update(value);
  dashboard.sendUpdates();
});

For a float slider:

slider.attachCallbackF([&](float value) {
  Serial.println("Slider: " + String(value));
  slider.update(value);
  dashboard.sendUpdates();
});

Methods

update()

Sets the slider position. Call dashboard.sendUpdates() afterwards to push the change to connected clients.

slider.update(int value);
slider.update(float value);

Pass a second const char* symbol argument to change the symbol at the same time:

slider.update(244, "%");

Example

main.cpp
#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
  #include <ESPAsyncTCP.h>
#elif defined(ESP32)
  #include <WiFi.h>
  #include <AsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ESPDash.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

AsyncWebServer server(80);
ESPDash dashboard(&server);

const int LED_PIN = 2;

Card slider(&dashboard, SLIDER_CARD, "Brightness", "", 0, 255, 1);

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi connection failed");
    return;
  }
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(LED_PIN, OUTPUT);

  slider.attachCallback([&](int value) {
    analogWrite(LED_PIN, value);
    slider.update(value);
    dashboard.sendUpdates();
  });

  server.begin();
}

void loop() {
  // ...
}

Notes

  • An integer slider passes its value to attachCallback(). If you construct the slider with the float overload but register only attachCallback(), the value is truncated to int.

On this page