// Egan Luis Tamara - 300367772 - ENGR 1190 midterm 1
#include <Servo.h> // including the servo library to enable us to use the servo
const int buttonA = 2, buttonB = 12, buttonC = 8, buttonD = 7; // assigning the push button pins to a more readable name
const int greenLed = 10, redLed = 9, blueLed = 6, amberLed = 5; // assigning the led pins to a more readable name
Servo myServo; // declaring a servo object for the code to read our physical servo
int randomNum;
void blink(int led, int count) {
// a blink function for any specified led to streamline the code
// function also features a "count" parameter to specify exactly how many times the led wants to be blinked
for (int i = 0; i < count; i++) { // a for loop that loops however many times the "count" variable specifies
digitalWrite(led, HIGH); // writes the specified led pin to be a logic HIGH, turning it on
delay(150);
digitalWrite(led, LOW); // writes the specified led pin to be a logic LOW, turning it off, creating the blink effect
delay(150);
}
}
void setup() {
// put your setup code here, to run once:
//setting all the buttons to be inputs
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
pinMode(buttonD, INPUT);
//setting all the led pins to be outputs
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(amberLed, OUTPUT);
myServo.attach(11); // assigning pin number 11 to the servo object
myServo.write(0); // initializing the servo heading to be 0 to start
randomSeed(analogRead(0)); // setting a randomSeed for the button B action using an empty analog pin as its seed
}
void loop() {
// put your main code here, to run repeatedly:
while (digitalRead(buttonA) == HIGH) { // while button A is pressed then the lights will blink continuously
// calling the blink function to blink the specified LED two times each, in sequential order,
// and then reversing the order
blink(greenLed, 2); // blink the green LED twice
blink(redLed, 2); // blink the red LED twice
blink(blueLed, 2); // blink the blue LED twice
blink(amberLed, 2); // blink the amber LED twice
blink(blueLed, 2); // blink the blue LED twice
blink(redLed, 2); // blink the red LED twice
}
while (digitalRead(buttonB) == HIGH) { // while button B is pressed then a random LED will blink twice
randomNum = random(1,5); //assign the randomNum integer to be an number between 1 and 4
if (randomNum == 1) { // if the random number is 1, then blink the green LED twice
blink(greenLed, 2);
delay(100); // putting the delay in to diffrentiate the blink iterations
}
else if (randomNum == 2) { // if the random number is 2, then blink the red LED twice
blink(redLed, 2);
delay(100);
}
else if(randomNum == 3) { // if the random number is 3, then blink the blue LED twice
blink (blueLed, 2);
delay(100);
}
else {
blink (amberLed, 2); // if the random number is not any of the above, in other words 4, then blink the amber LED twice
delay(100);
}
}
while (digitalRead(buttonC) == HIGH) {
// while button c is pressed, oscillate the servo back and forth between 9 degrees and 100 degrees
myServo.write(9); // writing the servo angle to be 9 degrees
delay(400); // setting a reasonable delay between angle writing to allow the servos to actually move
myServo.write(100); // writing the servo angle to be 100 degrees
delay(400);
}
while (digitalRead(buttonD) == HIGH) {
// while button D is pressed, excecute the following complication motion:
// rotates the servo to 25 degrees, and then blinks the blue LED twice
myServo.write(25);
delay(100);
blink(blueLed, 2);
delay(100);
// rotates the servo to 81 degrees, and then blinks the green LED twice
myServo.write(81);
delay(100);
blink(greenLed, 3);
delay(100);
// rotates the servo to 144 degrees, and then blinks the red and amber LEDS sequentially two times
myServo.write(144);
delay(100);
blink(redLed, 1);
delay(100);
blink(amberLed,1);
delay(100);
blink(redLed, 1);
delay(100);
blink(amberLed,1);
delay(100);
}
}