/*
Arduino | general-help
sb Monday, January 19, 2026 1:41 PM
Can anyone help me?
I’m trying to have my led to blink per servo.
Like i want 1 led to be on when the servo runs
and another 2 led for when the other 2 servos runs
*/
#include <Servo.h>
const int NUM_SERVOS = 3;
const int BTN_PIN = 13;
const int LED_PINS[NUM_SERVOS] = {11, 9, 7};
const int SERVO_PINS[NUM_SERVOS] = {12, 10, 8};
const unsigned long SPEED[NUM_SERVOS] = {10, 30, 90};
bool ledState[NUM_SERVOS] = {1, 1, 1};
int dir[NUM_SERVOS] = {1, 1, 1};
int pos[NUM_SERVOS] = {0, 0, 0};
int oldBtnState = 1;
unsigned long prevServoTime[NUM_SERVOS];
Servo servo[NUM_SERVOS];
void checkButton() {
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
Serial.println("Button Pressed");
} else { // was just released
Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
}
void servoSweep(int servoNumber) {
// move servo only if time is up
if (millis() - prevServoTime[servoNumber] >= SPEED[servoNumber]) {
prevServoTime[servoNumber] = millis(); // save the time the current move happened
pos[servoNumber] += dir[servoNumber]; // update next position
servo[servoNumber].write(pos[servoNumber]); // command the servo to move
// reverse direction if the servo reaches its limit
if (pos[servoNumber] == 0 || pos[servoNumber] == 180) {
dir[servoNumber] = -dir[servoNumber]; // change direction
ledState[servoNumber] = !ledState[servoNumber]; // toggle LED state
Serial.print("Servo ");
Serial.print(servoNumber);
if (pos[servoNumber] == 0) {
Serial.println(" going clock-wise.");
} else {
Serial.println(" going counter clock-wise.");
}
}
}
digitalWrite(LED_PINS[servoNumber], ledState[servoNumber]); // write LED state
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_SERVOS; i++) {
servo[i].attach(SERVO_PINS[i]);
pinMode(LED_PINS[i], OUTPUT);
}
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.println("Servo thing ready!\n");
}
void loop() {
checkButton();
servoSweep(0);
servoSweep(1);
servoSweep(2);
}