/*
Lenny Shpolanski, student #300237924
ENGR 1190 Final
Pushbutton circuits that generate a logic high are connected to digital pins:
2 - Button A (Red)
12 - Button B (Green)
8 - Button C (Blue)
7 - Button D (Yellow)
LED circuits are on the following digital pins:
5 - Orange LED
6 - Blue LED
9 - Red LED
10 - Green LED
Servo control line is on digital pin 11
Pushbutton connected to pin 2 (red) is set up with an interupt
*/
#include <Servo.h> //Includes servo library
Servo wiggly; //Creates servo object named "wiggly"
//----------------------------- Pause Function -----------------------------
//the pause function accepts an unsigned int as an input parameter
//and this will inidcate the number of milliseconds the program should pause for
void pause (unsigned long halt)
{
//the variable "temp" captures a timestamp of the current value given from the millis function
unsigned long temp = millis();
//the code then waits inside this while loop, until the difference between the current millis time,
//and the previous time of the timestamp, is less than or equal to the number of milliseconds
//provided in the input parameter.
while(millis()-temp <= halt)
{}
//it then exits the while loop, exiting the pause function
}
//----------------------------- Blink Function -----------------------------
//this function allows any LED to blink a given number of times.
//the first input parameter is the pin number of the LED,
//the second input parameter is the number of times that LED should blink
void blink(int pinNum, int blinkNum)
{
//the contents of this for loop make the selected LED blink,
//it will blink the number of times given by the second input parameter
for(;blinkNum>0;blinkNum--)
{
digitalWrite(pinNum, LOW);
pause(175);
digitalWrite(pinNum, HIGH);
pause(175);
}
}
//------------------- Interrupt Service Routine Function -------------------
void isr()
{
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
wiggly.write(5);
}
void setup()
{
// put your setup code here, to run once:
//designate pushbuttons pins as inputs
pinMode(2, INPUT); //Pushbutton for pin 2, it is able to detect interrupts
pinMode(12, INPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
//Attach ISR
attachInterrupt(digitalPinToInterrupt(2),isr,RISING);
//designate LED pins as outputs
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
//designate pin 11 as the servo PWM pin
wiggly.attach(11);
//turn all LEDs off at the start
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
void loop()
{
while(digitalRead(12)==HIGH)
{
int randnum = random(0,5);
if(randnum == 0)
{
blink(5,1);
}
if(randnum == 1)
{
blink(6,1);
}
if(randnum == 2)
{
blink(9,1);
}
if(randnum == 3)
{
blink(10,1);
}
}
while(digitalRead(8)==HIGH)
{
wiggly.write(10);
blink(9,2);
for(int i=10;i<=90;i++)
{
wiggly.write(i);
pause(3);
}
blink(10,3);
for(int i=90;i<=170;i++)
{
wiggly.write(i);
pause(3);
}
blink(6,4);
for(int i=170;i>=10;i--)
{
wiggly.write(i);
pause(3);
}
}
}