#include <Servo.h>
/* Ayoub Naoui
300350875
17-Feb-2024 final exam*/
//2-Button_A is red button pin 2
//store all the buttons into int(s)
//servo pwm pin to 11 orange wire
int servoPin = 11;
//int position = 0;
//int count = 0;
int position =0; //global varible for position here
Servo myServo; //name of the servo motor that i will be using here
/*
Exercise 1. Designate pushbutton A to generate a rising interrupt.
The interupt service routine must move the servo to 15 degrees
and disable/enable the defualt led blink sequnece given mn the next task
with no pushbutton actuated/clicked/pushed, keep repeateding the following default led
*/
//push. button a is green buttn
bool buttonPressed = false; //Bool to indicate whether we enabled or disabled defualt led blink sequence
int puttonAPin = 2; //red led button pin used for the interupt service routine
void isr()
{
//designate pushbutton a to generate a rising interupt. the interupt service routine
//must move the servo to 15degrees and must enable or disable the deualt led blink sequence
if(digitalRead(2)==HIGH)
{
buttonPressed = true; //set the buttin pressed bool to true in the code
}
myServo.write(15); //need to move the servo by 15 degrees
}
unsigned long previousMillis = millis();
//Need to first set up all the leds states
void setup() {
//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 green
pinMode(12, INPUT); //push button b red button
pinMode(8, INPUT); //push button c blue button
pinMode(7, INPUT); //push button d ornage buttion
//In your setup code; turn off all the leds
//and move the servo to 5 degrees
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
myServo.write(5);
//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");
//attach our interupt //anytime we push this button, hold horse, carry thi out
//attachInterrupt(digitalPinToInterrupt(buttonPin), isr, RISING);
attachInterrupt(digitalPinToInterrupt(puttonAPin), isr, RISING);
}
void loop()
{
//while the bool is neabled turn on the leds
//while the bool is disbale turn off the leds
//with no push button actuated turn off all the leds
/*Exercise 1:
with no push button actuated/clicked keep repated the following default led
blink then turn if off
Blink the green led once then turn it off once
Blink the red led and then turn it off twice
Blink the blue led and then turn it off three times
Blink tyhe orange led and then turn if off four times
*/
//while the button is pressed button
while(!buttonPressed)
{
//start a timer instead of using delay
unsigned currentMillis = millis();
//while the arduino timer < our current timer + 120// keep the led in
for(int i = 0; i < 1; i++)
{
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(10, LOW); //turn on the green led for 120ms
}
//reset the timer here
currentMillis = millis();
//w
while(millis() < currentMillis + 120)
{
digitalWrite(10, HIGH); //turn off the green led for 120ms
}
}
//blink the red led and then turn it off TWICE
for(int j=0; j < 2; j++)
{
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(9, LOW);
}
//reset the timer here
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(9, HIGH);
}
}
//blink the blue led and turn if off THREE times
for(int k = 0; k < 3; k++)
{
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(6, LOW);
}
//reset the timer here
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(6, HIGH);
}
}
//blink the orange led and turn it off FOUR TIMES
for(int p= 0; p < 4; p++)
{
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(5, LOW);
}
//reset the timer here
currentMillis = millis();
while(millis() < currentMillis + 120)
{
digitalWrite(5, HIGH);
}
}
}
//while push button red B-12 is actuated/clicked/pushed osciallte the servo back and forth between 10degres and 120 in a smooth manner(stopping for 8m at every single degree step)
while (digitalRead(12) == HIGH){
//goes from 10 to 119 degrees
for (position=10; position<120; ){
myServo.write(position);
//ms delay before going to the next degree
//previousMillis = millis();
while(millis() - previousMillis <= 8)
{
}
//reset the timer here
//previousMillis = millis();
//delay(5);
position++;
myServo.write(position);
//goes out the while loop when button A is not pressed
if (digitalRead(2) == LOW)
break;}
//goes from 120 to 10 degrees
for (position=120; position>10; ){
myServo.write(position);
//8ms delay before going to the next degree
//previousMillis = millis();
while(millis() - previousMillis <= 8)
{
}
//reset the timer here
//previousMillis = millis();
//delay(5);
position--;
myServo.write(position);
//goes out the while loop when button A is not pressed
if (digitalRead(2) == LOW)
break;
}
//excersize three while push button c -8 blue is presssed need to
//implement an led chaser by blinking each led twice i the following order: GREEN LED, red led, amber led and revering the blink order
}
//while pushbutton C is clicked/pushed/implements
//a led chaser is used
while(digitalRead(8)==HIGH)
{
//create a usigned millis as a timer
unsigned currentMillis = millis();
//need to iterate twice for the green led to blink it twice
for(int i =0;i < 2; i++)
{
//reset the millis here
currentMillis = millis();
//need to turn on the led for 120ms
while(millis() < currentMillis +120)
{
//myServo.write(10);
digitalWrite(10, LOW);
}
//need to tur off the led for 120ms
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(10);
digitalWrite(10, HIGH);
}
}
//for the red led need to blink it twice as well here
for(int j= 0; j < 2; j++)
{
//need to turn on the red led
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(90);
digitalWrite(9, LOW);
}
//need to reset the millsi here as well
//need to turn off the red led
currentMillis = millis();
//need to make sure that the millis
while(millis() < currentMillis + 120)
{
//myServo.write(90);
digitalWrite(9, HIGH);
}
}
///iterate twice over the blue led here
for(int k =0; k < 2; k++)
{
currentMillis = millis();
while(millis() < currentMillis + 120 )
{
//myServo.write(170);
//need to turn on the blue lked and turn off the blue led here as well
digitalWrite(6, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(170);
digitalWrite(6, HIGH);
}
}
//need to iterate twice and turn the amber led on and off twic ehere as well
for(int k =0; k < 2; k++)
{
currentMillis = millis();
while(millis() < currentMillis + 120 )
{
//myServo.write(170);
digitalWrite(5, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(170);
digitalWrite(5, HIGH);
}
}
//to itereate over in the reverse direction for all the leds as well
for(int k =0; k < 2; k++)
{
currentMillis = millis();
while(millis() < currentMillis + 120 )
{
//myServo.write(170);
digitalWrite(5, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(170);
digitalWrite(5, HIGH);
}
}
for(int k =0; k < 2; k++)
{
currentMillis = millis();
while(millis() < currentMillis + 120 )
{
//myServo.write(170);
digitalWrite(6, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(170);
digitalWrite(6, HIGH);
}
}
for(int j= 0; j < 2; j++)
{
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(90);
digitalWrite(9, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(90);
digitalWrite(9, HIGH);
}
}
for(int i =0;i < 2; i++)
{
currentMillis = millis();
while(millis() < currentMillis +120)
{
//myServo.write(10);
digitalWrite(10, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120) //delay for 8 milliseconds here
{
//myServo.write(10);
digitalWrite(10, HIGH);
}
}
//if we aere not pressing the button then simply break
if(digitalRead(8)==LOW)
{
break;
}
}
//for the final excerisze
//need to continously and randonmly choose between one of the follwing leds
//state 1: Move the servo ro 30 degrees, then blink the green led twice
//state 2: Move the servo to 90 degreed and alternately blink the red led and then the blue led after it
//state 3: need to move the servo to 150 degreed and then blink the amber led twice
int randomnumber = random(1, 5); //choose a number between the leds to maintain the continous aspect of the led here
while(digitalRead(7)==HIGH)
{
unsigned long int currentMillis = millis();
switch(randomnumber)
{
case 1:
{
myServo.write(30); //need to moe the sero by thirty degrees here
//blink the green led twice
for(int i =0;i < 2; i++)
{
currentMillis = millis();
while(millis() < currentMillis +120)
{
//myServo.write(10);
digitalWrite(10, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(10);
digitalWrite(10, HIGH);
}
}
}while(millis() < currentMillis + 120);
digitalWrite(10, HIGH);
break;
case 2: //moe the servo to 90 degrees and alternativekly blibnk red led and the blue led twice
do
{
//need to alternaley blink the red led and the blue led twice
int count;
for(count = 0; count < 2; count++)
{
currentMillis = millis();
//turn on the red and he green leds
while(millis() < currentMillis + 120)
{
myServo.write(90);
digitalWrite(9, LOW); //red led
digitalWrite(6, LOW); //blue led
}
currentMillis = millis();
//turn off the red and the green leds repsectively
while(millis() < currentMillis + 120)
{
myServo.write(90);
digitalWrite(9, HIGH); //red led
digitalWrite(6, HIGH); //red led
}
}
}while(millis() < currentMillis + 120);
digitalWrite(6, HIGH);
break;
case 3: //case 3 need to move the servo to 150 degrees and the amber led twice
do
{
myServo.write(150); //need to moe the sero by thirty degrees here
//blink the amber led twice
for(int i =0;i < 2; i++)
{
currentMillis = millis();
while(millis() < currentMillis +120)
{
//myServo.write(10);
digitalWrite(5, LOW);
}
currentMillis = millis();
while(millis() < currentMillis + 120)
{
//myServo.write(10);
digitalWrite(5, HIGH);
}
}
}while(millis() < currentMillis + 120);
digitalWrite(5, HIGH);
break;
}
if(digitalRead(7)==LOW)
{
break;
}
}
}