#include <Servo.h>
/*Geonardo Ng, Student #300367273
Code for MidTerm 1
Component list is as follows:
Pushbutton circuits that generate a logic high are connected to digital pins:
2 - Button A
12 - Button B
8 - Button C
7 - Button D
LEDS cicuits are on the following digital pins:
5 - Amber LED
6 - Blue LED
10 - Green LED
9 - Red LED
Servo control line is on digital pin 11
*/
//introducing function prototype for top to down design
void lmao();
void lmfao();
void lmbao();
void lmmfao();
Servo Servo1;//setting up the object for servo as Servo1
int const button_a = 2, button_b = 12, button_c = 8, button_d = 7;//using int value to determine the pins
int const amber_led = 5, blue_led = 6, green_led = 10, red_led = 9;
int angle = 45;//initiating the first angle for servo
int const delayTime = 250;//setting variable for easier change in delay time
void setup() {
//Setting default pin value to be high (led off)
digitalWrite(amber_led, HIGH);
digitalWrite(blue_led, HIGH);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);
//Setting up the pin for usage input and output (buttons and led respectively)
pinMode(amber_led, OUTPUT);
pinMode(blue_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(button_a, INPUT);
pinMode(button_b, INPUT);
pinMode(button_c, INPUT);
pinMode(button_d, INPUT);
Servo1.attach(11);//Setting up connection of servo at pin 11
}
void loop(){
// put your main code here, to run repeatedly:
if(digitalRead(button_a) == HIGH)//if statement for when button a is pressed
lmao();//calling lmao function
if(digitalRead(button_b) == HIGH)//if statemtnt for when button b is pressed
lmfao();//calling lmfao function
if(digitalRead(button_c) == HIGH)//if statemtnt for when button b is pressed
lmbao();//calling lmbao function
if(digitalRead(button_d) == HIGH)//if statement for when button c is pressed
lmmfao();//calling lmmfao function
}
void randomblink(){//making function for 1 random led will blink twice
int r=millis()%4;//getting the random int to set which led will blink
int color;
if(r==0)//if statements that determine which led blinks
color=amber_led;
if(r==1)
color=blue_led;
if(r==2)
color=green_led;
if(r==3)
color=red_led;
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
delay(delayTime);
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
delay(delayTime);
}
void blinkTwice(int x){//function to blink led twice
digitalWrite(x, LOW);
delay(delayTime);
digitalWrite(x, HIGH);
delay(delayTime);
digitalWrite(x, LOW);
delay(delayTime);
digitalWrite(x, HIGH);
delay(delayTime);
}
void lmao(){//making the function to blink twice and go in reverse from amber, blue, green, red
blinkTwice(amber_led);//blink the selected color twice
blinkTwice(blue_led);
blinkTwice(green_led);
blinkTwice(red_led);
delay(delayTime);
blinkTwice(red_led);
blinkTwice(green_led);
blinkTwice(blue_led);
blinkTwice(amber_led);
}
void lmfao(){//making function for 1 random led will blink twice
int r = millis()%4;//getting the random int to set which led will blink
int color;
if(r==0)//if statements that will tell which led blinks
color=amber_led;
if(r==1)
color=blue_led;
if(r==2)
color=green_led;
if(r==3)
color=red_led;
blinkTwice(color);//blink the selected color twice
}
void lmbao(){//function that will oscillate servo between 9 and 100 degree(s)
int angle = 9;//set the angle to be used
Servo1.write(angle);//changing the angle of servo
delay(5 * delayTime);//delay to give time for the servo to move
angle = 100;//set the angle to be used
Servo1.write(angle);//changing the angle of servo
}
void lmmfao(){//furnction that move servo to 25 degree, 81,blink grren led 3x, move servo to 144 degree, blink red and orange alternately
int color = blue_led;//initialize the color
Servo1.write(25);//changing the angle of servo
delay(5 * delayTime);
blinkTwice(color);//blink the selected color twice
Servo1.write(81);
delay(delayTime);
color = green_led;//change the color and blink 3 times
delay(delayTime);
blinkTwice(color);//blink the selected color twice
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
Servo1.write(144);
delay(5 * delayTime);
color = red_led;//change the color and blink once
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
delay(delayTime);
color = amber_led;//change the color and blink once
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
color = red_led;//change the color and blink once
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
delay(delayTime);
color = amber_led;//change the color and blink once
digitalWrite(color, LOW);
delay(delayTime);
digitalWrite(color, HIGH);
}