// ****************************************
// * *
// * NAME : Cayo Colin Carbonaro *
// * Program name : PChanger4.ino *
// * DATE : 2023-02-24 *
// * Desc : Light chaser with modifiable *
// * pattern by pressing button *
// * and a dial to change the speed*
// * *
// ****************************************
int timer = 0;
int timerMultiplier = 0;
int delayTime = 100; // sets a variable for the delay
int buttonPin = 6; // sets pin for the button
int speaker = 7; // sets pin for the speaker
int i = 0; // sets a variable to count the patterns
int set = 0; // sets a variable to count the sets
int sensor = A0;
int pattern[6][3] = { // array for each pattern and set
{16, 24, 28}, // pattern 1
{12, 20, 24}, // pattern 2
{4, 8, 16}, // pattern 3
{4, 12, 28}, // pattern 4
{24, 20, 12}, // pattern 5
{16, 8, 4} // pattern 6
};
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
DDRB = B011100;
pinMode(speaker, OUTPUT); // config pin speaker to an OUTPUT
pinMode(buttonPin, INPUT_PULLUP); // config pin buttonPin to an INPUT
}
void loop() {
timerMultiplier=(analogRead(sensor)/100)+1; // takes the values from the dial and shrinks them to have a value between 1-11
timer = delayTime * timerMultiplier; // muliplies the initial delay time by the multplier to get the final timer
if (digitalRead(buttonPin) == LOW) { //checks if the button is pressed
i++; // increases the pattern value by 1
if (i == 6) { // checks if the pattern value is 7
i = 0; // sets pattern value to 1
}
tone(speaker, i*100+200, 100); // plays a sound corresponding to the current pattern
delay(110); // small delay for code to run smoothly
}
if (set == 3) { // replays current pattern
set = 0;
}
PORTB = pattern[i][set]; // plays current pattern and set
delay(timer); // delay so pattern is viewable
set++; // goes up by 1 set
}