//Buzzers - passive or active
//active - 1 is to generate tone and 0 is off
//passive - uses PWM
// Setup ... done by Isaac and Shre
int ledPins[] = {10, 11, 12, 13};
int buttonPins[] = {2, 3, 4, 5};
int randomNumbers[10];
int gameTones[] = {262, 330, 392, 440};
int inputSequence[10];
bool buttonPressed = false;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(9, OUTPUT); // Buzzer
pinMode(6, INPUT_PULLUP); // Start button
}
// done by Ridgy
void generateRandomNumbers() { // Generate 10 random integers
for (int j = 0; j < 10; j++) {
randomNumbers[j] = random(1, 5); // Generate numbers between 1 and 5
}
}
// done for testing by Ridgy and Isaac
void printRandomNumbers() { // print random numbers
for (int i = 0; i < 10; i++) {
Serial.print(randomNumbers[i]);
Serial.print(" ");
}
Serial.println();
}
// done by Shre and Ridgy
void Color_Sound() {
bool buttonPressed = false; // Flag to track button press
while (true) {
for (int i = 0; i < 4; i++) {
buttonPressed = digitalRead(buttonPins[i]) == HIGH; // Check for button press
if (buttonPressed == LOW) {
digitalWrite(ledPins[i], HIGH);
tone(9, gameTones[i]);
delay(500);
digitalWrite(ledPins[i], LOW);
noTone(9);
break; // Exit inner loop on button press
}
}
}
}
// done by Shre and Isaac
void compare() {
for( int i = 0; i< inputSequence; i++)
if (inputSequence[i] != randomNumbers[i]){ // compare the index of input array and random number array
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
}
else {
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
}
}
// done by Shre and Ridgy
void inputStore() {
for (int i = 0; i < 10; i++) {
int buttonPressed = -1;
for (int j = 0; j < 4; j++) {
if (digitalRead(buttonPins[j]) == LOW) {
buttonPressed = j;
while(digitalRead(buttonPins[j]) == LOW);
break; // Exit loop as soon as one button is pressed
}
}
inputSequence[i] = buttonPressed;
delay(500);
}
compare();
}
//done by Shre
void loop() {
if (digitalRead(6) == LOW && !buttonPressed) {
buttonPressed = true;
} else if (digitalRead(6) == HIGH) {
buttonPressed = false;
}
if (buttonPressed) {
generateRandomNumbers();
//printRandomNumbers();
Color_Sound();
//randomSequence();
for (int i = 0; i < 10; i++) {
// Loop to activate each pin in the current sequence
for (int j = 1; j <= i; j++) {
int ledIndex = randomNumbers[j] - 1; // Map random number to LED index
// Activate the corresponding LED pin
digitalWrite(ledPins[ledIndex], HIGH);
// Optional: Play a tone if desired
tone(9, gameTones[ledIndex]);
// Delay for visibility
delay(500);
// Turn off the LED and tone
digitalWrite(ledPins[ledIndex], LOW);
noTone(9);
inputStore();
}
}
}
}
// Most of the funcions seems to be working individually, but it fails to work together.
// the main issues were with making the input array and compare which without any logical or syntax error seems to fail