/*
Zainab Khan
LightChaser.ino
March 25, 2025
This program makes three LED lights go on and off
in a set pattern depending on a button press
using arrays and DDRB
with a potentiometer for speed adjustment
*/
int buttonPin = 7; // button is connected to pin 7
int patternNum = 0; // variable to track the state(pattern number to display)
int potPin = A5; // Potentiometer is connected to A5
int potVal; // variable to store potentiometer reading
int patternSpeed; // variable for the delay between patterns
int patternArray[6][3] { // an array with 6 rows and 3 columns
{B001000, B011000, B111000}, // pattern 0
{B110000, B101000, B011000}, // pattern 1
{B100000, B010000, B001000}, // pattern 2
{B100000, B110000, B111000}, // pattern 3
{B011000, B101000, B110000}, // pattern 4
{B001000, B010000, B100000}, // pattern 5
}; // end of patternArray
void setup() {
DDRB = B111111; // assigning pins 8 - 13 as outputs
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
} // end of void setup
void loop() {
int buttonVal = digitalRead(buttonPin); // when buttonVal is used in code it will read the value of buttonPin
potVal = analogRead(potPin); // read the value from the potentiometer - range (0-1023)
patternSpeed = map(potVal, 0, 1023, 0, 500);
// changes the 0-1023 reading to a 0-500 millisecond scale
if (buttonVal == LOW) { // checking if the buttonPins value is Low
patternNum++; // When buttonVal is equal to LOW patternNum is updated to patternNum + 1
} // end of if for buttonVal == LOW
for (int i = 0; i < 3; i++) { // i is equal to 0, if i is 3 or greater than 3, i will be updated by 1
PORTB = patternArray[patternNum][i];
delay(patternSpeed);
if (patternNum == 6) { // checking if patternNum = 6
patternNum = 0;
delay(patternSpeed);
} // end of if for patternNum 6
} // end of for loop
// printing what pattern it is displaying
Serial.print("Pattern State: ");
Serial.print(patternNum);
Serial.println(" ");
delay(100);
Serial.print(" ");
// printing what the pattern speed is
Serial.print("pattern Speed: ");
Serial.println(patternSpeed);
delay(100);
} // end of void loop