Please help me explain the logic of converting this?

Please help me explain how to convert this code segment from do-while to for.

for (randNumber==2; ledArray[loopCounter]=0; ++)

   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);

I'm in tears for not understanding this. Wish I was kidding.

Here's a good tutorial on C++ loop types. Start here: C++ Loop Types
There are separate links that compare and contrast the 3 main types: "for", "while" "do-while".

int level[]={0,1,2,3,4,5,6,7,8,9};
  for (level=0; level < 9; level++)
  {level=random(2,6);}

I tried that and got::

In function 'void loop()':
246:6: error: incompatible types in assignment of 'int' to 'int [10]'
248:13: error: incompatible types in assignment of 'int' to 'int [10]'
248:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
248:33: error: lvalue required as increment operand
249:9: error: incompatible types in assignment of 'long int' to 'int [10]'

tried in both setup and loop

But thanks @gfvalvo, that link helped.

First, take your code and press the Ctrl and T keys at the same time. That will reformat your code and make it easier to read. Next, I don't see where you set an initial value for loopCounter, but I assume it is 0. A for loop consists to 3 expressions:

for (epression1; expression2; expression3) {
// statements controlled by the for loop
}

Expression1 is usually an initialization statement. My guess is that you want to start ledCounter out at 0, so you might write:

for (int ledCounter = 0; expression2; expression3) {
// for loop statement body--statements controlled by the for loop
}

Expression1 is only executed once--the first time the for loop code starts to execute.

Expression2 is usually a logical test to determine if we need to make another pass through the statements controlled by the for loop. It appears that this should be the same expression that we see at the bottom of the do-while:

for (int ledCounter = 0; ledCounter < loopCounter; expression3) {
// statements controlled by the for loop
}

Somewhere in the code you did not post you probably set loopCounter to a value. (You should always post all your code so we don't have to guess.) I have no idea what the value should be , but lets set it to 6
and increment it each time after we turn on an LED.

Expression3 is usually used to change a variable that controls the number of passes made through the loop. So we will just increment it:

for (int ledCounter = 0; ledCounter < loopCounter; loopCounter++) {
// statements controlled by the for loop
}

Now look at the code:

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++;}
 }

This looks very repetitive. Also, we know the random number is always going to be between 2 and 5 inclusively. Could we simplify all the statements above to:

    ledArray[loopCounter] = randNumber - 2;
    ledCounter++;

Walk through each possible value 2 through 5 and see if it doesn't yield the same results. Now we have something like:

  for (int ledCounter = 0; ledCounter < loopCounter; expression3) {
     randNumber = random(2, 6);
     digitalWrite(randNumber, HIGH);
     delay(500);
     digitalWrite(randNumber, LOW);
     delay(100);
     ledArray[loopCounter] = randNumber - 2;
     ledCounter++;
  }

Since I can't see all of your code, you'll have to supply the missing definitions of the values for the variables and the arrays.

Edit: WHen control reaches the closing brace at the bottom of the for loop statement body, control is sent to expression3. Upon processing expression3, control is immediately transferred to expression2 to see if we need to go through the loop again. Note expression1 is not visited again.

Thanks. Here's my disaster of a code. Let me tinker around with the information you just provided.

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

@Econjack....the ledcounter was just to control how many leds it lights up per round. That's not the array.

What I would really like to do is to first populate the 0-9 arrays with any random number between 2 and 6.
Then I could maybe look into using switch cases or even just do while or if for the game by telling it to light up each corresponding LED with each array. So like level one would be array [0], then level two would be arrays [0] and [1], then 0,1,2 and so forth. then I'll look into the input and scoring portion.

It's not very common to modify the index of a 'for' loop in the loop's body. Typically, it's only done by the "increment" part of the 'for' statement.

It's even less common for the index of a 'for' loop to be an array.

int popLate()
{
  led[0]=random(2,6);
  led[1]=random(2,6);
  led[2]=random(2,6);
  led[3]=random(2,6);
  led[4]=random(2,6);
  led[5]=random(2,6);
  led[6]=random(2,6);
  led[7]=random(2,6);
  led[8]=random(2,6);
  led[9]=random(2,6);
}

I tried this^^^
then tested with this

void loop ()
{int popLate();
 digitalWrite(led[3], HIGH);
 delay(5000);
 digitalWrite(led[3], LOW);
 delay(1000);
}

but nothing is happening?

What is this function prototype doing inside the loop function?

int popLate();

Do you know what pin number would be contained in array element led[3], and if that pin is an output, and if that output is connected to an LED?

 digitalWrite(led[3], HIGH);

@Jremington, yes all the pins in the array are randomized between 2 and 6 which are all output LED pins.

One down, one to go.

SO, I have this working up until it's time to score the input (compare the button pushed to the LED lit).

void loop ()
{do{
 digitalWrite(led[0], HIGH);
 delay(2000);
 digitalWrite(led[0], LOW);
 delay(1000);
  ledCounter++;
}
 while (ledCounter<1);
 waitForInput();
 scoreInput();
}

with this

int popLate()
{
  led[0]=random(2,6);
  led[1]=random(2,6);
  led[2]=random(2,6);
  led[3]=random(2,6);
  led[4]=random(2,6);
  led[5]=random(2,6);
  led[6]=random(2,6);
  led[7]=random(2,6);
  led[8]=random(2,6);
  led[9]=random(2,6);
}

this is the code I use to compare the inputs. It worked for the way I had the game last week when it was generating random numbers for all levels instead of using the same sequence but incrementing by new number each round. ANd it worked back then but not working now.

{if (memcmp(led, buttonArray, sizeof(led)) == 0)

jremington:
What is this function prototype doing inside the loop function?

int popLate();

Do you know what pin number would be contained in array element led[3], and if that pin is an output, and if that output is connected to an LED?

 digitalWrite(led[3], HIGH);

I've placed it in the setup. Caught it earlier. thanks

Any ideas on what command or rule I can use to comapre the input to whatever LED pops? Because the LED is random I can't really use numbers because the numbers are random on the output. Any ideas?

I've placed it in the setup.

It doesn't go there, either.

It works there. It's in the setup as a function. I created a function in the main

Since this portion works, the new issue is figuring out how to compare the led to the button. the LEDs are random.

int buttonMatcher(){
  if (led[0]==2)
   {ledArray[loopCounter]=0;
    ledCounter++;}
   if(led[0]==3)
   {ledArray[loopCounter]=1;
    ledCounter++;}
   if(led[0]==4)
   {ledArray[loopCounter]=2;
  ledCounter++;}
   if(led[0]==5)
  {ledArray[loopCounter]=3;
  ledCounter++;}
}

I got the comparison to work by introducing this function and using it after the segment before input.
However, this would mean I would have to create 10 different ones while sapping the led[0] accordingly. Anyone know a shorter way, please?

@premobowei,

Every time I look at the code in one of your posts it makes my brain bleed. This is for a variety of reasons, one of which is that your formatting and indentation are atrocious. There are two commonly-accepted ways to handle placement of the curly brackets ('{' and '}'). You are using NEITHER of them. And, the Arduino IDE will provide proper indentation for you with a simple 'ctrl-t' key press. So please take a look at the two copies of your previous code that I've formatted below and use one of them consistently in your future posts.

The reason for this convention goes far beyond making the code "pretty". It really helps people understand and debug the code. Making it difficult for people that are trying to help you -- FOR FREE -- is not a winning strategy. This is not the first time people have made this request of you. Why have you refused to do it?

int buttonMatcher()
{
  if (led[0] == 2)
  {
    ledArray[loopCounter] = 0;
    ledCounter++;
  }
  if (led[0] == 3)
  {
    ledArray[loopCounter] = 1;
    ledCounter++;
  }
  if (led[0] == 4)
  {
    ledArray[loopCounter] = 2;
    ledCounter++;
  }
  if (led[0] == 5)
  {
    ledArray[loopCounter] = 3;
    ledCounter++;
  }
}
int buttonMatcher() {
  if (led[0] == 2) {
    ledArray[loopCounter] = 0;
    ledCounter++;
  }
  if (led[0] == 3) {
    ledArray[loopCounter] = 1;
    ledCounter++;
  }
  if (led[0] == 4) {
    ledArray[loopCounter] = 2;
    ledCounter++;
  }
  if (led[0] == 5) {
    ledArray[loopCounter] = 3;
    ledCounter++;
  }
}

Amaizing how you keep posting the same, defunct code with a new question again and again without minding the help you get :expressionless:

https://forum.arduino.cc/index.php?topic=653497.msg4404431#msg4404431
https://forum.arduino.cc/index.php?topic=653445.0
https://forum.arduino.cc/index.php?topic=652941.msg4400847#msg4400847
https://forum.arduino.cc/index.php?topic=652831.0
https://forum.arduino.cc/index.php?topic=652399.msg4400180#msg4400180

In other topics you have only posted snippets from it.