Shad Whiteley A20422784
#include <Servo.h>
// Button pins
#define GREY_BUTTON_PIN 3
#define BLUE_BUTTON_PIN A1
#define WHITE_BUTTON_PIN A4
#define BLACK_BUTTON_PIN A2
#define YELLOW_BUTTON_PIN A3
// LED and switch pins
#define BLUE_LED_PIN 2
#define RED_LED_PIN 10
#define SWITCH_PIN 12
// Servo pins
#define RIGHTSERVO_PIN 9
#define LEFTSERVO_PIN 6
// Create servo objects
Servo rightServo;
Servo leftServo;
// Track LED state
bool redLedState = false;
void setup() {
Serial.begin(9600); // Start Serial Monitor
// Set buttons as inputs with internal pull-ups
pinMode(GREY_BUTTON_PIN, INPUT_PULLUP);
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
pinMode(WHITE_BUTTON_PIN, INPUT_PULLUP);
pinMode(BLACK_BUTTON_PIN, INPUT_PULLUP);
pinMode(YELLOW_BUTTON_PIN, INPUT_PULLUP);
// Set LED and switch
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
// Attach servos
rightServo.attach(RIGHTSERVO_PIN);
leftServo.attach(LEFTSERVO_PIN);
// Set initial positions
rightServo.write(90);
leftServo.write(90);
}
void loop() {
// Read button states
bool blueButton = digitalRead(BLUE_BUTTON_PIN) == LOW;
bool greyButton = digitalRead(GREY_BUTTON_PIN) == LOW;
bool whiteButton = digitalRead(WHITE_BUTTON_PIN) == LOW;
bool switchState = digitalRead(SWITCH_PIN) == LOW;
// Debug prints
Serial.print("Grey: "); Serial.print(greyButton);
Serial.print(" | Blue: "); Serial.print(blueButton);
Serial.print(" | White: "); Serial.print(whiteButton);
Serial.print(" | Switch: "); Serial.println(switchState);
// Control Blue LED (stays on while button is pressed)
digitalWrite(BLUE_LED_PIN, blueButton ? HIGH : LOW);
// Toggle Red LED with switch press
static bool lastSwitchState = HIGH; // Track previous state
if (switchState && lastSwitchState == HIGH) {
redLedState = !redLedState;
digitalWrite(RED_LED_PIN, redLedState);
delay(300); // Debounce delay
}
lastSwitchState = switchState;
// Move Servos
if (greyButton) {
rightServo.write(0); // Move right servo
} else {
rightServo.write(90);
}
if (whiteButton) {
leftServo.write(180); // Move left servo
} else {
leftServo.write(90);
}
delay(10);
}