/*The goal of this coding exercise is give each of the 4 buttons a different
function. Accomplishing Different tasks as each one is being pressed such as
Blinking LED in certain order or rotating the servo*/
#include <Servo.h> //include the servo library
using namespace std;
Servo servo; // creates a servo object that specifies which servo is being used
int LedStateG = LOW; //led state for the led1
int LedStateA = LOW; //led state for the led2
bool statusDefault = 1; //boolean to turn on the default function on/off
int statusCounter = 0; //counter to keep track of how many times the interrupt has been pressed
void altBlink(int num1, int num2){
int counter = 0;
unsigned long currentTime = 0; //variable to keep track of the value of millis
while (counter < 4) { //while the statusDefault is true
if (millis() - currentTime >= 200) { //checks interval
currentTime = millis();//updates the current time
if (LedStateG == LOW) { //if ledstate of green is low
LedStateG = HIGH; // set green to high
LedStateA = LOW; // set amber to low
}
else {
LedStateG = LOW; //set green to low
LedStateA = HIGH; // set amber to high
}
digitalWrite(num1, LedStateG); //turn on/off the led depending on the current state of the ledstate variable
digitalWrite(num2, LedStateA);//turn on/off the led depending on the current state of the ledstate variable
counter++;
}
}
}
void defaultstate() { //a void function for the default state of the circuit
unsigned long currentTime = 0; //variable to keep track of the value of millis
while (statusDefault == 1) { //while the statusDefault is true
if (millis() - currentTime >= 200) { //checks interval
currentTime = millis();//updates the current time
if (LedStateG == LOW) { //if ledstate of green is low
LedStateG = HIGH; // set green to high
LedStateA = LOW; // set amber to low
}
else {
LedStateG = LOW; //set green to low
LedStateA = HIGH; // set amber to high
}
digitalWrite(5, LedStateG); //turn on/off the led depending on the current state of the ledstate variable
digitalWrite(10, LedStateA);//turn on/off the led depending on the current state of the ledstate variable
}
}
}
void Q2(int j) { //function to run the command for question 2, moves servo back and forth
static unsigned long currentTime1 = 0; //keeps track of time 1
static unsigned long currentTime2 = 0; //keeps track of time 2
if (j == 10) { //if j equal 10
for (int i = 0; i < 120; i++) { //while i is less than 120
if (millis() - currentTime1 >= 8) { //check interval
currentTime1 = millis(); //updates time tracker
servo.write(i); //moves servo to value i
}
}
}
if (j == 120) { //if j is equal to 120
for (int i = 0; i > 10; i--) { //while i is greater than 10
if (millis() - currentTime2 >= 8) {//check interval
currentTime2 = millis();//update time tracker
servo.write(i); //moves servo to value i
}
}
}
}
void blink(int num) { //function to make led blink,
int counter = 0;
unsigned long currentTime = 0; //variable to keep track of the value of millis
while (counter < 4) { //while the statusDefault is true
if (millis() - currentTime >= 200) { //checks interval
currentTime = millis();//updates the current time
if (LedStateG == LOW) { //if ledstate of green is low
LedStateG = HIGH; // set green to high
}
else {
LedStateG = LOW; //set green to low
}
digitalWrite(num, LedStateG); //turn on/off the led depending on the current state of the ledstate variable
counter++;
}
}
}
int rupt() {
statusCounter++;
servo.write(145);
digitalWrite(10, HIGH); //set all LED's off
digitalWrite(9, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
if (statusCounter % 2 != 0) {
statusDefault = 0;
}
if (statusCounter % 2 == 0) {
statusDefault = 1;
}
}
void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT); //Orange LED
pinMode(6, OUTPUT); //Blue LED
pinMode(9, OUTPUT); //Red LED
pinMode(10, OUTPUT); //Green LED
pinMode(2, INPUT); //Button A
pinMode(7, INPUT); //Button D
pinMode(8, INPUT); //Button C
pinMode(12, INPUT); //Button B
servo.attach(11); //connect the servo object to pin 11
randomSeed(analogRead(0)); //sets seed for the random() to use when genrating a number to simulate randomness
pinMode(2, INPUT);
digitalWrite(10, HIGH); //set all LED's off
digitalWrite(9, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
attachInterrupt(digitalPinToInterrupt(2), rupt, RISING);
}
void loop() {
//PART 1
defaultstate(); //call default function
//PART 2
while (digitalRead(12)) { // while button is pressed
Q2(120); //call Question 2 function
Q2(10); //call Question 2 function
}
//PART 3
int counter = 0;
while (digitalRead(8)) { // while button is pressed
blink(10); //call blink function
digitalWrite(10, HIGH); //turn off led
blink(9); //call blink function
digitalWrite(9, HIGH); //turn off led
blink(6); //call blink function
digitalWrite(6, HIGH); //turn off led
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
blink(6); //call blink function
digitalWrite(6, HIGH); //turn off led
blink(9); //call blink function
digitalWrite(9, HIGH); //turn off led
blink(10); //call blink function
digitalWrite(10, HIGH); //turn off led
}
//PART 4
while (digitalRead(7)){ //while button is pressed
unsigned long currentTime1 = 0; //variable to keep track of the value of millis
if (millis() - currentTime1 >= 400) { //check interval
currentTime1 = millis(); //updates time tracker
int rand = random(2); //generates a random number form 0-2
if (rand == 0){ //state 1
servo.write(30); //move servo
digitalWrite(10, HIGH); //turn off led
blink(9); //call blink function
}
if (rand == 1){ //state 2
servo.write(90); //move servo
altBlink(9 ,6); //call blink function
digitalWrite(9, HIGH); //turn off led
digitalWrite(6, HIGH); //turn off led
}
if (rand == 2){ //state 3
servo.write(150);//move servo
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
blink(5); //call blink function
digitalWrite(5, HIGH); //turn off led
}
}
}
}