아두이노 우노 R4 WiFi 블루투스 슬라이더 예제 BLE를 통한 듀얼 슬라이더 제어 튜토리얼
개요
블루투스 슬라이더 예제는 DIYables 블루투스 STEM 앱을 통해 듀얼 슬라이더 컨트롤을 제공합니다. BLE(Bluetooth Low Energy) 를 사용하는 Arduino UNO R4 WiFi 전용으로 설계되어 구성 가능한 범위와 단계로 두 개의 독립적인 값을 제어합니다. LED 밝기, 모터 속도, 서보 위치 조정 및 조절 가능한 숫자 입력이 필요한 모든 애플리케이션에 적합합니다.
참고: Arduino UNO R4 WiFi는 BLE(Bluetooth Low Energy)만 지원합니다. 클래식 블루투스는 지원하지 않습니다. DIYables 블루투스 앱은 Android에서 BLE와 클래식 블루투스를 모두 지원하고, iOS에서는 BLE를 지원합니다. 이 보드는 BLE를 사용하므로 앱은 Android와 iOS 모두에서 작동합니다.
"DIYables Bluetooth"를 검색한 다음 DIYables의 DIYables Bluetooth 라이브러리를 찾습니다.
Install 버튼을 클릭하여 라이브러리를 설치합니다.
다른 라이브러리 종속성 설치를 요청받습니다.
Install All 버튼을 클릭하여 모든 라이브러리 종속성을 설치합니다.
BLE 코드
Arduino IDE에서 File Examples DIYables Bluetooth ArduinoBLE_Slider 예제로 이동하거나, 위의 코드를 복사하여 Arduino IDE 편집기에 붙여 넣습니다.
/* * DIYables Bluetooth Library - Bluetooth Slider Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Slider feature: * - Control values using sliders (0-100) * - Support for dual sliders * - Configurable range and step * * Compatible Boards: * - Arduino UNO R4 WiFi * - Arduino Nano 33 BLE / BLE Sense * - Arduino Nano 33 IoT * - Arduino MKR WiFi 1010 * - Arduino Nano RP2040 Connect * - Any board supporting the ArduinoBLE library * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor to see connection status * 3. Use DIYables Bluetooth App to connect and control sliders * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothSlider.h>#include <platforms/DIYables_ArduinoBLE.h>// BLE Configurationconst char* DEVICE_NAME = "Arduino_Slider";const char* SERVICE_UUID = "19B10000-E8F2-537E-4F6C-D104768A1214";const char* TX_UUID = "19B10001-E8F2-537E-4F6C-D104768A1214";const char* RX_UUID = "19B10002-E8F2-537E-4F6C-D104768A1214";// Create Bluetooth instancesDIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Slider app instance (min=0, max=100, step=1)DIYables_BluetoothSlider bluetoothSlider(0, 100, 1);// Variables to store current slider valuesint currentSlider1 = 0;int currentSlider2 = 0;// PWM output pins (for LED brightness control example)constint PWM_PIN_1 = 9;constint PWM_PIN_2 = 10;voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - Slider Example");// Initialize PWM pinspinMode(PWM_PIN_1, OUTPUT);pinMode(PWM_PIN_2, OUTPUT);// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add slider app to server bluetoothServer.addApp(&bluetoothSlider);// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!");// Send initial slider positions bluetoothSlider.send(currentSlider1, currentSlider2); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Set up slider callback for value changes bluetoothSlider.onSliderValue([](int slider1, int slider2) {// Store the received values currentSlider1 = slider1; currentSlider2 = slider2;// Print slider values (0-100)Serial.print("Slider 1: ");Serial.print(slider1);Serial.print(", Slider 2: ");Serial.println(slider2);// Map slider values (0-100) to PWM range (0-255)int pwm1 = map(slider1, 0, 100, 0, 255);int pwm2 = map(slider2, 0, 100, 0, 255);// Control LED brightnessanalogWrite(PWM_PIN_1, pwm1);analogWrite(PWM_PIN_2, pwm2);// TODO: Add your control logic here based on slider values// Examples:// - Servo control: servo.write(map(slider1, 0, 100, 0, 180));// - Motor speed: analogWrite(MOTOR_PIN, pwm1);// - Volume control: setVolume(slider1);// - Brightness control: setBrightness(slider2); });// Optional: Handle requests for current slider values (when app loads) bluetoothSlider.onGetConfig([]() {// Send the stored slider values back to the app bluetoothSlider.send(currentSlider1, currentSlider2);Serial.print("App requested values - Sent: Slider1=");Serial.print(currentSlider1);Serial.print(", Slider2=");Serial.println(currentSlider2); });// You can change slider configuration at runtime:// bluetoothSlider.setRange(0, 255); // Change range to 0-255// bluetoothSlider.setStep(5); // Change step to 5 (coarser control)Serial.println("Waiting for Bluetooth connection...");}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Optional: Update slider positions based on sensor input// Example: Send current values periodically/* static unsigned long lastUpdate = 0; if (millis() - lastUpdate >= 5000) { lastUpdate = millis(); bluetoothSlider.send(currentSlider1, currentSlider2); } */delay(10);}
Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino UNO R4 WiFi에 업로드합니다.
시리얼 모니터를 엽니다.
시리얼 모니터에서 결과를 확인합니다. 아래와 같이 표시됩니다:
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 Bluetooth - Slider Example
Waiting for Bluetooth connection...
bluetoothSlider.onSliderValue([](int slider1, int slider2) { currentSlider1 = slider1; currentSlider2 = slider2;Serial.print("Slider 1: ");Serial.print(slider1);Serial.print(", Slider 2: ");Serial.println(slider2);// Map to PWM and control outputint pwm1 = map(slider1, 0, 100, 0, 255);analogWrite(PWM_PIN_1, pwm1);});
앱으로 현재 값 전송
bluetoothSlider.onGetConfig([]() { bluetoothSlider.send(currentSlider1, currentSlider2);});// Or send anytime:bluetoothSlider.send(50, 75); // Set both sliders