// ****************************************************
// *                                                  *
// * NAME        : Yazan Alhamayda                    *
// * Program Name: DDRB.ino                           *
// * Date        : 2023-02-21                         *
// * Desc        : This program is to light           *
// *           the chaser with 6 pattern outcomes     *
// *           using input data directional register  *                                       *
// ****************************************************


int buttonPin    = 6;    // the pin for our button
int patternDelay = 110;  // delay in ms between pattern steps
int patternNumber = 1;   // tracks what pattern your on

void setup() {
  DDRB = B111111;                       // 1 = OUTPUT a  0 = INPUT DATA DIRECTION REGISTER
  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
    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 add 110 ms delay to ensure it played without affecting the program
    patternNumber = patternNumber +1;            // go to next pattern
    if (patternNumber == 7 ){                    // check if we exceeded the max patterns
      patternNumber = 1;                         // if so reset pattern to 1
    } // end if()
  } // 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 loop()