How to clear array?

@UKHeliBob, I know this is an old topic but could you help explain this? I want to completely clear whatever information is stored in my arrays after someone fails the game. How do I do this?

int ledArray[55];
int buttonArray[100];
int loopCounter;
int ledCounter;
int inputCounter;
int game;
int waiting;
int pass;
             
void ledPrompt()//prompts player for inout during game after the LEDs have been picked randomly each round
{digitalWrite(10, HIGH);
  delay(1000);
  digitalWrite(10, 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() //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;


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


void loop ()//by using functions, the void loop section here is very short. 
{initiate();
do{
pickLed();
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 or fail
}

Why not simply do a loop and clear each element of the array?

while (i=0,i<=55,i++){
 ledArray[i]=0
}

Split from a very old topic

What do you want to put in the arrays when they are "cleared" ?
You could do it with a for loop and save whatever you want in the array

More likely you just set the array index variable back to zero and start writing to the array again on the next run of the game. You will know know how many entries have been made in the new game because you will be keeping count using the array index variable.

You could chose to set the array index back to zero then write a "flag" value such as -1 to the next array position after writing a value to the current position. You could then use this value to determine when to stop reading the values from the array when you need to. This is commonly done when writing to char arrays to produce a C style string where a zero marks the end of the string and is used as the "flag" value

@Steve, I have an issue where the game works fine until it fails and then it's supposed to break out of the do while and go through the void loop again which would mean clearing a lot of the counters and values, but for some reason, after it fails, it becomes like a zombie.

Where would I put that code you provided?

Write a function, maybe named tidyUp(), and put all the code in there that tidies up by whatever means you choose to use. Call the function whenever you need to tidy up

UKHeliBob:
Write a function, maybe named tidyUp(), and put all the code in there that tidies up by whatever means you choose to use. Call the function whenever you need to tidy up

Yeah, this is what the int initiate() is meant to do and I think it works because it works in the first round and my code doesn't go back to it until the player fails the game. When the player fails is where the issue starts because as you can see I have the initiate function at the start of the void loop which means once the player fails, the code will break from the do-while and go back into the void loop and it should initiate again. But I just don't know why it messes up afterwards.

premobowei:
@Steve, I have an issue where the game works fine until it fails and then it's supposed to break out of the do while and go through the void loop again which would mean clearing a lot of the counters and values, but for some reason, after it fails, it becomes like a zombie.

Where would I put that code you provided?

Probably in the initiate() function.

I don't see where you are testing for a ten second timeout, but that's going to be difficult to do with all of the delay statements in you code.

Have you put Serial.print statements in the functions to determine where the program goes Zombie?

To answer the question in the title, you can use memset to clear the arrays. You can place the calls to it in initiate().

premobowei:
But I just don't know why it messes up afterwards.

We have no idea what that means; you should give a proper description of what fails / how it fails.

SteveMann:
Probably in the initiate() function.

I don't see where you are testing for a ten second timeout, but that's going to be difficult to do with all of the delay statements in you code.

Have you put Serial.print statements in the functions to determine where the program goes Zombie?

@Steve, the timer I made while using the tinkercard simulator worked for the simulation but in the actual Arduino it didn't work so I took it off. I don't have delays in my waiting for input segment,. The delays are only for the LED when the buttons are pushed (if), outside of that there are no delays.

@Sterretje, after the player puts the wrong inputs, it goes through the LED sequences and does the thetrics to show it's startig the game over, and it starts it over and even gives the first round's LED, but the problem is it scores it wrong even when it is right (and it plays the LED sequence for when someone fails). So something goes wrong with the scoring after the game restarts.

This is the function that waits for input each round after LEDs have been designated. I put a green LED test there to determine if it has met the condition of the timeout. I put a wait++ at the end of the reading button sequence to make it increase the wait timer everytime it reads the buttons and none have been pushed. When I run it, it starts blinking the test light right away like it's not even waiting??? Is it because I don't have any delays between the time it reads each button? I don't want to put a delay there because it might interfere with capturing inputs?

 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++;}
     if (wait>5000)
     {test();
     fail();
      loopCounter=0;
      ledCounter=0;
      inputCounter=0;
      pass=0;
      }
 }
 while (inputCounter < loopCounter);//WAIT FOR INPUT
 }
 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++;
     delay(250);}
     
     if (wait>40)
     {test();
      loopCounter=0;
      ledCounter=0;
      inputCounter=0;
      pass=0;
      }
 }

YAY! Got the timeout to work with this.

Only thing left is to figure out the bug with after the player fails.

Using an LED as a test, I've detected that the Do While only works at the last segment of comparing the inputs. It doesn't work in the waiting for input section so when the time limit is passed, it doesn't break out of the do while even though I've included the criteria needed to break the do while inside the time limit function. Any ideas?

SteveMann:
Why not simply do a loop and clear each element of the array?

while (i=0,i<=55,i++){

ledArray[i]=0
}

:o

TheMemberFormerlyKnownAsAWOL:
:o

I'm no longer looking at that. I'm now trying to figure out why it bugs out after someone loses the game and it restarts.

Probably because your code assumes some variable (maybe the arrays!) is in the initial state, but the variable in question doesn't get restored to the initial state. Either examine your code carefully to see which variable that might be, or just reset every variable used to record any aspect of game state in your initialize() function.

Why not use:

   memset(ledArray, 0, sizeof(ledArray));

for the arrays of interest?

yep, I use memset a lot to clear arrays quickly.
significantly faster than looping.

The code @Steve provided could work but I don't get how to use it becuase it's a while code? I don't know how much of the array would've been used up any given time the game fails so it's hard to manage a condition that would clear the arrays.

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;
ledArray[55]=0;
buttonArray[100]=0;
//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); 
}

I tried just flatout entering the 0 for the arrays but didn't make a difference .

premobowei:
The code @Steve provided could work

No, it couldn't, for multiple reasons

What do you think this does?

ledArray[55]=0;
buttonArray[100]=0;

Other than breaking your gizmo, that is..