// Including libraries used in program
#include <Servo.h>
// Declaring all global constants used to correlate pin numbers to input and output devices
const int ORANGE_LED = 5;
const int BLUE_LED = 6;
const int RED_LED = 9;
const int GREEN_LED = 10;
const int BUTTON_A = 2;
const int BUTTON_B = 12;
const int BUTTON_C = 8;
const int BUTTON_D = 7;
const int SERVO_PIN = 11;
// Declaring global variable for using millis function for timing
unsigned long startMs;
// Declaring global constants for delay functions
const int millisDelay = 150;
const int ONE_BLINK = 1;
const int TWO_BLINKS = 2;
const int THREE_BLINKS = 3;
const int FOUR_BLINKS = 4;
// Declaring wiggly as my servo
Servo wiggly;
// Declaring volatile boolean variable to enable/disable default routine
volatile int defaultFlag = 2;
// Function to cause a delay without using the arduino delay function, and use the millis function instead
// Takes the time in millis to delay the code
void wait(unsigned long timeToWait){
// Setting startMs variable to the current time in Millis
startMs = millis();
// Using while loop to wait for the waiting time has passed before continuing
while(millis() - startMs <= timeToWait){
// Nothing happens inside this loop, just waits until the waiting time has passed before continuing through program
}
}
// Function to blink a specific LED light by using my wait function
void blinkLED(int led, int numberOfBlinks){
for(int i = 0; i < numberOfBlinks; i++){
// Turns on specific LED
digitalWrite(led, LOW);
// Wait function is called to cause a delay in the code
wait(millisDelay);
// Turns off specific LED after the wait function
digitalWrite(led, HIGH);
// Wait function is called again to cause a delay before returning to loop function
wait(millisDelay);
}
}
// Exercise 1 - task 1; Clicking pushbutton A turns off all LEDs and moves servo to 5 degrees, using rising edge interrupt
// Function for turning off all Leds and moving the servo to 5 degrees
void interruptRoutine(){
// Enabling/Disabling default behaviour
defaultFlag = defaultFlag * (-1);
digitalWrite(ORANGE_LED, HIGH);
digitalWrite(BLUE_LED, HIGH);
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, HIGH);
wiggly.write(145);
}
// Setup function to correlate pins to be take inputs or produce output signals
void setup() {
// configuring all buttons as input devices
pinMode(BUTTON_A,INPUT);
pinMode(BUTTON_B,INPUT);
pinMode(BUTTON_C,INPUT);
pinMode(BUTTON_D,INPUT);
// configuring all LEDs as output devices
pinMode(ORANGE_LED,OUTPUT);
pinMode(BLUE_LED,OUTPUT);
pinMode(RED_LED,OUTPUT);
pinMode(GREEN_LED,OUTPUT);
// designating pin 11 as my servo PWM pin
wiggly.attach(SERVO_PIN);
// attaching interrupt to button a
attachInterrupt(digitalPinToInterrupt(BUTTON_A), interruptRoutine, RISING);
}
// Constantly running loop function
void loop() {
// Default Behaviour
if(defaultFlag == 2){
digitalWrite(RED_LED, HIGH);
digitalWrite(BLUE_LED, HIGH);
for(int i = 0; i < 5; i++){
digitalWrite(ORANGE_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
wait(150);
digitalWrite(GREEN_LED, LOW);
digitalWrite(ORANGE_LED, HIGH);
wait(150);
}
digitalWrite(ORANGE_LED, HIGH);
digitalWrite(GREEN_LED, HIGH);
for(int i = 0; i < 5; i++){
digitalWrite(BLUE_LED, LOW);
digitalWrite(RED_LED, HIGH);
wait(150);
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
wait(150);
}
digitalWrite(RED_LED, HIGH);
}
// Exercise 1 - task 2; while push button B is pressed, oscillate servo between 10 and 120 degrees, stopping for 8ms at every degree
while(digitalRead(BUTTON_B)){
// Rotating servo incrementally
for(int j = 10; j <= 120; j++){
wiggly.write(j);
wait(8);
}
for(int j = 125; j >= 35; j--){
wiggly.write(j);
wait(8);
}
}
// Exercise 1 - task 3; blink each LED twice in following order (Green, Red, Blue, Orange)
while(digitalRead(BUTTON_C)){
blinkLED(GREEN_LED, TWO_BLINKS);
blinkLED(RED_LED, TWO_BLINKS);
blinkLED(BLUE_LED, TWO_BLINKS);
blinkLED(ORANGE_LED, TWO_BLINKS);
blinkLED(ORANGE_LED, TWO_BLINKS);
blinkLED(BLUE_LED, TWO_BLINKS);
blinkLED(RED_LED, TWO_BLINKS);
blinkLED(GREEN_LED, TWO_BLINKS);
}
// Exercise 1 - task 4; while button d is pressed, rotate the servo incrementally from 35 to 125 degrees
// also, 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.
while(digitalRead(BUTTON_D)){
// Using random function to randomly assign an int between 1-4 (Maximum of 5 is excluded)
int randomSelection = random(1, 4);
// Using if/else if statements to run blink function twice for specific LEDs, depending on which random number was generated
if(randomSelection == 1){
wiggly.write(30);
blinkLED(GREEN_LED, TWO_BLINKS);
}
else if(randomSelection == 2){
wiggly.write(90);
for(int i = 0; i < 3; i++){
digitalWrite(BLUE_LED, LOW);
digitalWrite(RED_LED, HIGH);
wait(150);
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
wait(150);
}
}
else if(randomSelection == 3){
wiggly.write(150);
blinkLED(ORANGE_LED, FOUR_BLINKS);
}
}
}