#include <Adafruit_NeoPixel.h>
#include <ESP32Servo.h>
#define SERVO1_PIN 18
#define SERVO2_PIN 19
#define JOYSTICK_X 34
#define JOYSTICK_Y 35
#define JOYSTICK_SW 32
#define NEOPIXEL_PIN 5
#define NUM_PIXELS 16 // Adjust according to your NeoPixel ring
Servo servo1, servo2;
Adafruit_NeoPixel strip(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
// Initialize Servos
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
// Initialize Joystick
pinMode(JOYSTICK_SW, INPUT_PULLUP);
// Initialize NeoPixel
strip.begin();
strip.show(); // Clear the ring
}
void loop() {
// Read Joystick Values
int xValue = analogRead(JOYSTICK_X);
int yValue = analogRead(JOYSTICK_Y);
// Map joystick values to servo angles (0 to 180 degrees)
int servo1Angle = map(xValue, 0, 4095, 0, 180);
int servo2Angle = map(yValue, 0, 4095, 0, 180);
// Move Servos
servo1.write(servo1Angle);
servo2.write(servo2Angle);
// NeoPixel Effect
rainbowCycle(10);
delay(50); // Small delay for stability
}
// Function for Rainbow Cycle Effect
void rainbowCycle(int wait) {
static int cycle = 0;
for (int i = 0; i < strip.numPixels(); i++) {
int pixelIndex = (i + cycle) & 255;
strip.setPixelColor(i, Wheel(pixelIndex));
}
strip.show();
cycle++;
delay(wait);
}
// Helper function for NeoPixel colors
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}