#include <Servo.h>
// Define pins for servos
const int SERVO1_PIN = 44;
const int SERVO2_PIN = 45;
const int SERVO3_PIN = 46;
// Define pins for LEDs
const int LED1_PIN = 22;
const int LED2_PIN = 23;
const int LED3_PIN = 24;
// Define pins for bar graph segments (using 8 segments)
const int BARGRAPH_PINS[8] = {25, 26, 27, 28, 29, 30, 31, 32};
// Create servo objects
Servo servo1;
Servo servo2;
Servo servo3;
void setup() {
// Attach servos to their PWM pins
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo3.attach(SERVO3_PIN);
// Set LED pins as outputs
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
// Set bar graph pins as outputs
for (int i = 0; i < 8; i++) {
pinMode(BARGRAPH_PINS[i], OUTPUT);
}
// Set all servos to 0°
servo1.write(0);
servo2.write(0);
servo3.write(0);
// Turn off LEDs
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
// Turn off all bar graph segments
for (int i = 0; i < 8; i++) {
digitalWrite(BARGRAPH_PINS[i], LOW);
}
// Hold initial state for 2 seconds
delay(2000);
}
void loop() {
// STATE 1: 2 seconds
// - servo1 to 60° (servo2 and servo3 remain at 0°)
// - LED1 on (others off)
// - Bar graph segments 1 and 2 on (segments 3-8 off)
servo1.write(60);
servo2.write(0);
servo3.write(0);
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
// Bar graph: turn on segments 1 and 2, turn off segments 3-8
for (int i = 0; i < 8; i++) {
if(i < 2) {
digitalWrite(BARGRAPH_PINS[i], HIGH);
} else {
digitalWrite(BARGRAPH_PINS[i], LOW);
}
}
delay(2000);
// STATE 2: 2 seconds
// - servo2 to 120° (others 0°)
// - LED2 on (others off)
// - Bar graph segments 3,4,5 on (others off)
servo1.write(0);
servo2.write(120);
servo3.write(0);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, HIGH);
digitalWrite(LED3_PIN, LOW);
// Bar graph: segments 3-5 ON; segments 1-2 and 6-8 OFF
for (int i = 0; i < 8; i++) {
if(i >= 2 && i < 5) { // segments 3,4,5 (array index 2,3,4)
digitalWrite(BARGRAPH_PINS[i], HIGH);
} else {
digitalWrite(BARGRAPH_PINS[i], LOW);
}
}
delay(2000);
// STATE 3: 2 seconds
// - servo3 to 180° (others 0°)
// - LED3 on (others off)
// - Bar graph segments 6,7,8 on (others off)
servo1.write(0);
servo2.write(0);
servo3.write(180);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, HIGH);
// Bar graph: segments 6-8 ON; segments 1-5 OFF
for (int i = 0; i < 8; i++) {
if(i >= 5) { // segments 6,7,8 (index 5,6,7)
digitalWrite(BARGRAPH_PINS[i], HIGH);
} else {
digitalWrite(BARGRAPH_PINS[i], LOW);
}
}
delay(2000);
// RESET: 2 seconds
// - All servos to 0°
// - All LEDs and bar graph segments turn off.
servo1.write(0);
servo2.write(0);
servo3.write(0);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
for (int i = 0; i < 8; i++) {
digitalWrite(BARGRAPH_PINS[i], LOW);
}
delay(2000);
}