/* Nirmal Bains
300379902
ENGR1190 MIDTERM
February 14, 2024
*/
#include <Servo.h>
Servo myservo; // give servo a name
/*
attachInterrupt(digitalPinToInterrupt(abut))
void isr() interrupt service routinefunction
{
myservo.write(5);
(turn off all LEDs) quick immediate type thing
}
*/
void setup() {
// put your setup code here, to run once:
// designate LED pins as OUTPUTS
pinMode(5, OUTPUT); // orange LED
pinMode(6, OUTPUT); // blue LED
pinMode(9, OUTPUT); // red LED
pinMode(10, OUTPUT); // green LED
// designate pushbutton pins as INPUTS
pinMode(2, INPUT); // red button
pinMode(12, INPUT); // green button
pinMode(8, INPUT); // blue button
pinMode(7, INPUT); // orange button
// designate pin 11 as the servo PWM pin
myservo.attach(11);
// setup serial monitor to test the push buttons
Serial.begin(9600);
randomSeed(analogRead(0)); // shuffles the random function
}
// create a function that blinks LEDs
void blink1(int pin) // int pin allows any LED to be used as input
{
digitalWrite(pin, LOW); // turn on LED
delay(150); // 150ms delay
digitalWrite(pin, HIGH); // turn off LED
delay(150); // 150ms delay
}
void alternate1() // state 1 for task 4
{
myservo.write(30); // rotate servo to 30 degrees
blink1(6); // blink blue LED twice using blink function
blink1(6);
}
void alternate2() // state 2 for task 4
{
myservo.write(90); // rotate servo to 90 degrees
blink1(10); // blink green LED 3 times using blink function
blink1(10);
blink1(10);
}
void alternate3() // state 3 for task 4
{
myservo.write(120); // rotate servo to 120 degrees
for(int k = 1; k < 3; ++k) // create a loop so that it repeats twice
{
blink1(5); // blink orange LED
blink1(9); // blink red LED
}
}
/*void servoRotate() // created new function for task 3
{
int h = myservo.write(20); // wanted to assign servo position to a
// variable to be able to increment but I'm not sure how.
for(h = 20; h < 121; h++)
{
delay(8);
}
}
*/
void loop() {
// put your main code here, to run repeatedly:
// start by turning off all the LEDs
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
/* Task 1: while red button is pressed each LED must blink twice in
the order: green, red, blue, orange. AND then in reverse sequence.*/
while(digitalRead(2)) // while red button (pin 2) is pressed
{
blink1(10); // use the function I created earlier to blink LEDs
blink1(10); // each one blinks twice and then in reverse order.
blink1(9);
blink1(9);
blink1(6);
blink1(6);
blink1(5);
blink1(5);
blink1(5);
blink1(5);
blink1(6);
blink1(6);
blink1(9);
blink1(9);
blink1(10);
blink1(10);
}
/* Task 2: continuously and randomely select LED and blink twice. */
while(digitalRead(12)) // while green button is pressed
{
for(int i = 1; i < 10; i++) // repeat 10 times
{
int randLED = random(5, 11); // select a random pin from 5-10
blink1(randLED); // blink function blinks the randomely selected LED
blink1(randLED);
}
}
/* Task 3 : while blue button is pressed, incrementally rotate servo
between 20 and 120 degrees, stopping at every intermediate degree
position for 8ms. */
/* while(digitalRead(8)) //while blue button is pressed
{
servoRotate();
}*/
/* task 4 : while orange button is pressed, alternate between:
-move servo to 30 degrees and blink blue LED twice (state1)
-move servo to 90 degrees and blink green LED 3 times (state2)
-move servo to 120 degrees and alternately blink orange and red LEDs
twice. (state3)
*/
while(digitalRead(7)) // while orange button is pressed
{
alternate1(); // execute state 1
alternate2(); // execute state 2
alternate3(); // execute state 3
}
}