#include <Servo.h>
/* ayoub naoui
14-Feb-2024
Arduino Midterm Exercises
/*Task 1 back and forth Led tracer
while button A is presseed each led must blink twice in the follwoing sequence: green, red, blue, orange
and then each led must blink twice in reverse order*/
//Task 2
//Excercise 2: random led blinker while button B is presed continously and randomly select an Led and blink it twice
//generate a random number between 5 and 11
//Task 3
//3 Excerise 3: Servo oscillator while button C is pressed incrementally rotate the servo back and froth between 20 and 120 degrees, momentarily stopiing at every single intermediate position for 8ms
//Task 4
//excersice 4 Synchronized Dance
//while Button D is pressed alternate between the followoign three states
//1.Move the servo to 30degrees and blink blue LED twice
//2.Move the servo to 90degrees and blink the green LED twice
//3.Move the servo to 120degrees and alternately blink the orange and red led twice (orange BLINK -> RED BLINK -> Orange bink->red blink)
int position = 0;
int count = 0;
Servo myservo;
String UniqueComment = ">Nakul Is the Best";
void setup() {
// put your setup code here, to run once:
//designate all LED pins
pinMode(5, OUTPUT); //amber led
pinMode(6, OUTPUT); //blue led
pinMode(9, OUTPUT); //red led
pinMode(10, OUTPUT); //green led
//designate all push button pins
pinMode(2, INPUT); //push button a (red)
pinMode(12, INPUT); //push button b (green)
pinMode(8, INPUT); //push button c (blue)
pinMode(7, INPUT); //push button d (yellow)
randomSeed(analogRead(0));
//designate pin 11 to servo motor
myservo.attach(11);
Serial.begin(9600); //number of bits of 1 and 0s set ever second
Serial.println("Hello");
}
int leds[4] = {5, 6, 9, 10}; //random led
bool hasWrittenOnce = false; //To test if the serial monitor has written something once
int count2 = 0;
void loop() {
// put your main code here, to run repeatedly:
//turn off all the LEDs
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
/*Task 1 back and forth Led tracer
while button A is presseed each led must blink twice in the follwoing sequence: green, red, blue, orange
and then each led must blink twice in reverse order*/
while(digitalRead(2)==HIGH) //while button A is pressed
{
//iterate once need to go from green led (10) to red led (9) to blue led (6) and finally to the orange led pin (5)
for(count2 = 0; count2 < 1; count2++)
{
//blink twice green LED
digitalWrite(10, LOW);
delay(150);
digitalWrite(10, HIGH);
delay(150);
digitalWrite(10, LOW);
delay(150);
digitalWrite(10, HIGH);
delay(300);
//blink twice red LED
digitalWrite(9, LOW);
delay(150);
digitalWrite(9, HIGH);
delay(150);
digitalWrite(9, LOW);
delay(150);
digitalWrite(9, HIGH);
delay(300);
//blink twice blue LED
digitalWrite(6, LOW);
delay(150);
digitalWrite(6, HIGH);
delay(150);
digitalWrite(6, LOW);
delay(150);
digitalWrite(6, HIGH);
delay(300);
//blink twice orange LED
digitalWrite(5, LOW);
delay(150);
digitalWrite(5, HIGH);
delay(150);
digitalWrite(5, LOW);
delay(150);
digitalWrite(5, HIGH);
delay(300);
//If the person is not clicking on the led then break out from the for loop goes back to the first for loop
if(digitalRead(2)==LOW)
{
break;
}
} //loop for on iteration and blink all of the folowing leds (in reverse order)
for(count2 = 0; count2 < 1; count2++)
{
//blink the orange led
digitalWrite(5, LOW);
delay(150);
digitalWrite(5, HIGH);
delay(150);
digitalWrite(5, LOW);
delay(150);
digitalWrite(5, HIGH);
delay(300);
//blink twice blue LED
digitalWrite(6, LOW);
delay(150);
digitalWrite(6, HIGH);
delay(150);
digitalWrite(6, LOW);
delay(150);
digitalWrite(6, HIGH);
delay(300);
//blink twice red LED
digitalWrite(9, LOW);
delay(150);
digitalWrite(9, HIGH);
delay(150);
digitalWrite(9, LOW);
delay(150);
digitalWrite(9, HIGH);
delay(300);
//blink twice green LED
digitalWrite(10, LOW);
delay(150);
digitalWrite(10, HIGH);
delay(150);
digitalWrite(10, LOW);
delay(150);
digitalWrite(10, HIGH);
delay(300);
//If the person is not clicking on the led then break out from the for loop goes back to the first for loop
if(digitalRead(2)==LOW)
{
break;
}
}
}
/* method 1
//2 Excercise 2: random led blinker while button B is presed continously and randomly select an Led and blink it twice
//generate a random number between 5 and 11
int randomPin = random(5, 11);
//while button B or 12 is pressed
while(digitalRead(12)==HIGH)
{
int randomPin = random(5, 11); //Need to generate a random number bewteeb 5 and 10 so need to add 1 to it so its 11 (1-10) excluding 11
for(int i = 0; i < 2; i++) { //Turn on and iterate a random led twice
digitalWrite(randomPin, HIGH); //Turn on any random led
delay(500); //delay for 500ms to blink the led
digitalWrite(randomPin, LOW); // Turn of the random led so that the next randpm led can be turned on
delay(500); //delay for 500ms to blink the led
}
delay(1000);
//if the user is not pressing anybutton then can break from the loop
if(digitalRead(12)==LOW)
{
break;
}
}
*/
//method 2
//2 Excercise 2: random led blinker while button B is presed continously and randomly select an Led and blink it twice
//generate a random number between 5 and 11
//int randomPin = random(5, 11);
//while button B or 12 is pressed
while(digitalRead(12)==HIGH)
{
//int randomPin = random(5, 11); //Need to generate a random number bewteeb 5 and 10 so need to add 1 to it so its 11 (1-10) excluding 11
for(int i = 0; i < 2; i++) { //Turn on and iterate a random led twice
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))], HIGH); //Turn on any random led
delay(500); //delay for 500ms to blink the led
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))], LOW); // Turn of the random led so that the next randpm led can be turned on
delay(500); //delay for 500ms to blink the led
}//if the user is not pressing anybutton then can break from the loop
if(digitalRead(12)==LOW)
{
break;
}
}
//3 Excerise 3: Servo oscillator while button C is pressed incrementally rotate the servo back and froth between 20 and 120 degrees, momentarily stopiing at every single intermediate position for 8ms
while(digitalRead(8)==HIGH)
{
//goes from 20 to 119 degrees
for(position = 20; position < 120;)
{
//move the servo motor by 1 degree at each iteration
//this gives us the incrementally rotate servo back and forth between 20 and 120
myservo.write(position);
//print to the serial screen what the rotation was at each single degree
Serial.print("Degrees Rotation= ");
Serial.println(position);
//8ms delay before going to the next degree
delay(8);
//increment by 1 degree each time
position++;
//goes out the while loop when button C is not presse
if(digitalRead(8)==LOW)
{
break;
}
}
delay(250); //delay at exactly 120 degree when reached there
//goes from 120 to 20 degrees
for(position = 120; position > 20;)
{
//move the servo motor by 1 degree at each iteration
myservo.write(position);
//print to the serial screen what the rotation was at each single degree
Serial.print("Degrees Rotation= ");
Serial.println(position);
//8ms delay before going to the next degree
delay(8);
//decrement by 1 degree each time
position--;
//goes out the while loop when button C is not presse
if(digitalRead(8)==LOW)
{
break;
}
}
delay(250); //delay at 20 degrees when reaches there
}
//excersice 4 Synchronized Dance
//while Button D is pressed alternate between the followoign three states
//1.Move the servo to 30degrees and blink blue LED twice
//2.Move the servo to 90degrees and blink the green LED twice
//3.Move the servo to 120degrees and alternately blink the orange and red led twice (orange BLINK -> RED BLINK -> Orange bink->red blink)
while(digitalRead(7)==HIGH)
{
//Rotate the serbo motor by 30 degrees
myservo.write(30);
//Blink the blue led twice
for(int i = 0; i < 2; i++)
{
//turn on the blue led
digitalWrite(6, LOW);
//delay for 50ms
delay(50);
//turn of the blue led
digitalWrite(6, HIGH);
//delay the led for 50ms
delay(50);
//if the user is not pressing the yellow button then break the while loop
if(digitalRead(7)==LOW)
{
break;
}
}
//rotate the servo motor to 90 degres
myservo.write(90);
//blink the green led 3 times
for(int i = 0; i < 3; i++)
{
//turn on the green led
digitalWrite(10, LOW);
//delay the green led by 30ms
delay(50);
//turn off the green led
digitalWrite(10, HIGH);
//delay the green led by 50 ms
delay(50);
//is tehe user is not pressing the yeloow button then break the while loop
if(digitalRead(7)==LOW)
{
break;
}
}
//then turn the servo to 120 degrees
myservo.write(120);
//alternately turn on the orange and the red leds twice
for(count = 0;count < 2; count++)
{
//turn on the orange led
digitalWrite(5, LOW);
//delay the led for 100 ms
delay(100);
//then turn on the red led
digitalWrite(9, LOW);
//delay 150 ms (miliseconds)
delay(100);
//turn off orange led
digitalWrite(5, HIGH);
//delay fro 100ms
delay(100);
//turn off the red led
digitalWrite(9, HIGH);
//delay 200 ms (miliseconds)
delay(100);
}
}
}