//Student Name: Kevin Wang
//Student #: 300367211
//Engineering Midterm code that does what excercises on page 2 of handout says
#include <Servo.h> //include servo.h class from which we will create a servo object
Servo myservo; //create servo object to control a servo
//respectively assign the A, B, C, D button input pins as button[i] from i=0 to i=3 for ease of use
const int button[] = {2, 12, 8, 7};
//respectively assign the AMBER BLUE RED GREEN LIGHT control pins as light[i] from i=0 to i=3 for ease of use
const int light[] = {5, 6, 9, 10};
//define a function that makes light[x] blink once given the function input "x"
void blink(int x){
digitalWrite(light[x], HIGH); //turn light x off
delay(1000); //wait a second
digitalWrite(light[x], LOW); //turn light x on
delay(1000); //wait a second
}
void setup() {
// setup code which runs once
//Assign the pins attached to each button as input pins (where the stuff in parantheses refer to Kevin's digital setup)
pinMode(2, INPUT); //BUTTON A(red): assign pin 2 (attached to red button) as an input pin
pinMode(12, INPUT); //BUTTON B(green): assign pin 12 (attached to green button) as an input pin
pinMode(8, INPUT); //BUTTON C(blue): assign pin 8 (attached to blue button) as an input pin
pinMode(7, INPUT); //BUTTON D(yellow): assign pin 7 (attached to yellow button) as an input pin
//Assign the pins attached to each light as output pins
pinMode(5, OUTPUT); //AMBER LIGHT: assign pin 5 (attached to amber light) as an output pin
pinMode(6, OUTPUT); //BLUE LIGHT: assign pin 6 (attached to green button) as an output pin
pinMode(9, OUTPUT); //RED LIGHT: assign pin 9 (attached to blue button) as an output pin
pinMode(10, OUTPUT); //GREEN LIGHT: assign pin 10 (attached to yellow button) as an output pin
myservo.attach(11); //Attach the servo on pin 11 to the servo object
}
void loop() {
// main code that runs repeatedly:
//if button A is pressed then blink green, red, blue then orange led twice. Then blink each led twice in the reverse sequence.
if (digitalRead(button[0]) == HIGH){
for (int i = 3; i >= 0; i--){ //from i=3 to i=0 in increments of 1
blink(i); //blink light i twice
blink(i);
}
for (int i = 0; i <= 3; i++){ //from i=0 to i=3 in increments of 1
blink(i); //blink light i twice
blink(i);
}
}
//if button B is pressed then blink a random led twice
if (digitalRead(button[1]) == HIGH){
int num = random(0, 4); //assign a random number from [0, 4) to "num"
blink(num); //blink the led which corresponds to the randomly generated number twice
blink(num);
delay(1000); //delay an extra second before double blinking a new random number
}
//if button C is pressed then oscillate the servo back and forth between 9 degrees and 100 degrees
if (digitalRead(button[2]) == HIGH){
int pos = 0; //variable to store the servo position
for (pos = 9; pos <= 100; pos++) { //goes from 9 degrees to 100 degrees in steps of 1 degree
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(20); //waits 20ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos--) { //goes from 100 degrees to 9 degrees
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(20); //waits 20ms for the servo to reach the position
}
}
/*if button D is pressed then move the servo to 25 degree and blink the blue led twice.
Then move the servo to 81 degrees and blink the gree led three times.
Finally move the servo to 144 degrees and alterately blink the red and orange leds twice.
*/
if (digitalRead(button[3]) == HIGH){
int pos = 0; //variable to store the servo position
for (pos = 0; pos <= 25; pos++) { //goes from 0 degrees to 25 degrees in steps of 1 degree
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(20); //waits 20ms for the servo to reach the position
}
//blink blue light twice
blink(1);
blink(1);
for (pos = 25; pos <= 81; pos++) { //goes from 25 degrees to 81 degrees in steps of 1 degree
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(20); //waits 20ms for the servo to reach the position
}
//blink green light thrice
blink(3);
blink(3);
blink(3);
for (pos = 81; pos <= 144; pos++) { //goes from 81 degrees to 144 degrees in steps of 1 degree
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(20); //waits 20ms for the servo to reach the position
}
//alternatively blink the red and orange leds twice
blink(2);
blink(0);
blink(2);
blink(0);
}
}