//Nima Karami- student#: 300375493- date: feb-14th-2024
//First midterm of ENGR 1190, Instructor Nakul Verma
//In the midterm, we are looking forward to getting
/*In the following part of the code, first the library "Servo.h" is added
so that we can utilize the servo motor and library "Servo" is added to
The library manager.*/
#include <Servo.h> // including the library servo
Servo wiper; // naming the servo motor as wiper
int tempAngle;// global temporary varibale for angle which is used by servo motor
/* The following part of the code is void setup function which is executed once.
In other words, we set up our arduino by means fo this function. */
void setup() {
/*In this part pinMode command, pin number and (INPUT or OUTPUT) are used to
program pins to send or recive data (signal). Moreover, attach function is used to
connect the servo motor(wiper) to pin 11.*/
pinMode(2,INPUT); // setting pin # 2 to recive data(signal) from Button_a
pinMode(12,INPUT); // setting pin # 12 to recive data(signal) from Button_b
pinMode(8,INPUT); // setting pin # 8 to recive data(signal) from Button_c
pinMode(7,INPUT); // setting pin # 7 to recive data(signal) from Button_d
pinMode(5,OUTPUT); // setting pin # 5 to send data(signal) to Orang LED
pinMode(6,OUTPUT); // setting pin # 6 to send data(signal) to Blue LED
pinMode(9,OUTPUT); // setting pin # 9 to send data(signal) to Red LED
pinMode(10,OUTPUT); // setting pin # 10 to send data(signal) to Green LED
wiper.attach(11); //setting servo(wiper) as pin 11.
}
/* The following part of the code is void loop function which runs over and over
,therefore, we use it to run all the commands that we need run for ever in this part.*/
void loop() {
//start by turning off all the LEDs. To do so, we set all the pins responsible for
//each LED to active_LOW (HIGH).
digitalWrite(5,HIGH); //setting pin # 5 to active_LOW(HIGH)
digitalWrite(6,HIGH); //setting pin # 6 to active_LOW(HIGH)
digitalWrite(9,HIGH); //setting pin # 9 to active_LOW(HIGH)
digitalWrite(10,HIGH); //setting pin # 10 to active_LOW(HIGH)
/* The following part of the code is responsible for Task 1 of the midterm in which
while button a is pressed, each LED must blink twice in sequence of green, red , blue,
orange and then blink each LED must blink twice in the reverse order.*/
//------------------------------------------------------------------------------------
// A "while loop" is used since we need LEDs to blink "while" button a is pressed
// digitalRead command is responsible to tell us if button a is pressed or not
// by returning the value of HIGH (pressed) or LOW (not pressed)
while(digitalRead(2)==HIGH)//if button a is pressed enters the while loop
{
digitalWrite(10,LOW); //turning the green led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(10,HIGH);//turning the green led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(10,LOW); //turning the green led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(10,HIGH);//turning the green led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(9,LOW); //turning the red led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(9,HIGH);//turning the red led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(9,LOW); //turning the red led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(9,HIGH); //turning the red led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(6,LOW);//turning the blue led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(6,HIGH);//turning the blue led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(6,LOW);//turning the blue led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(6,HIGH);//turning the blue led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(5,LOW);//turning the orange led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(5,HIGH);//turning the orange led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(5,LOW);//turning the orange led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(5,HIGH);//turning the orange led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(5,LOW);//turning the orange led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(5,HIGH);//turning the orange led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(5,LOW);//turning the orange led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(5,HIGH);//turning the orange led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(6,LOW);//turning the blue led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(6,HIGH);//turning the blue led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(6,LOW);//turning the blue led light on.
delay(150);//keeping it on for 150 ms.
digitalWrite(6,HIGH);//turning the blue led light off.
delay(150);//keeping it off for 150 ms.
digitalWrite(9,LOW); //turning the red led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(9,HIGH);//turning the red led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(9,LOW); //turning the red led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(9,HIGH); //turning the red led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(10,LOW); //turning the green led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(10,HIGH);//turning the green led light off.
delay(150); //keeping it off for 150 ms.
digitalWrite(10,LOW); //turning the green led light on.
delay(150); //keeping it on for 150 ms.
digitalWrite(10,HIGH);//turning the green led light off.
delay(150); //keeping it off for 150 ms.
}
/* The following part of the code is responsible for Task 2 of the midterm in which
while button b is pressed, continuously and randomly LED lights will blink by means of
random function and a forloop to generate a new number befor turning on each LED .*/
//------------------------------------------------------------------------------------
// A "while loop" is used since we need LEDs to blink "while" button b is pressed
// digitalRead command is responsible to tell us if button a is pressed or not
// by returning the value of HIGH (pressed) or LOW (not pressed)
//Random functon is responssiblefor generating new numbers so that we can use them to
//choose which LED to blink
//for loop is use to generate 4 random numbers per each time the while loop is compiled
while(digitalRead(12)==HIGH) //if button b is pressed enters the while loop
{
for(int i=0;i<4;i++)//for loop to generate random number 4 times
{
int tempx=random(1,5);// generating a random nuber between 1 and 4 and setting it equal ro a temp variable
if(tempx==1) // if the random nuber number is equal to 1 blinks the green led twice
{
digitalWrite(10,LOW);
delay(150);
digitalWrite(10,HIGH);
delay(150);
digitalWrite(10,LOW);
delay(150);
digitalWrite(10,HIGH);
delay(150);
} else if(tempx==2) // if the random nuber number is equal to 2 blinks the red led twice
{
digitalWrite(9,LOW);
delay(150);
digitalWrite(9,HIGH);
delay(150);
digitalWrite(9,LOW);
delay(150);
digitalWrite(9,HIGH);
delay(150);
} else if(tempx==3) // if the random nuber number is equal to 3 blinks the blue led twice
{
digitalWrite(6,LOW);
delay(150);
digitalWrite(6,HIGH);
delay(150);
digitalWrite(6,LOW);
delay(150);
digitalWrite(6,HIGH);
delay(150);
} else if(tempx==4) // if the random nuber number is equal to 4 blinks the orange led twice
{
digitalWrite(5,LOW);
delay(150);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(150);
digitalWrite(5,HIGH);
delay(150);
}
}
}
/* The following part of the code is responssible for Task 3 in which, while button
c is pressed , the servo motor Incrementally rotates back and forth between 20 and
120 degrees and momentarily stops at every single intermediate degrr position for 8 ms.*/
//------------------------------------------------------------------------------------
// A "while loop" is used since we need the servo motor to rotate "while" button c is pressed
// digitalRead command is responsible to tell us if button c is pressed or not
// by returning the value of HIGH (pressed) or LOW (not pressed)
// two for loops are used in task 3 first one for moving the servo motor from 20 to 120 degrees
// the second one for moving the servo motor from 120 to 20 degrees. Also use of for loops
// makes it possible to stop the servo motor at each angle for 8ms.
while(digitalRead(8)==HIGH)//if button c is pressed enters the while loop
{
//for loop make the servo motor to move from 20 to 120 degrees and making it stop
// at each degree for 8 ms
for(tempAngle=20; tempAngle<=120; tempAngle++){
wiper.write(tempAngle); // servo stops at the tempAngle which is an int for dgree
delay(8); //stopping for 8ms
}
//for loop make the servo motor to move from 120 to 20 degrees and making it stop
// at each degree for 8 ms
for(tempAngle=120; tempAngle>=20;tempAngle--){
wiper.write(tempAngle);// servo stops at the tempAngle which is an int for dgree
delay(8);//stopping for 8ms
}
}
/* Next part of the code is responsible for Task 4 in which, while button d is pressed
1- The servo will move to 30 degrees and the blue LED will blink twice.
2- The servo will move to 90 degrees and the blue LED will blink three times.
3- The servo will move to 120 degrees, the orange and red LEDs will alternately blink twice. */
//------------------------------------------------------------------------------------------------
// A "while loop" is used since we need the servo motor to rotate and LEDs to blink "while" button d
// is pressed digitalRead command is responsible to tell us if button c is pressed or not
// by returning the value of HIGH (pressed) or LOW (not pressed)
while(digitalRead(7)==HIGH)//if button d is pressed enters the while loop
{
wiper.write(30);//Moving the Motor to 30 degrees
//blinking the blue led twice
digitalWrite(6,LOW);
delay(150);
digitalWrite(6,HIGH);
delay(150);
digitalWrite(6,LOW);
delay(150);
digitalWrite(6,HIGH);
delay(150);
wiper.write(90);//Moving the Motor to 90 degrees
//blinking the blue LED will three times.
digitalWrite(10,LOW);
delay(150);
digitalWrite(10,HIGH);
delay(150);
digitalWrite(10,LOW);
delay(150);
digitalWrite(10,HIGH);
delay(150);
digitalWrite(10,LOW);
delay(150);
digitalWrite(10,HIGH);
delay(150);
wiper.write(120);//Moving the Motor to 120 degrees
//blinking the orange and red LEDs will alternately twice.
digitalWrite(5,LOW);
delay(150);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(9,LOW);
delay(150);
digitalWrite(9,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(150);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(9,LOW);
delay(150);
digitalWrite(9,HIGH);
delay(150);
}
}