// Define LED setup
#define NUM_LEDS 9
#define LED_PIN 5
CRGB leds[NUM_LEDS];
// Define Servo setup
#define NUM_SERVOS 3
Servo servos[NUM_SERVOS];
int servoPins[NUM_SERVOS] = {34, 35, 32}; // Define servo control pins
// Define synchronization variables
unsigned long songStartTime;
unsigned long currentTime;
// Sample timings for LED and Servo movements
const unsigned long ledTimings[NUM_LEDS] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}; // ms
const unsigned long servoTimings[NUM_SERVOS] = {1500, 4500, 7500}; // ms
const int servoPositions[NUM_SERVOS] = {0, 90, 180}; // Degrees
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize LEDs
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
// Initialize Servos
for (int i = 0; i < NUM_SERVOS; i++) {
servos[i].attach(servoPins[i]);
}
// Start the song
songStartTime = millis();
}
void loop() {
currentTime = millis() - songStartTime;
// Update LEDs
for (int i = 0; i < NUM_LEDS; i++) {
if (currentTime >= ledTimings[i]) {
leds[i] = CHSV(random(0, 255), 255, 255); // Change to a random color
} else {
leds[i] = CRGB::Black; // Turn off LED
}
}
FastLED.show();
// Update Servos
for (int i = 0; i < NUM_SERVOS; i++) {
if (currentTime >= servoTimings[i]) {
servos[i].write(servoPositions[i]);
} else {
servos[i].write(0); // Move to initial position
}
}
// Check if the song has ended
if (currentTime > 10000) { // Assuming song duration is 10 seconds for example
songStartTime = millis(); // Restart the song
}
}