// Ewan Stebbing / 300374166
// Midterm: Arduino Coding Exercise
// My overall stratagy while approaching this coding exercise was to at first attempt
// to find an elegent, compact solution to the problem. Even though I am not being
// marked on code style, I feel that it helps readability, and is a personal test of
// my problem solving ability. Wherever I felt that this approach was too demanding,
// I would just stumble to the first approach that meets the requirment of the problem
// and go from there. Using arrays to hold pin numbers, splitting large functions into
// smaller ones, and using various loops helped to complete this assignment compactly.
#include <Servo.h>
// create new servo object
Servo servo1;
// function headers
void buttonAPressed();
void buttonBPressed();
void buttonCPressed();
void buttonDPressed();
// initialize array that holds pin values of the LEDS, must be outside of
// setup because it needs to be reachable from all functions
const int ledPins[] = {5, 6, 9, 10};
// this function is used to prepare attachments for use
void setup() {
// attatch/declare types of pins
pinMode(2, INPUT);
pinMode(12, INPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// set LED pins to high so they start turned off
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
// set servo to pin 11 and set to zero degrees
servo1.attach(11);
servo1.write(0);
}
// This loop only serves as a place which other functions are called upon
// button presses, in order to avoid having a massive function
void loop() {
// Each if statement is asking if the pin attached to a button has been activated,
// and if it has the process will be passed to respective function
if (digitalRead(2) == HIGH)
buttonAPressed();
if (digitalRead(12) == HIGH)
buttonBPressed();
if (digitalRead(8) == HIGH)
buttonCPressed();
if (digitalRead(7) == HIGH) {
buttonDPressed();
}
}
// This function will be called when button A (pin 2) is activated
// using imbedded for loops, while the button is held down, the lights will flash in
// either a decsending or acsending order. If the button is released, this function
// will continue to execute all 4 loops of the lights
void buttonAPressed() {
// while button A is held
while (digitalRead(2) == HIGH) {
// this for loop makes sure the descending pattern goes twice
for (int j = 0; j < 2; j++) {
// this for loop makes sure each light is sequentially lit up
for (int i = 0; i < 4; i++) {
// ledPins[3-i] maps to an integer pin number that is linked to a given LED
// As i increases in this loop, the index of ledPins goes from 3, 2, 1, 0
digitalWrite(ledPins[3- i], LOW);
// delay is used here and throughout in order for our percepetion of the light
delay(350);
digitalWrite(ledPins[3- i], HIGH);
}
}
// like above, this loop makes sure acsending pattern goes twice
for (int j = 0; j < 2; j++) {
// this loop makes sure each light will be lit up
for (int i = 0; i < 4; i++) {
// index of ledPins will go 0, 1, 2, 3
digitalWrite(ledPins[i], LOW);
delay(350);
digitalWrite(ledPins[i], HIGH);
}
}
}
}
// This function is called when button B (pin 12) is activated
// This block is small enough that it could be reasonably put in the loop function
// however for consistency, I will give it a unique function
void buttonBPressed() {
// generate a random number [0,3]
int randomNum = random(4);
// loop twice
for (int i = 0; i < 2; i++) {
// uses the randomNum generated prior as the index for ledPins to determine LED
digitalWrite(ledPins[randomNum], LOW);
delay(350);
digitalWrite(ledPins[randomNum], HIGH);
delay(350);
}
}
// Function called when button C (pin 8) is activated
// Function only affects the servo, by alternating its posistion
// Between all servo movements, there is a 300ms delay to give time for the movement
void buttonCPressed() {
// first moves the servo to 100 degrees
servo1.write(100);
delay(300);
// while button is pressed
while (digitalRead(8) == HIGH) {
// alternate between 9 and 100 degrees
servo1.write(9);
delay(300);
servo1.write(100);
delay(300);
}
// reset to zero degrees for the benefit of other/future functions
servo1.write(0);
delay(300);
}
// Function called when button D (pin 7) is activated
// May be a more compact way to do this, but the advantage of this approach is
// this code is very easy to read
// Function performs a variety of LED and servo operations
void buttonDPressed() {
// Move the servo to 25 degrees, no delay is needed after because it will be covered
// by the delay for the LED
servo1.write(25);
// loop twice
for (int i = 0; i < 2; i++) {
// blink blue led
digitalWrite(ledPins[1], LOW);
delay(300);
digitalWrite(ledPins[1], HIGH);
delay(300);
}
// Servo to 81 degrees
servo1.write(81);
// loop 3 times
for (int i = 0; i < 3; i++) {
// blink green LED
digitalWrite(ledPins[3], LOW);
delay(300);
digitalWrite(ledPins[3], HIGH);
delay(300);
}
// Servo to 144 degrees
servo1.write(144);
// loop twice
for (int i = 0; i < 2; i++) {
// blink red
digitalWrite(ledPins[2], LOW);
delay(300);
digitalWrite(ledPins[2], HIGH);
delay(300);
// blink orange
digitalWrite(ledPins[0], LOW);
delay(300);
digitalWrite(ledPins[0], HIGH);
delay(300);
}
// Servo reset to 0
servo1.write(0);
delay(200);
}