// **********************************************
// * *
// * NAME : Ahmad Hamzeh *
// * Program Name : Lightchaser with DDRB *
// * Date : 2023-02-21 *
// * Desc : Our first program to *
// * make a light chaser with *
// * patterns and a speaker *
// * with a simplified code *
// **********************************************
int delayTime = 100; // Pattern Delay is 100ms
int buttonPin = 6; // Pattern Delay is 100ms
int buttonValue = 0; // value of button
int speaker = 7; // pin for the BUZZER
int lightPattern = 1; // begin with pattern 1
void setup() { // set up functions (output and input)
// put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP); // setting buttonPin as input pullup
pinMode(speaker, OUTPUT); // setting speaker as output
DDRB = B111111; // set up DDRB so they are all outputs
} // end setup()
void loop() { // code under here will loop
// put your main code here, to run repeatedly:
buttonValue = digitalRead(buttonPin); // check value of button
if (buttonValue == LOW) { // check button value if pressed
tone(speaker,500,delayTime); // buzzer setup
delay (110); // wait 100 ms
lightPattern = lightPattern + 1; // change the pattern
if (lightPattern > 6 ) { // check if pattern exceeds max numbeer of patterns (6)
slightPattern = 1; // setting pattern back to 1
} // end if()
}// end if()
if (lightPattern == 1){ // check to see if first pattern should run
PORTB = B000100; // PORT B TO LED OFF OFF ON
delay(delayTime); // wait 100 ms
PORTB = B001100; // PORT B TO LED OFF ON ON
delay(delayTime); // wait 100 ms
PORTB = B011100; // PORT B TO LED ON ON ON
delay(delayTime); // wait 100 ms
} // end if()
else if (lightPattern == 2){ // check to see if second pattern should run
PORTB = B011000; // PORT B TO LED ON ON OFF
delay(delayTime); // wait 100 ms
PORTB = B010100; // PORT B TO LED ON OFF ON
delay(delayTime); // wait 100 ms
PORTB = B001100; // PORT B TO LED OFF ON ON
delay(delayTime); // wait 100 ms
} // end else if()
else if (lightPattern == 3){ // check to see if third pattern should run
PORTB = B010000; // PORT B TO LED ON OFF OFF
delay(delayTime); // wait 100 ms
PORTB = B001000; // PORT B TO LED OFF ON OFF
delay(delayTime); // wait 100 ms
PORTB = B000100; // PORT B TO OFF OFF ON
delay(delayTime); // wait 100 ms
} // end else if()
else if (lightPattern == 4){ // check to see if fourth pattern should run
PORTB = B010000; // PORT B TO LED ON OFF OFF
delay(delayTime); // wait 100 ms
PORTB = B011000; // PORT B TO LED ON ON OFF
delay(delayTime); // wait 100 ms
PORTB = B011100; // PORT B TO LED ON ON ON
delay(delayTime); // wait 100 ms
} // end else if()
else if (lightPattern == 5){ // check to see if fifth pattern should run
PORTB = B001100; // PORT B TO LED OFF ON ON
delay(delayTime); // wait 100 ms
PORTB = B010100; // PORT B TO LED ON OFF ON
delay(delayTime); // wait 100 ms
PORTB = B011000; // PORT B TO LED ON ON OFF
delay(delayTime); // wait 100 ms
} // end else if()
else if (lightPattern == 6){ // check to see if sixth pattern should run
PORTB = B000100; // PORT B TO LED OFF OFF ON
delay(delayTime); // wait 100 ms
PORTB = B001000; // PORT B TO LED OFF ON OFF
delay(delayTime); // wait 100 ms
PORTB = B010000; // PORT B TO LED ON OFF OFF
delay(delayTime); // wait 100 ms
} // end else if()
} // end loop()