int contGame;
int ledArray[3];
int buttonArray[10];
int loopCounter;
int ledCounter;
int inputCounter;
int amountMatch;
int buttonState=0;
int lastButtonState=0;
int amoutOutput;
int game;
void ledPrompt()//waiting for input
{digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(50);}
void blinkGreen()
{digitalWrite(5, HIGH);
delay(250);
digitalWrite(5, LOW);
delay(250);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(5, LOW);
delay(500);}
void blinkOrange()
{digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(500);}
void waitToStart() //cycle through LEDs while awaiting 1st input
{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);}
const int orangeLed = 13;
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 gameStart = 12;
const int x = 10;
int count = 0;
int randLedNumber=random(2,5);
int array_Cmp;
void gameHasStarted ()
{digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);}
void setup ()
{
Serial.begin(9600);
pinMode (r, OUTPUT);
pinMode (y, OUTPUT);
pinMode (b, OUTPUT);
pinMode (g, OUTPUT);
pinMode (buttonRed, INPUT);
pinMode (buttonYellow, INPUT);
pinMode (buttonGreen, INPUT);
pinMode (buttonBlue, INPUT);
pinMode (gameStart, INPUT);
pinMode (x, OUTPUT);
loopCounter=1;
ledCounter=0;
game;
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);
}
void loop (){
do{
randLedNumber=random(2,6);
digitalWrite(randLedNumber, HIGH);
delay(250);
digitalWrite(randLedNumber, LOW);
delay(250);
if (randLedNumber==2)
{ledArray[loopCounter]=0;
ledCounter++;
delay(500);}
if(randLedNumber==3)
{ledArray[loopCounter]=1;
ledCounter++;
delay(500);}
if(randLedNumber==4)
{ledArray[loopCounter]=2;
ledCounter++;
delay(500);}
if(randLedNumber==5)
{ledArray[loopCounter]=3;
ledCounter++;
delay(500);}
}
while (ledCounter < loopCounter);
ledPrompt();
//(buttons)::G(9), Y(7), R(6), B(8)
//(LEDs):: G(5), Y(3), R(2), B(4)
do
{
if (digitalRead(buttonRed)==LOW)
{digitalWrite(r, HIGH);
inputCounter++;
delay(100);
digitalWrite(r,LOW);
delay(100);
buttonArray[loopCounter]=0;
delay(100);}
if (digitalRead(buttonYellow)==LOW)
{digitalWrite(y, HIGH);
inputCounter++;
delay(100);
digitalWrite(y,LOW);
delay(100);
buttonArray[loopCounter]=1;
delay(100);}
if (digitalRead(buttonBlue)==LOW)
{digitalWrite(b, HIGH);
inputCounter++;
delay(100);
digitalWrite(b,LOW);
delay(100);
buttonArray[loopCounter]=2;
delay(100);}
if (digitalRead(buttonGreen)==LOW)
{digitalWrite(g, HIGH);
inputCounter++;
delay(100);
digitalWrite(g, LOW);
delay(100);
buttonArray[loopCounter]=3;}
}
while (inputCounter < loopCounter);
delay(1000);
if (memcmp(ledArray, buttonArray, sizeof(ledArray)) == 0)
{
Serial.print("RIGHT");
delay(1000);
loopCounter++;
ledCounter=0;
inputCounter=0;
game++;}
else
{
Serial.print("WRONG");
blinkOrange();
delay(5000);
loopCounter=0;
ledCounter=0;
inputCounter=0;
waitToStart();
}
Serial.print(loopCounter);
}
HI! I really want to extend sincere gratitude to those who have helped with tips and pointers through this project since I started it. I've now gotten things to a pretty great place and I'm so relieved.
This is a Simon Says game.
I need to add 3 minor components to the project then it'll be perfect.
- When the board is on, it'll display the LEDs in a cycle until any of the 4 buttons are pressed, then it'll start the game. It's supposed to choose the first LED to light up and this is supposed to be random but my issue is it keeps setting up the same LEDs through all of the rounds. So it's very easy to learn the cycle. How do I achieve true randomization?
- I want to be able to put a time limit on how long to wait for input. I was hoping to wait a certain length of time for the first input and then expand the time with each input but I have no clue how to go about this.
- Lastly, I need to be able to end the game at the 10th round and run the code again to have it go through Void Setup and then back to void loop but haven't found anything on the internet regarding this. I also need to do this for when the match is wrong.
I made this new thread because the other has gotten pretty far and I feel this is a good place to start a new one. Mods are welcome to lock the other one though I wouldn't delete it because perhaps others may learn from it. Thank you!!