// **********************************************************
// * *
// * NAME : Mirza Shahriar *
// * Program Name: lightchasers.ino *
// * Date : 2023-02-21 *
// * Desc : Our first program to *
// * turn on led lights in multiple *
// * patterns and play a tone. *
// * *
// **********************************************************
int buttonPin = 6; // the pin for our button
int patternDelay = 110; // delay in ms between pattern steps
int patternNumber = 1; // tracks what pattern you're on
void setup() {
DDRB = B111111;
pinMode(buttonPin, INPUT_PULLUP); // config button to be an INPUT
pinMode(7, OUTPUT); // config speaker on pin 7
} // end setup ()
void loop() {
if (digitalRead(buttonPin) == LOW) { // check if pushed
patternNumber = patternNumber +1; // go to next pattern
if (patternNumber == 7) { // check which pattern it is on
patternNumber = 1; // if so reset to 1
} // end if ()
tone (7, 500, 100); // When the button is detected as pressed, play a tone of 500hz, for 100 ms.
delay (110); // To make the tone play and add a 110 ms delay to ensure it plays without affecting the program
} // end if ()
if (patternNumber == 1) { // check if its pattern 1
PORTB = B000100; // send power to pin 10
delay(patternDelay); // wait 110 ms
PORTB = B001100; // send power to pin 11 and 10
delay(patternDelay); // wait 110 ms
PORTB = B011100; // send power to pin 10, 11 and 12
delay(patternDelay); // wait 110 ms
}
else if (patternNumber == 2) { // check if its pattern 2
PORTB = B011000; // send power to pin 12 and 11
delay(patternDelay); // wait 110 ms
PORTB = B010100; // send power to pin 12 and 10
delay(patternDelay); // wait 110 ms
PORTB = B001100; // send power to pin 11 and 10
delay(patternDelay); // wait 110 ms
}
else if (patternNumber == 3) { // check if its pattern 3
PORTB = B010000; // send power to pin 12
delay(patternDelay); // wait 110 ms
PORTB = B001000; // send power to pin 11
delay(patternDelay); // wait 110 ms
PORTB = B000100; // send power top pin 10
delay(patternDelay); // wait 110 ms
}
else if (patternNumber == 4) { // check if its pattern 4
PORTB = B010000; // send power to pin 12
delay(patternDelay); // wait 110 ms
PORTB = B011000; // send power to pin 11 and 12
delay(patternDelay); // wait 110 ms
PORTB = B011100; // send power to pin 12,11 and 10
delay(patternDelay); // wait 110 ms
}
else if (patternNumber == 5) { // check if its pattern 5
PORTB = B001100; // send power to pin 11 amd 12
delay(patternDelay); // wait 110 ms
PORTB = B010100; // send power to pin 12 and 10
delay(patternDelay); // wait 110 ms
PORTB = B011000; // send power to pin 12 and 11
delay(patternDelay); // wait 110 ms
}
else if (patternNumber == 6) { // check if its pattern 6
PORTB = B000100; // send power to pin 10
delay(patternDelay); // wait 110 ms
PORTB = B001000; // send power to pin 11
delay(patternDelay); // wait 110 ms
PORTB = B010000; // send power to pin 12
delay(patternDelay); // wait 110 ms
} // end if()
} // end void loop ()