// name: Adrian Jacobs, student ID: 300356808
// including servo library
#include <Servo.h>
// creating variable for servo motor
Servo servoMotor;
// temporary variable that will be given the millis value
unsigned long tempTime = 0;
// defining a function that will blink an LED for 500 milliseconds
void blinkLED(int pinNumber) {
// giving the millis value to tempTime
tempTime = millis();
// keeing the LED on until millis become greater than tempTime by at least 500 milliseconds
while (millis() < tempTime + 500) {
digitalWrite(pinNumber, LOW);
}
// turning the LED off
digitalWrite(pinNumber, HIGH);
}
// defining a new delay function using the millis function
void millisDelay(int delayLength) {
// giving the millis value to tempTime
tempTime = millis();
// waiting until millis becomes greater than tempTime by at least 500 milliseconds
while (millis() < tempTime + delayLength) {
}
}
void setup() {
// pin setup for the 4 pushbuttons
pinMode(2, INPUT); // button A (blue button)
pinMode(7, INPUT); // button D (red button)
pinMode(8, INPUT); // button C (yellow button)
pinMode(12, INPUT); // button B (green button)
// pin setup for the 4 LEDs
pinMode(5, OUTPUT); // amber LED
pinMode(6, OUTPUT); // blue LED
pinMode(9, OUTPUT); // red LED
pinMode(10, OUTPUT); // green LED
// setup for the serial monitor
Serial.begin(9600);
// pin setup for the servo motor
servoMotor.attach (11);
}
void loop() {
// begin by turning all of the LEDs off
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
// default LED bahavior
// for (int i = 0; i < 5; i++) {
// blinkLED(5);
// millisDelay(250);
// blinkLED(10);
// millisDelay(250);
// }
// for (int i = 0; i < 5; i++) {
// blinkLED(6);
// millisDelay(250);
// blinkLED(9);
// millisDelay(250);
// }
// detecting if button is A is being pressed
if (digitalRead(2) == HIGH) {
// couldn't figure out how to get an interrupt to work
}
// detecting if button B is being pressed
if (digitalRead(12) == HIGH) {
// moving the servo motor to 10 degrees
servoMotor.write(10);
// waiting 500 milliseconds
millisDelay(500);
// increasing the angle of the servo motor by 1 degree every 8 milliseconds
for (int i = 10; i < 121; i++) {
servoMotor.write(i);
millisDelay(8);
}
}
// detecting if button C is being pressed
if (digitalRead(8) == HIGH) {
// blinking the green LED
blinkLED(10);
// waiting 250 milliseconds
millisDelay(250);
blinkLED(10);
millisDelay(250);
// blinking the red LED
blinkLED(9);
millisDelay(250);
blinkLED(9);
millisDelay(250);
// blinking the blue LED
blinkLED(6);
millisDelay(250);
blinkLED(6);
millisDelay(250);
// blinking the amber LED
blinkLED(5);
millisDelay(250);
blinkLED(5);
millisDelay(250);
blinkLED(5);
millisDelay(250);
blinkLED(5);
millisDelay(250);
blinkLED(6);
millisDelay(250);
blinkLED(6);
millisDelay(250);
blinkLED(9);
millisDelay(250);
blinkLED(9);
millisDelay(250);
blinkLED(10);
millisDelay(250);
blinkLED(10);
}
// detecting if button D is being pressed
if (digitalRead(7) == HIGH) {
// randomly generating either 0, 1, or 2
int state = rand() % 3;
// switch case with the 3 random states
switch(state) {
case 0:
// moving the servo motor to 30 degrees
servoMotor.write(30);
// blinking the green LED twice
blinkLED(10);
millisDelay(250);
blinkLED(10);
millisDelay(250);
break;
case 1:
// moving the servo motor to 90 degrees
servoMotor.write(90);
// blinking LEDs in the following pattern: red, blue, red, blue
for (int i = 0; i < 3; i++) {
blinkLED(9);
millisDelay(250);
blinkLED(6);
millisDelay(250);
}
break;
case 2:
// moving the servo motor to 150 degrees
servoMotor.write(150);
// blinking the amber LED 4 times
for (int i = 0; i < 4; i++) {
blinkLED(5);
millisDelay(250);
}
break;
}
}
}