// Anselm Chow 300364684
#include <Servo.h>
Servo myservo; // Create a Servo object
int step = 1;
int angle = 0;
const int servoPin = 11; // Define the servo pin
unsigned long previousMillis2 = 0; // Variable to store the previous time
unsigned long interval2 = 8; // Interval time between each movement
int pos = 10; // Current servo position
const int ledPins[] = {5, 6, 9, 10}; // Array of LED pins
const int numLeds = sizeof(ledPins)/sizeof(ledPins[0]); // Number of LEDs in the array
unsigned long previousMillis = 0; // Variable to store the previous time
unsigned long interval3 = 100; // Interval time between each LED on/off for LED chaser
unsigned long interval4 = 150;
void setup() {
// initialize all the leds
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// initialize all the pushbuttons
pinMode(2, INPUT);
pinMode(12, INPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
// initialize the servo motor
myservo.attach(11);
// initialize serial communication at 9600 baud
Serial.begin(9600);
// generate a random number
randomSeed(analogRead(0));
}
void loop() {
// Initial LED state as off
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
unsigned long currentMillis = millis(); // Get the current milliseconds using millis
// while button a is pushed
// Sorry Nakul I don't know how to cooperate interrupt I will tried my best
while (digitalRead(2)) {
myservo.write(45);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
// While no button is pushed
while (!digitalRead(2) && !digitalRead(12) && !digitalRead(8) && !digitalRead(7)) {
// Really sorry I don't know what to do
}
// while button b is pushed
while (digitalRead(12)) {
currentMillis = millis(); // Get the current milliseconds using millis
if (currentMillis - previousMillis2 >= interval2) { // Wait 8ms for turning
previousMillis2 = currentMillis; // Update the previous time
pos = pos + 10; // Move the servo by 10 degrees
if (pos > 120) {
pos = 10; // Reset the position to 10 degrees
}
myservo.write(pos); // Turn the servo to certain position
}
}
// while button c is pushed
while (digitalRead(8)) {
currentMillis = millis(); // Get the current milliseconds using millis
if (currentMillis - previousMillis >= interval3) {
previousMillis = currentMillis; // Update the previous time
static int ledIndex = 0; // Variable to store the current LED index
digitalWrite(ledPins[ledIndex], HIGH); // Turn on the current LED
delay(50); // Wait for a short time
digitalWrite(ledPins[ledIndex], LOW); // Turn off the current LED
ledIndex = (ledIndex + 1) % numLeds; // Move to the next LED
}
}
// while button d is pushed
while (digitalRead(7)) {
int randomNumber = random(0, 3);
switch(randomNumber) {
case (1):
myservo.write(30); // Move servo to 30 degree
if (currentMillis - previousMillis >= interval4) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is on turn it off and vice-versa:
for (int i = 0; i > 2; i++) {
if (digitalRead(10)) {
digitalWrite(10, LOW);
} else {
digitalWrite(10, HIGH);;
}
}
}
break;
case(2):
myservo.write(90); // Move servo to 90 degree
if (currentMillis - previousMillis >= interval4) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is on turn it off and vice-versa:
for (int i = 0; i > 3; i++) {
if (digitalRead(9) && digitalRead(6)) {
digitalWrite(9, LOW);
digitalWrite(6, LOW);
} else {
digitalWrite(9, HIGH);
digitalWrite(6, HIGH);
}
}
}
break;
case(3):
myservo.write(150); // Move servo to 150 degree
if (currentMillis - previousMillis >= interval4) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is on turn it off and vice-versa:
for (int i = 0; i > 4; i++) {
if (digitalRead(5)) {
digitalWrite(5, LOW);
} else {
digitalWrite(5, HIGH);;
}
}
break;
}
}
}
}