//*********************************
//*********************************
//*** Name: Asad Saad ***
//*** Date: 9/19/23 ***
//*** Program: Assignment4.ino ***
//*** Desc.: A program that ***
//*** changes traffic light ***
//*** pattern when a button ***
//*** pressed (port manipulation)**
//*** Potentiometer used to cont***
//*** rol the speed/delay of pat***
//*********************************
//*********************************
int ledDelay = 100; // sets up the delay for led on pin 13
int buttonVal = 0; // button value variable which can either be 0 or 1
int patternNum = 0; // sets pattern number which are 0-5 pattersn
int button = 7; // sets button to pin 8
int pVal [6][3] = {
{B001000, B011000, B111000},
{B110000, B101000, B011000},
{B100000, B010000, B001000},
{B100000, B110000, B111000},
{B011000, B101000, B110000},
{B001000, B010000, B100000}
}; //creates a two dimensional array, storing the values of all the different patterns
void setup() {
DDRB = B111000; // Sets arduino ports 13, 12, 11 to output and the rest to output using port manipulation
pinMode (button, INPUT_PULLUP); // Sets the pin to 5V, when pin is shorted it is registered as input
Serial.begin (9600); // sets serial output to 9600 bits
}//end setup
void loop() {
ledDelay =((analogRead(A0)/100) + 1) * 100; // sets the ledDelay variable equal to the value of the analogue input of the potentiometer, with additional calculations to make the process easier
Serial.println (ledDelay);
buttonVal = digitalRead(button); //sets button value variable to the same value as the actual button pin value
if (buttonVal == 0){
patternNum++; // if the button value is 1 then add 1 to the pattern number, changing the pattern
Serial.println ("0"); // prints 0 to the serial monitor when button is shorted
} //end if()
if (patternNum > 6){
patternNum = 0; //if the pattern number exceeds 6, it resets to zero, restricting it to 6 values
}// end if()
for(int x = 0; x < 3; x++){ //loops 3 times for 3 patterns
PORTB = pVal [patternNum][x]; //every loop it sets the value of PORTB to whatever the pattern num is, and whatever x is as the second array value
delay (ledDelay); //adds a delay for the pattern to be visible to the human eye
}// end for loop
}//end loop