The WebSlider example provides two independent slider controls accessible through a web browser. Designed for Arduino Uno R4 WiFi and DIYables STEM V4 IoT educational platform with enhanced analog capabilities, precision control features, and built-in educational modules for learning PWM and analog electronics. Each slider offers values from 0-255, making them perfect for PWM control, brightness adjustment, motor speed control, and any application requiring analog-like control values.
Features
Dual Sliders: Two independent slider controls (0-255 range each)
Real-time Values: Instant value updates via WebSocket communication
PWM Compatible: 8-bit values (0-255) perfect for analogWrite() functions
Visual Feedback: Real-time value display for each slider
Preset Buttons: Quick preset values for common configurations
Touch & Mouse Support: Works on desktop, tablet, and mobile devices
Responsive Design: Adapts to different screen sizes
Value Persistence: Sliders remember last position when page reloads
Platform Extensible: Currently implemented for Arduino Uno R4 WiFi, but can be extended for other hardware platforms. See DIYables_WebApps_ESP32
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. Additionally, some of these links are for products from our own brand, DIYables .
Connect the Arduino Uno R4/DIYables STEM V4 IoT board to your computer using a USB cable.
Launch the Arduino IDE on your computer.
Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
Navigate to the Libraries icon on the left bar of the Arduino IDE.
Search "DIYables WebApps", then find the DIYables WebApps library by DIYables
Click Install button to install the library.
You will be asked for installing some other library dependencies
Click Install All button to install all library dependencies.
On Arduino IDE, Go to File Examples DIYables WebApps WebSlider example, or copy the above code and paste it to the editor of Arduino IDE
/* * DIYables WebApp Library - Web Slider Example * * This example demonstrates the Web Slider feature: * - Two independent sliders (0-255) * - Real-time value monitoring * - Template for hardware control * * Hardware: Arduino Uno R4 WiFi or DIYables STEM V4 IoT * * Setup: * 1. Update WiFi credentials below * 2. Upload the sketch to your Arduino * 3. Open Serial Monitor to see the IP address * 4. Navigate to http://[IP_ADDRESS]/webslider */#include <DIYablesWebApps.h>// WiFi credentials - UPDATE THESE WITH YOUR NETWORKconstchar WIFI_SSID[] = "YOUR_WIFI_SSID";constchar WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD";// Create WebApp server and page instancesUnoR4ServerFactory serverFactory;DIYablesWebAppServerwebAppsServer(serverFactory, 80, 81);DIYablesHomePage homePage;DIYablesWebSliderPage webSliderPage;// Current slider valuesint slider1Value = 64; // Default 25%int slider2Value = 128; // Default 50%voidsetup() {Serial.begin(9600);delay(1000);// TODO: Initialize your hardware pins hereSerial.println("DIYables WebApp - Web Slider Example");// Add home and web slider pageswebAppsServer.addApp(&homePage);webAppsServer.addApp(&webSliderPage);// Optional: Add 404 page for better user experiencewebAppsServer.setNotFoundPage(DIYablesNotFoundPage());// Start the WebApp serverif (!webAppsServer.begin(WIFI_SSID, WIFI_PASSWORD)) {while (1) {Serial.println("Failed to start WebApp server!");delay(1000); } }// Set up slider callback for value changes webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) {// Store the received values slider1Value = slider1; slider2Value = slider2;// Print slider values (0-255)Serial.println("Slider 1: " + String(slider1) + ", Slider 2: " + String(slider2));// TODO: Add your control logic here based on slider values// Examples:// - Control PWM: analogWrite(LED_PIN, slider1);// - Control servos: servo.write(map(slider1, 0, 255, 0, 180));// - Control motor speed: analogWrite(MOTOR_PIN, slider2);// - Control brightness: strip.setBrightness(slider1);// - Send values via Serial, I2C, SPI, etc. });// Set up callback for config requests (when client requests current values) webSliderPage.onSliderValueToWeb([]() { webSliderPage.sendToWebSlider(slider1Value, slider2Value);Serial.println("Web client requested values - Sent: Slider1=" + String(slider1Value) + ", Slider2=" + String(slider2Value)); });}voidloop() {// Handle WebApp server communicationswebAppsServer.loop();// TODO: Add your main application code heredelay(10);}
Configure WiFi credentials in the code by updating these lines:
Click Upload button on Arduino IDE to upload code to Arduino UNO R4/DIYables STEM V4 IoT
Open the Serial Monitor
Check out the result on Serial Monitor. It looks like the below
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
DIYables WebApp - Web Slider Example
INFO: Added app /
INFO: Added app /web-slider
DIYables WebApp Library
Platform: Arduino Uno R4 WiFi
Network connected!
IP address: 192.168.0.2
HTTP server started on port 80
Configuring WebSocket server callbacks...
WebSocket server started on port 81
WebSocket URL: ws://192.168.0.2:81
WebSocket server started on port 81
==========================================
DIYables WebApp Ready!
==========================================
📱 Web Interface: http://192.168.0.2
🔗 WebSocket: ws://192.168.0.2:81
📋 Available Applications:
🏠 Home Page: http://192.168.0.2/
🎚️ Web Slider: http://192.168.0.2/web-slider
==========================================
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
If you do not see anything, reboot Arduino board.
Take note of the IP address displayed, and enter this address into the address bar of a web browser on your smartphone or PC.
Example: http://192.168.0.2
You will see the home page like below image:
Click to the Web Slider link, you will see the Web Slider app's UI like the below:
Or you can also access the page directly by IP address followed by /web-slider. For example: http://192.168.0.2/web-slider
Try moving the two sliders to control analog values (0-255) and watch the real-time feedback in the Serial Monitor.
Creative Customization - Adapt the Code to Your Project
Slider 1: First control slider with value display (0-255)
Slider 2: Second control slider with value display (0-255)
Value Display: Real-time numeric values for both sliders
Preset Buttons: Quick access to common values (0%, 25%, 50%, 75%, 100%)
Operating the Sliders
Desktop (Mouse Control)
Click and Drag: Click on slider handle and drag to adjust value
Click Position: Click anywhere on slider track to jump to that value
Fine Control: Use small mouse movements for precise adjustment
Mobile/Tablet (Touch Control)
Touch and Drag: Touch slider handle and drag to new position
Tap Position: Tap on slider track to set value
Smooth Control: Finger dragging provides smooth value changes
Value Ranges
Each slider provides:
Minimum Value: 0 (0% - completely off)
Maximum Value: 255 (100% - maximum intensity)
Resolution: 256 discrete steps (8-bit precision)
PWM Compatibility: Direct use with analogWrite() function
Programming Examples
Basic Slider Handler
voidsetup() {// Set up slider callback for value changes webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) {// Store the received values slider1Value = slider1; slider2Value = slider2;// Print slider values (0-255)Serial.println("Slider 1: " + String(slider1) + ", Slider 2: " + String(slider2));// Add your control logic here });}
LED Brightness Control
// Pin definitions for PWM LEDsconstint LED1_PIN = 9; // PWM pin for first LEDconstint LED2_PIN = 10; // PWM pin for second LEDvoidsetup() {// Configure LED pins as outputspinMode(LED1_PIN, OUTPUT);pinMode(LED2_PIN, OUTPUT);// Set initial brightnessanalogWrite(LED1_PIN, slider1Value);analogWrite(LED2_PIN, slider2Value); webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) {// Store values slider1Value = slider1; slider2Value = slider2;// Control LED brightness directly (0-255 PWM)analogWrite(LED1_PIN, slider1);analogWrite(LED2_PIN, slider2);Serial.println("LED1 Brightness: " + String(slider1) + ", LED2 Brightness: " + String(slider2)); });}
void setupThresholdControl() { webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) { slider1Value = slider1; slider2Value = slider2;// Threshold-based control for discrete outputsconstint LOW_THRESHOLD = 85; // 33%constint MEDIUM_THRESHOLD = 170; // 66%// Control digital outputs based on slider 1 thresholdsif (slider1 < LOW_THRESHOLD) {// Low level: Turn off all outputsdigitalWrite(2, LOW);digitalWrite(3, LOW);digitalWrite(4, LOW); } elseif (slider1 < MEDIUM_THRESHOLD) {// Medium level: Turn on first outputdigitalWrite(2, HIGH);digitalWrite(3, LOW);digitalWrite(4, LOW); } else {// High level: Turn on all outputsdigitalWrite(2, HIGH);digitalWrite(3, HIGH);digitalWrite(4, HIGH); }// Use slider 2 for analog PWM controlanalogWrite(9, slider2); });}
Preset Value System
// Predefined preset valuesconstint PRESETS[][2] = { {0, 0}, // Preset 0: Both off {64, 128}, // Preset 1: Low/Medium {128, 128}, // Preset 2: Both medium {255, 128}, // Preset 3: High/Medium {255, 255} // Preset 4: Both maximum};void applyPreset(int presetNumber) {if (presetNumber >= 0 && presetNumber < 5) { slider1Value = PRESETS[presetNumber][0]; slider2Value = PRESETS[presetNumber][1];// Update hardwareanalogWrite(9, slider1Value);analogWrite(10, slider2Value);// Send updated values to web interface webSliderPage.sendToWebSlider(slider1Value, slider2Value);Serial.println("Applied preset " + String(presetNumber) + ": " + String(slider1Value) + ", " + String(slider2Value)); }}void setupPresetSystem() {// You could trigger presets based on other inputs// For example, reading digital pins for preset buttons webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) { slider1Value = slider1; slider2Value = slider2;// Your normal slider handlinganalogWrite(9, slider1);analogWrite(10, slider2); });}voidloop() { server.loop();// Check for preset trigger conditions// Example: Read buttons connected to digital pinsstaticbool lastButton = false;bool currentButton = digitalRead(7); // Preset button on pin 7if (currentButton && !lastButton) { // Button pressedstaticint currentPreset = 0; applyPreset(currentPreset); currentPreset = (currentPreset + 1) % 5; // Cycle through presets } lastButton = currentButton;}
Hardware Integration Examples
LED Strip Control
// For WS2812B or similar addressable LED strips// (requires additional libraries like FastLED or Adafruit NeoPixel)constint LED_STRIP_PIN = 6;constint NUM_LEDS = 30;void setupLEDStrip() {// Initialize LED strip (depends on library used) webSliderPage.onSliderValueFromWeb([](int slider1, int slider2) { slider1Value = slider1; slider2Value = slider2;// Slider 1 controls brightness (0-255)// Slider 2 controls color temperature or hueuint8_t brightness = slider1;uint8_t hue = slider2;// Update LED strip (example with conceptual functions)// strip.setBrightness(brightness);// strip.fill(CHSV(hue, 255, 255));// strip.show();Serial.println("LED Strip - Brightness: " + String(brightness) + ", Hue: " + String(hue)); });}
Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!