#include <Servo.h>
/* Sofia Rodriguez 300374853
11-Apr-2024
ENGR 1190 Final Practice
Exercise 1
Constraints:
- Don´t use "delay()" funcion. Instead, use "millis()"
- Setup "button A" to generate a "rising edge interrupt"
NOTE: RISING triggers when the pin goes from low to high,
so the interrupt will happen when the button is pressed.
Exercise 1 instructions:
1. Clicking pushbutton A will turn off all the LEDs and move the servo
to 5° in the ISR (interrupt service routine).
2. While pushbutton B is pressed, randomly blink any of the LEDs. Note:
the LEDs must continuously blink while button B is pressed. There can
be no dead time where no leds are blinking.
3. While pushbutton C is pressed, oscillate the servo between 10°,
then 90°, and then 170°. Momentarily stop at each position, and:
- Blink the red led twice while at the 10° position.
- Blink the green led three times while at the 90° position.
- Blink the blue led four times while at the 170° position.
After the rotation to 170°, return the servo to 10° and keep repeating
the cycle while pushbutton C is pressed.
4. While pushbutton D is pressed, oscillate the servo between 35°
and 125°. Momentarily stop at each position, and:
- Alternately (NOT SEQUENTIALLY) blink the red led and green
led twice (exact pattern: red-green-red-green-OFF.)
- Sequentially, blink the blue led twice and then blink the
yellow led twice.
*/
//variables
Servo myservo;
int buttonA = 2; //Red. Note: pin 2 can detect interrupts
int buttonB = 12; //Green
int buttonC = 8; //Blue
int buttonD = 7; //Yellow
int orangeLED = 5;
int blueLED = 6;
int redLED = 9;
int greenLED = 10;
int pin; //For task 2
void setup() {
//setup code, to run once:
//designate all input devices: push button pins
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
pinMode(buttonD, INPUT);
//designate all output devices:
//LED pins
pinMode(orangeLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
//servo motor
myservo.attach(11);
Serial.begin(9600);
//attach ISR
attachInterrupt(digitalPinToInterrupt(2), buttonAPressed, RISING);
}
//setup delay function with millis(), called wait
void wait (unsigned long pause);
//setup blink function
void blink (int led, int reps);
// Interrupt Service Routine (ISR)
// Also 1. Button A task
void buttonAPressed () {
//moves the servo motor to 5°
myservo.write(5);
//turn off all the LEDs
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
void loop() {
//main code, to run repeatedly:
// 2. Buttom B task
while (digitalRead(buttonB) == HIGH){
//generate a random pin number from 5 to 10 (max number is exclusive)
pin = random(5,11);
//makes sure there is a LED connected to the random pin number
if(pin == orangeLED || pin == blueLED || pin == redLED || pin == greenLED){
//blink led once
blink(pin,1);
}
}
// 3. Button C task
while (digitalRead(buttonC) == HIGH){
//moves the servo motor to 10°
myservo.write(10);
wait(150);
//Blink the red led twice
blink(redLED,2);
//Exit loop if button C is not pushed anymore
if (digitalRead(buttonC) == LOW) break;
//moves the servo motor to 90°
myservo.write(90);
wait(150);
//Blink the green led 3 times
blink (greenLED,3);
//Exit loop if button C is not pushed anymore
if (digitalRead(buttonC) == LOW) break;
//moves the servo motor to 170°
myservo.write(170);
wait(150);
//Blink the blue led 4 times
blink(blueLED,4);
}
// 4. Button D task
while (digitalRead(buttonD) == HIGH){
//moves the servo motor to 35°
myservo.write(35);
wait(150);
//Alternately blink the red led and green led twice
blink(redLED,1);
blink(greenLED,1);
blink(redLED,1);
blink(greenLED,1);
//Exit loop if button D is not pushed anymore
if (digitalRead(buttonD) == LOW) break;
//moves the servo motor to 125°
myservo.write(125);
wait(150);
//Sequentially, blink the blue led twice and then blink the orange led twice
blink(blueLED,2);
blink(orangeLED,2);
}
}
//setup delay function with millis(), called wait
void wait (unsigned long pause){
//capture a timestamp in variable temp
unsigned long temp = millis();
/* If the difference in current time and the timestamp (temp)
is less than the required delay (pause), keep spooling the while loop.
The while loop will exit when the difference is 0*/
while (millis() - temp <= pause){
//Nothing to do here :)
//Just waiting...
}
}
//setup blink function
void blink (int led, int reps){
while (reps > 0){
reps--;
digitalWrite(led, HIGH);
wait(150);
digitalWrite(led, LOW);
wait(150);
digitalWrite(led, HIGH);
}
}