/*
* A circuit that contains 3 different LED lights that turn on and off based on the
* position of the servo handle.
*
* https://wokwi.com/projects/402606526410158081
*
* Jason Hull
* COSI 521
*/
#include <Servo.h>
const int BLUE_PIN = 11;
const int RED_PIN = 12;
const int YELLOW_PIN = 13;
Servo myservo;
void setup() {
// Set the pins for the LEDs
pinMode(BLUE_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
// Start powered down
digitalWrite(BLUE_PIN, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
// Config the servo and move to zero
myservo.attach(9);
myservo.write(0);
delay(333);
}
/* move the servo in 90 degree increments and change the LEDs along the way */
void loop() {
move(BLUE_PIN, RED_PIN, 0, 90);
move(RED_PIN, BLUE_PIN, 90, 180);
move(YELLOW_PIN, RED_PIN, 180, 90);
move(RED_PIN, YELLOW_PIN, 90, 0);
}
void move(int led_on_pin, int led_off_pin, int move_from_deg, int move_to_deg) {
// change the LEDs
digitalWrite(led_on_pin, HIGH);
digitalWrite(led_off_pin, LOW);
// move the servo - may be forward or reverse
int increment = move_from_deg < move_to_deg ? 1 : -1;
int current_pos_deg = move_from_deg;
while (current_pos_deg != move_to_deg) {
current_pos_deg += increment;
myservo.write(current_pos_deg);
delay(20);
}
}