int ledArray[10];
int buttonArray[10];
int loopCounter;
int ledCounter;
int inputCounter;
int game;
int waiting;
int pass;
int wait;
int timeUp;
//ALL THE BUTTONS & LEDS ARE GIVEN NAMES HERE SO THEY'RE EASIER TO REFERENCE
const int buttonRed = 6;
const int buttonYellow = 7;
const int buttonGreen = 9;
const int buttonBlue = 8;
const int r = 2;
const int y = 3;
const int b = 4;
const int g = 5;
const int x = 10;//LED ON PIN 10 IS THE WHITE LED USED FOR PROMPTS
///USED FOR PROCESSING AND COMPARING INPUTS
int randNumber;
int array_Cmp;
void youWin ()
{ digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(3, LOW);
delay(100);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(4, LOW);
delay(100);
digitalWrite(5, HIGH);
delay(100);
digitalWrite(5, LOW);
delay(100);
}
void ledPrompt()
//prompts player for inout during game after
//the LEDs have been picked randomly each round
{ digitalWrite(x, HIGH);
delay(1000);
digitalWrite(x, LOW);
delay(50);
}
void gameHasStarted()
//LED prompt after first button
//is pushed to start the game
{
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(100);
}
void fail() //LED sequence when the wrong input is entered
{ digitalWrite(2, HIGH);
delay(1000);
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(50);
digitalWrite(3, LOW);
delay(50);
digitalWrite(4, LOW);
delay(50);
digitalWrite(5, LOW);
delay(50);
}
void waitToStart() //cycles through LEDs while waiting to start the game
{ digitalWrite(2, HIGH);
delay(250);
digitalWrite(2, LOW);
delay(250);
digitalWrite(3, HIGH);
delay(250);
digitalWrite(3, LOW);
delay(250);
digitalWrite(4, HIGH);
delay(250);
digitalWrite(4, LOW);
delay(250);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(5, LOW);
delay(250);
}
int initiate()//FUNCTION WAITS FOR ANY OF 4 BUTTONS TO BE PUSHED TO START THE GAME
{ game = 0;
waiting = 0;
loopCounter = 1;
randNumber = 0;
ledCounter = 0;
inputCounter = 0;
pass = 2;
//FUNCTION INCLUDES ALL OF THE ASSUMPTIONS NEEDED TO EXECUTE THE 1ST ROUND PROPERLY
do {
if (digitalRead(buttonRed) == LOW)
{ game = 2;
gameHasStarted();
}
if (digitalRead(buttonGreen) == LOW)
{ game = 2;
gameHasStarted();
}
if (digitalRead(buttonBlue) == LOW)
{ game = 2;
gameHasStarted();
}
if (digitalRead(buttonYellow) == LOW)
{ game = 2;
gameHasStarted();
}
else {
waitToStart();
}
}
while (game <= 1);
delay(1000);
}//END OF GAME INITIATOR FUNCTION
int pickLed ()//FUNCTION WILL PICK A RANDOM LED
{
do {
randNumber = random(2, 6);
digitalWrite(randNumber, HIGH);
delay(500);
digitalWrite(randNumber, LOW);
delay(100);
if (randNumber == 2)
{ ledArray[loopCounter] = 0;
ledCounter++;
}
if (randNumber == 3)
{ ledArray[loopCounter] = 1;
ledCounter++;
}
if (randNumber == 4)
{ ledArray[loopCounter] = 2;
ledCounter++;
}
if (randNumber == 5)
{ ledArray[loopCounter] = 3;
ledCounter++;
}
}
while (ledCounter < loopCounter);
ledPrompt();//signals for input after it's done picking the LEDs for each round
wait == 1;
}//END OF PICKLED FUNCTION
int waitForInput ()//FUNCTION WILL WAIT FOR USER INPUT OR END GAME AFTER 10 SECONS OF NO INPUT
{
do
{
if (digitalRead(buttonRed) == LOW)
{ digitalWrite(r, HIGH);
inputCounter++;
delay(150);
digitalWrite(r, LOW);
delay(150);
buttonArray[loopCounter] = 0;
}
if (digitalRead(buttonYellow) == LOW)
{ digitalWrite(y, HIGH);
inputCounter++;
delay(150);
digitalWrite(y, LOW);
delay(150);
buttonArray[loopCounter] = 1;
}
if (digitalRead(buttonBlue) == LOW)
{ digitalWrite(b, HIGH);
inputCounter++;
delay(150);
digitalWrite(b, LOW);
delay(150);
buttonArray[loopCounter] = 2;
}
if (digitalRead(buttonGreen) == LOW)
{ digitalWrite(g, HIGH);
inputCounter++;
delay(150);
digitalWrite(g, LOW);
delay(150);
buttonArray[loopCounter] = 3;
}
else {
wait++; //the wait counter is incremented each time the loop reads all 4 input and no input has been made
delay(250);
}//the delay here adds 1/4th of a second each time no input is made (every 4 loop here is 1 second);
if (wait > 40) //40 divided by 0.25 = 10 seconds of wait
{
pass = 0;
fail();
} //after waiting 10 seconds for input the game automatically fails.
}
while (inputCounter < loopCounter);//WAIT FOR INPUT
} //END OF WAIT_FOR_INPUT FUNCTION
int scoreInput ()//START OF SCORE_INPUT FUNCTION
{ if (memcmp(ledArray, buttonArray, sizeof(ledArray)) == 0)
{
delay(1000);
loopCounter++;
ledCounter = 0;
inputCounter = 0;
game++;
pass = 2;
}
else
{
loopCounter = 0;
ledCounter = 0;
inputCounter = 0;
pass = 0;
}
}
//END OF SCORE_INPUT FUNCTION
void setup ()
{ randomSeed(analogRead(0));
pinMode (r, OUTPUT);
pinMode (y, OUTPUT);
pinMode (b, OUTPUT);
pinMode (g, OUTPUT);
pinMode (buttonRed, INPUT_PULLUP);
pinMode (buttonYellow, INPUT_PULLUP);
pinMode (buttonGreen, INPUT_PULLUP);
pinMode (buttonBlue, INPUT_PULLUP);
pinMode (buttonRed, INPUT);
pinMode (buttonYellow, INPUT);
pinMode (buttonGreen, INPUT);
pinMode (buttonBlue, INPUT);
pinMode (x, OUTPUT);
game = 0;
waiting = 0;
loopCounter = 1;
randNumber = 0;
ledCounter = 0;
inputCounter = 0;
pass = 2;
timeUp = 2;
initiate();
}
void loop ()//by using functions, the void loop section here is very short.
{
do {
pickLed();
waitForInput();
scoreInput();
if (loopCounter > 4)//states the game should end after round 10
{ youWin();
pass = 0;
}
}
while (pass > 1); // keeps the game going until round 10 or fail
initiate();
}
I did already. I did it that way because of something else I got from this same forum. But yeah I've fixed it already.
I don't want to have to completely start over. I understand what arrays do but maybe I don't understand how they work. If I create an array to store whatever button is lit( which I can do very easily, how would I determine if the right button has been pressed (not to be confused for the right amount of button). My issue is that the LEDs will be randomized because they're an output, but the input (buttons) clearly won't be random.