/** Advanced Sequence
* @author Torin Zaugg
* @date 02/12/2018
* @comments Working with Arduino UNO
*/
int lamps[] = {3, 4, 5, 6, 9, 10, 11}; //This array contains the pins of all the LEDs
#define Button 2 //This defines which pin the button is on
int sequenceRuns = 5; //The number of times the sequence should run when the button is pressed
int sequenceDirection = 1; //Which direction the sequence should run in
boolean doSequence = false; //Wether or not the sequence should run, by default it should not (thus false)
/*
* The setup function - this code is only ran once
* Use it to setup the interrupt from the butotn, set the direction of the LED pins and button pin
*/
void setup() {
/*
* attachInterrupt() tells the arduino to run a function each time the button is pressed (this is called an interrupt.
* It takes 3 parameters:
* The first is the pin that triggers the interrupt
* The second is the name of the function to run
* The thrid is what to watch for - falling is the pin going to ground, rising is the pin going to +5V.
*/
attachInterrupt(digitalPinToInterrupt(Button), runSequence, FALLING);
//Set the direction of the LEDs to output by running through the array with a for loop
for (int i = 0; i < 7; i = i + 1){
pinMode(lamps[i], OUTPUT);
}
pinMode(Button, INPUT); //Set the button to an output
//Turn off all the LEDs buy running through the array with a for loop
for (int i = 0; i < 7; i = i + 1){
digitalWrite(lamps[i], LOW);
}
}
/*
* The runSequence function - this code is ran each time the interrupt is run.
* The interrupt is run by pressing the button
*/
void runSequence(){
doSequence = true; //Next time loop() is called, the sequence will run
}
/*
* The loop function - this code is run repeatedly
*/
void loop() {
if(doSequence){ //If doSequence is true (the button has been pressed)
//Repeat as many times as the value of sequenceRuns
for(int i = 0; i < sequenceRuns; i = i + 1){
if(sequenceDirection == 1){ //If sequenceDirection is set to 1
for (int i = 0; i < 7; i = i + 1){ //For each LED in the array
digitalWrite(lamps[i], HIGH); //Turn the LED on
delay(250); //Wait 250 miliseconds
}
for (int i = 0; i < 7; i = i + 1){ //For each LED in the array
digitalWrite(lamps[i], LOW); //Turn off the LED
delay(250); //Wait 250 miliseconds
}
}else{ //If sequenceDirection is not set to 1
for (int i = 7; i > -1; i = i - 1){ //For each LED in the array in reverse order
digitalWrite(lamps[i], HIGH); //Turn on the LED
delay(250); //Wait 250 miliseconds
}
for (int i = 7; i > -1; i = i - 1){ //For each LED in the array (stil in reverse)
digitalWrite(lamps[i], LOW); //Turn off the LED
delay(250); //Wait 250 miliseconds
}
}
}
doSequence = false; //set doSequence to false to the sequence only runs the number of times
//of sequenceRuns and doesn't run every time loop() is called.
}
}