//****************************************************
//*                                                  *
//* NAME                : Sami Zeitoun               *
//* PROGRAM NAME        : program1.ino               *
//* DATE                : 2023-02-21                 *
//* DESCRIPTION         : Program to shorten the     *
//*                       code for light             *
//*                        patterns and buzzer       *
//****************************************************

int timeDelayMultiplier = 0;           // divides potentiometer at different speed
int buttonPin     =6;                  // the pin for the button
int speakerPin    =7;                  // the pin for the speaker
int patternNumber  =1;                 // tracks what patter its on 
int patternDelay  =100;                // delay in milliseconds between pattern steps
int patternStep = 0;                   // the variable to track current pattern step
int timer       = 0;                   // the variable for the delay time 
int sensor      = A0;                  // the pin for the potentionmeter

int delayTime      = 100;              // delay in ms between pattern steps
int patternArray [6][3] = { {4,12,28} , {24,20,12} , {16,8,4} , {16,24,28}  , {12,20,24} , {4,8,16},  };   // array including all thje 6 patterns in decimal format


void setup() {
  DDRB = B111111;                                                                                           // initialize PORTB pins to OUTPUTS
  pinMode(buttonPin, INPUT_PULLUP);                                                                         // config pin 6 to an INPUT_PULLUP
  pinMode(speakerPin, OUTPUT);                                                                              // config pin 7 to an OUTPUT
}// end setup()


void loop(){
  timeDelayMultiplier =  (analogRead(sensor)/100) +1; // 1023/100        >   10.0   > .1(11,1) 
  timer = delayTime *timeDelayMultiplier;

  Serial.println(timer);

 if (digitalRead(buttonPin) == LOW ){                                                                       // check if button pressed
    patternNumber = patternNumber + 1;                                                                      // plays next pattern

    if (patternNumber == 6) {                                                                               // checks if patternNumber exeeds the maxium number of 6
      patternNumber = 0;                                                                                    // reset patternNumber to first pattern
    }// end if()  


    tone(speakerPin,500,100);                                                                               // play buzzer
    delay(110);                                                                                             // delay to keep buzzer playing
  } // end setup()

  PORTB = patternArray[patternNumber][patternStep]; // setting the current pattern using array
  delay(timer);                            // delay to keep pattern running
  patternStep++;                                  // move to next pattern step

  if (patternStep == 3){                        // check if pattern number exceeds
   patternStep = 0;                             // reset pattern number to 1
} // end if()
}