#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <Adafruit_NeoPixel.h>
// --- Wi-Fi Access Point Configuration ---
const char* AP_SSID = "OneServoWalker";
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
WebServer server(80);
// --- NeoPixel Configuration ---
#define LED_PIN 6
#define NUM_LEDS 16 // Kept at 16 for your transfer-fade look on the 24-LED ring
#define MAX_BRIGHTNESS 100
// --- Servo Configuration (12-bit ESP32 PWM) ---
#define SERVO_PIN 5
#define PWM_RES_BITS 12
#define CENTER_ANGLE 90
#define SWEEP_ANGLE 35
#define TURN_OFFSET 25 // Shifts center left/right to steer the walker
#define STEP_DELAY_MS 450
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Animation & gait states
uint8_t hue_offset = 0;
float breath_step = 0.0;
unsigned long last_led_update = 0;
bool step_left = true;
unsigned long last_servo_step = 0;
// Walker drive modes: "STOP", "FWD", "LEFT", "RIGHT"
String driveMode = "STOP";
// Mobile-friendly Remote Control HTML Page
const char INDEX_HTML[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>One Servo Walker Remote</title>
<style>
body { font-family: sans-serif; background: #121212; color: #fff; text-align: center; margin: 0; padding: 20px; }
h2 { margin-bottom: 5px; color: #00e5ff; }
#status { color: #aaa; margin-bottom: 25px; font-size: 1.1em; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; max-width: 340px; margin: 0 auto; }
button {
background: #1f1f1f; color: #fff; border: 2px solid #00e5ff; border-radius: 16px;
padding: 24px 10px; font-size: 1.2em; font-weight: bold; cursor: pointer;
touch-action: manipulation; user-select: none;
}
button:active { background: #00e5ff; color: #121212; }
.stop { border-color: #ff4081; color: #ff4081; }
.stop:active { background: #ff4081; color: #fff; }
.empty { visibility: hidden; }
</style>
</head>
<body>
<h2>One Servo Walker</h2>
<div id="status">Mode: STOP</div>
<div class="grid">
<div class="empty"></div>
<button onclick="sendCmd('FWD')">FWD</button>
<div class="empty"></div>
<button onclick="sendCmd('LEFT')">LEFT</button>
<button class="stop" onclick="sendCmd('STOP')">STOP</button>
<button onclick="sendCmd('RIGHT')">RIGHT</button>
</div>
<script>
function sendCmd(dir) {
document.getElementById('status').innerText = 'Mode: ' + dir;
fetch('/cmd?dir=' + dir);
}
</script>
</body>
</html>
)rawliteral";
void writeServoAngle(int angle) {
angle = constrain(angle, 0, 180);
uint32_t pulse_us = map(angle, 0, 180, 500, 2400);
uint32_t duty = (pulse_us * 4095) / 20000;
ledcWrite(SERVO_PIN, duty);
}
uint32_t wheel(byte pos) {
if (pos < 85) {
return strip.Color(255 - pos * 3, pos * 3, 0);
} else if (pos < 170) {
pos -= 85;
return strip.Color(0, 255 - pos * 3, pos * 3);
} else {
pos -= 170;
return strip.Color(pos * 3, 0, 255 - pos * 3);
}
}
void updateAmbientLEDs() {
float breath = (sin(breath_step) + 1.0) / 2.0 * 0.8 + 0.2;
uint8_t current_brightness = (uint8_t)(breath * MAX_BRIGHTNESS);
strip.setBrightness(current_brightness);
for (int i = 0; i < NUM_LEDS; i++) {
uint8_t color_index = (hue_offset + (i * 4)) & 255;
strip.setPixelColor(i, wheel(color_index));
}
strip.show();
hue_offset = (hue_offset + 1) & 255;
breath_step += 0.04;
}
void updateWalkerGait() {
if (driveMode == "STOP") {
writeServoAngle(CENTER_ANGLE);
return;
}
// Bias the center angle to steer left or right, or keep centered for forward
int activeCenter = CENTER_ANGLE;
if (driveMode == "LEFT") {
activeCenter = CENTER_ANGLE - TURN_OFFSET;
} else if (driveMode == "RIGHT") {
activeCenter = CENTER_ANGLE + TURN_OFFSET;
}
if (step_left) {
writeServoAngle(activeCenter - SWEEP_ANGLE);
} else {
writeServoAngle(activeCenter + SWEEP_ANGLE);
}
step_left = !step_left;
}
void handleRoot() {
server.send(200, "text/html", INDEX_HTML);
}
void handleCommand() {
if (server.hasArg("dir")) {
driveMode = server.arg("dir");
if (driveMode == "STOP") {
writeServoAngle(CENTER_ANGLE);
}
}
server.send(200, "text/plain", driveMode);
}
void setup() {
// Initialize NeoPixels
strip.begin();
strip.show();
// Initialize 12-bit Servo PWM
ledcAttach(SERVO_PIN, 50, PWM_RES_BITS);
writeServoAngle(CENTER_ANGLE);
// Start Open Wi-Fi Access Point
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(AP_SSID);
// Redirect all DNS requests to 192.168.4.1 to trigger the Captive Portal
dnsServer.start(DNS_PORT, "*", apIP);
// Web server routes + captive portal fallback
server.on("/", handleRoot);
server.on("/cmd", handleCommand);
server.onNotFound(handleRoot); // Catches OS captive portal checks (Apple/Android/Windows)
server.begin();
}
void loop() {
dnsServer.processNextRequest();
server.handleClient();
unsigned long now = millis();
if (now - last_led_update >= 30) {
last_led_update = now;
updateAmbientLEDs();
}
if (now - last_servo_step >= STEP_DELAY_MS) {
last_servo_step = now;
updateWalkerGait();
}
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1