Go Down

Topic: Timer & Exiting Loop & Randomization (Read 291 times) previous topic - next topic

premobowei

??? I know no range limitation is needed but it can only choose a number between 2 and 4. No limitiation is needed for the code but for what I'm trying to do it is needed. But already moved past that.
What I need to figure out now is a way to control the time it waits for input.

premobowei

So, I've successfully achieved everything I needed to and after about a week of work, I've created my Simon says with all of the functions I was looking for.


The only thing i didn't achieve was true randomization but I'll take that loss.
Also, I'm not sure about my buttons but they work and I don't seem to have any floating going on so that's great. I've also attached my wiring and setup which will copy onto the actual game (the extra button will be taken off). .


THANK YOU SO SO SO MUCH TO EVERYONE WHO GUIDED ME THROUGH THIS. I ACTUALLY NOW HAVE SOME RESPECT AND UNDERSTANDING OF CODING. THIS WAS GREAT!!!


This is the final product.

Code: [Select]
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;
int waiting;

             
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 initiate(){

game=0;
waiting=0;
loopCounter=1;
randLedNumber=0;
ledCounter=0;
game=0;
   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 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;
  randLedNumber=0;
ledCounter=0;
 initiate();
}
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(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 {waiting++;
           Serial.print(waiting);
           delay(500);
           }
      if (waiting>=20)
      {initiate();}
 }
 while (inputCounter < loopCounter);//WAIT FOR INPUT
 
    if (memcmp(ledArray, buttonArray, sizeof(ledArray)) == 0)
    {
      Serial.print("RIGHT");
      delay(1000);
      loopCounter++;
      ledCounter=0;
      randLedNumber=0;
      inputCounter=0;
      game++;
     Serial.print(loopCounter);}
     
     
    else
    {
     Serial.print("WRONG");
     blinkOrange();
      game=0;
      initiate();
    }
  if (loopCounter > 9)
  {
  Serial.print("YOU WIN, I GIVE UP");
  delay(1000);
  initiate();
  }
  }

vaj4088

The example shows how to achieve randomization.  The example shows how to get random integers in a range but you already know how to do that.

One line in the setup() function would get you the desired randomization but you have to insert the line yourself.


pert

I haven't looked at the other thread, but I think the best thing to do would be to add a note to that thread to explain that you've started a new one and provide a link to this one. That should help to avoid wasting time due to having parallel discussions between the two threads.
I asked you nicely to do something and explained why I wanted you to do it. So why didn't you?

pert

You definitely want to use randomSeed() I don't understand why you wouldn't do that after it was explained so clearly to you.

For this purpose, the analog reading as a seed to the PRNG should be fine (assuming the pin is floating), but that is really not an ideal solution when you want good randomization. When it matters, and when you don't have TRNG hardware available, the best solution is to use timer jitter, as is done in the Entropy library:
https://sites.google.com/site/astudyofentropy/

premobowei

#20
Dec 09, 2019, 06:21 am Last Edit: Dec 09, 2019, 06:37 am by premobowei Reason: forgot to add code
Got the randomization to work with randomSeed. Thanks guys...

Now it's just being inconsistent. I fix one thing and another breaks. Can someone take a look at the code.
Does the array look correct? I set the button to 10 but the LED to 3 (0-3) because there are only 4 LEDs to choose from? Is this correct? Maybe that's why it's having issues keeping up.


Code: [Select]
int ledArray[3];
int buttonArray[10];
int loopCounter;
int ledCounter;
int inputCounter;
int game;
int waiting;
int pass;
             
void ledPrompt()//waiting for input
{digitalWrite(10, HIGH);
  delay(1000);
  digitalWrite(10, LOW);
 delay(50);}


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() //cycle 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);}

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;
int randNumber;
int array_Cmp;

void gameHasStarted ()
{digitalWrite(10, HIGH);
  delay(1000);
 digitalWrite(10, LOW);
  delay(100);}
 
void test(){
digitalWrite(y, HIGH);
delay(3000);
digitalWrite(y,LOW);
delay(100);}

int initiate()//FUNCTION WAITS FOR ANY OF 4 BUTTONS TO BE PUSHED TO START THE GAME
{
//FUNCTION INCLUDES ALL OF THE ASSUMPTIONS NEEDED TO EXECUTE THE 1ST ROUND PROPERLY
game=0;
waiting=0;
loopCounter=1;
randNumber=0;
ledCounter=0;
inputCounter=0;
 pass=1;
   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);
}

int pickLed ()//FUNCTION WILL PICK A RANDOM LED
 {
   do{
  randNumber=random(2,6);
  digitalWrite(randNumber, HIGH);
  delay(250);
  digitalWrite(randNumber, LOW);
  delay(250);
  if (randNumber==2)
   {ledArray[loopCounter]=0;
    ledCounter++;
  delay(500);}
   if(randNumber==3)
   {ledArray[loopCounter]=1;
    ledCounter++;
  delay(500);}
   if(randNumber==4)
   {ledArray[loopCounter]=2;
  ledCounter++;
  delay(500);}
   if(randNumber==5)
  {ledArray[loopCounter]=3;
  ledCounter++;
  delay(500);}
   }
  while (ledCounter < loopCounter);
  ledPrompt();//signals for input after it's done picking the LEDs for each round
   }

 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 {waiting++;
           delay(500);}
      if (waiting>=20)
      {pass=0;}
 }
 while ((((inputCounter < loopCounter))) || (((pass >= 1))));//WAIT FOR INPUT
 }   //END OF WAIT_FOR_INPUT FUNCTION

int scoreInput ()//START OF SCORE_INPUT FUNCTION
{if (memcmp(ledArray, buttonArray, sizeof(ledArray)) == 0)
    {//some values are ++ here to make sure the increments occur each round without values being carried over from previous round)
      loopCounter++;
      ledCounter=0;
      randNumber=0;
      inputCounter=0;
      pass++;
    }
     
    else
    {loopCounter=0;
      fail();
      ledCounter=0;
      randNumber=0;
      inputCounter=0;
      initiate();
      pass=0;
    }
}
//END OF SCORE_INPUT FUNCTION

void setup ()
{randomSeed(analogRead(0));
 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 (x, OUTPUT);
}
void loop ()//by using functions, the void loop section here is very short.
{initiate();
do{
pickLed();
delay(100);
waitForInput();
delay(250);
scoreInput();
if (loopCounter = 10)//states the game should end after round 10
{pass=0;}
delay(200);
}
while(pass>=1); // keeps the game going until round 10
delay(1000);
}


premobowei

You definitely want to use randomSeed() I don't understand why you wouldn't do that after it was explained so clearly to you.

For this purpose, the analog reading as a seed to the PRNG should be fine (assuming the pin is floating), but that is really not an ideal solution when you want good randomization. When it matters, and when you don't have TRNG hardware available, the best solution is to use timer jitter, as is done in the Entropy library:
https://sites.google.com/site/astudyofentropy/
Thanks. Will do.

premobowei

Anything? Now it's just being ridiculous. It waits for input after giving the 1st LED but fails the game without even scoring it!!!

premobowei

Fixed that last issue. Now the issue is when the game fails and its supposed to start over. It does all of the theatrics of starting over but then when I go to enter the input for the first round, it fails it automatically and starts over again....
Code: [Select]
int ledArray[55];
int buttonArray[100];
int loopCounter;
int ledCounter;
int inputCounter;
int game;
int waiting;
int pass;
             
void ledPrompt()//waiting for input
{digitalWrite(10, HIGH);
  delay(1000);
  digitalWrite(10, LOW);
 delay(50);}


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() //cycle 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);}

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;
int randNumber;
int array_Cmp;

void gameHasStarted ()
{digitalWrite(10, HIGH);
  delay(1000);
 digitalWrite(10, LOW);
  delay(100);}
 
void test(){
digitalWrite(y, HIGH);
delay(3000);
digitalWrite(y,LOW);
delay(100);}

void test2() {
digitalWrite(b, HIGH);
delay(3000);
digitalWrite(b,LOW);
delay(100);}

int initiate()//FUNCTION WAITS FOR ANY OF 4 BUTTONS TO BE PUSHED TO START THE GAME
{
//FUNCTION INCLUDES ALL OF THE ASSUMPTIONS NEEDED TO EXECUTE THE 1ST ROUND PROPERLY
game=0;
waiting=0;
loopCounter=1;
randNumber=0;
ledCounter=0;
inputCounter=0;
pass=2;
   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);
}

int pickLed ()//FUNCTION WILL PICK A RANDOM LED
 {
   do{
  randNumber=random(2,6);
  digitalWrite(randNumber, HIGH);
  delay(250);
  digitalWrite(randNumber, LOW);
  delay(250);
  if (randNumber==2)
   {ledArray[loopCounter]=0;
    ledCounter++;
  delay(500);}
   if(randNumber==3)
   {ledArray[loopCounter]=1;
    ledCounter++;
  delay(500);}
   if(randNumber==4)
   {ledArray[loopCounter]=2;
  ledCounter++;
  delay(500);}
   if(randNumber==5)
  {ledArray[loopCounter]=3;
  ledCounter++;
  delay(500);}
   }
  while (ledCounter < loopCounter);
  ledPrompt();//signals for input after it's done picking the LEDs for each round
   }

 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;}
 }
 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;
      waitToStart();
      pass=0;
    test2();}
}
//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);
 pinMode (buttonYellow, INPUT);
 pinMode (buttonGreen, INPUT);
 pinMode (buttonBlue, INPUT);
 pinMode (x, OUTPUT);
}
void loop ()//by using functions, the void loop section here is very short.
{initiate();
test2();
do{
pickLed();
delay(100);
waitForInput();
scoreInput();
if (loopCounter > 10)//states the game should end after round 10
{pass=0;}
}
while(pass>1); // keeps the game going until round 10
test();
}


Go Up