Failure to generate random array values

Hello,

This is my first post, so sorry in advance if I fail to follow the conventions.

I’ve been working on a little game that blinks a sequence of colored lights which the player must play back in the correct order, and I’m trying to generate the sequence for each run through randomly in an array.

The code to randomly generate the sequence and subsequently print that sequence is not editing the array, leaving all values 0. This functioning of this portion of the code is not dependent on my circuit in any way, so long as I don’t have anything attached to A0, which I don’t. This makes me think my problem must be with the software.

I took this same code and tested it without the other parts of the project, i.e. just the one array and the two for loops to generate and print the random sequence, in a separate sketch and the code works just fine, generating random sequences. I can’t see any practical difference between what I did in my main sketch and the test sketch.

I also tested a different board with the same result.

Attached is my main code. The portion that is giving me problems is the part under the comment, Generate and print sequence. My intention, to be clear, is to generate an array with seven values ranging from 1 to 5, and repeating values are allowed. I’d appreciate any pointers you could give me. If it would help to post the test sketch as well, I can do that.

byte ledB = 9, ledG = 10, ledR = 11;
byte buttonB = 2, buttonG = 3, buttonY = 4, buttonR = 5, buttonP = 6;
byte buzzer = 8;
int wait = 1500;
int between = 500;
byte powerG = 50; //ideal power of green for Yellow
byte fullPower = 255;
byte noPower = 0;
byte bPress = 1, gPress = 2, yPress = 3, rPress = 4, pPress = 5;
byte input[7];
byte pattern[7] = {0, 0, 0, 0, 0, 0, 0};
byte number;

//Frequencies
byte soundB = 100;
byte soundG = 200;
int soundY = 300;
int soundR = 400;
int soundP = 500;
int win = 750;
int lose = 50;

void setup() {
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(buttonB, INPUT_PULLUP);
  pinMode(buttonG, INPUT_PULLUP);
  pinMode(buttonY, INPUT_PULLUP);
  pinMode(buttonR, INPUT_PULLUP);
  pinMode(buttonP, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop()
{
  //Reset input password**********************************************************************
  for(byte place = 0; place < 7; place++)
  {
    input[place] = 0;
  }
  //Wait for start button press ***************************************************************
  Serial.println("Standby");
  bool start = digitalRead(buttonY);
  while (start == 1)
  {
    delay(1000);
    start = digitalRead(buttonY);
  }
  //Generate and print sequence*****************************************************************
  byte index = 0;
  for (index; index < 7; index++);
  {
    pattern[index] = random(1, 6);
  }
  for (byte printer = 0; printer < 7; printer++)
  {
    Serial.println(pattern[printer]);
  }
  //Play sequence*********************************************************************************
  Serial.println("Begin");
  for (byte cycle = 0; cycle < 7; cycle++)
  {
    if(pattern[cycle] == bPress)
    {
      ledOn(ledB, soundB);//Blue
    }
    else if(pattern[cycle] == gPress)
    {
      ledOn(ledG, soundG);//Green
    }
    else if(pattern[cycle] == yPress)
    {
      ledMix(ledG,ledR, powerG, soundY);//Yellow
    }
    else if(pattern[cycle] == rPress)
    {
      ledOn(ledR, soundR); //Red
    }
    else if(pattern[cycle] == pPress)
    {
       ledMix(ledB, ledR, fullPower, soundP);//Purple
    }
  }
  //Take input*****************************************************************************
  number = 0;
  byte count = 0;
  byte maxCount = 100;
  for (count; count<=maxCount; count++)
  {
    enter(buttonB, bPress, ledB, ledR, noPower, soundB);
    enter(buttonG, gPress, ledG, ledR, noPower, soundG);
    enter(buttonY, yPress, ledR, ledG, powerG, soundY);
    enter(buttonR, rPress, ledR, ledB, noPower, soundR);
    enter(buttonP, pPress, ledB, ledR, fullPower, soundP);
    
    if (number == 7)
    {
    count = 101;
    }
  delay(500);
  Serial.println(number);
  Serial.println(input[number-1]);
  //Serial.println(count);
  }
  //Test input ***********************************************************************************
  inputCheck(pattern);
}

void enter(byte button, byte buttonPress, byte led, byte mix, byte power, int sound)
{
  bool check = digitalRead(button);
  if (check == 0)
  {
    Serial.println("Press");
    input[number] = buttonPress;
    number++;
    digitalWrite(led, HIGH);
    analogWrite(mix, power);
    tone(buzzer, sound);
    while (check == 0)
    {
      delay(200);
      check = digitalRead(button);
    }
    noTone(buzzer);
    digitalWrite(led, LOW);
    analogWrite(mix,0);
  }
}

void inputCheck(byte sequence[7])
{
  int win = 750;
  int lose = 50;
  byte key = 0;
  for (byte counter = 0; counter < 7; counter++)
  {
  if (sequence[counter] == input[counter])
  {
    Serial.println("Good");
    key += 1;
    Serial.println(key);
  }
  else if (sequence[counter] != input[counter])
  {
  Serial.println("Bad");
  counter = 7;
  }
  }
  if (key == 7)
  {
    Serial.println("Correct");
    digitalWrite(ledG, HIGH);
    tone(buzzer, win);
    delay(1000);
    digitalWrite(ledG, LOW);
    noTone(buzzer);
    delay(1000);
    digitalWrite(ledG, HIGH);
    tone(buzzer, win);
    delay(1000);
    digitalWrite(ledG, LOW);
    noTone(buzzer);
    delay(1000);
    digitalWrite(ledG, HIGH);
    tone(buzzer, win);
    delay(1000);
    digitalWrite(ledG, LOW);
    noTone(buzzer);
  }
  else
  {
    Serial.println("Wrong");
    digitalWrite(ledR, HIGH);
    tone(buzzer, lose);
    delay(1000);
    digitalWrite(ledR, LOW);
    noTone(buzzer);
    delay(1000);
    digitalWrite(ledR, HIGH);
    tone(buzzer, lose);
    delay(1000);
    digitalWrite(ledR, LOW);
    noTone(buzzer);
    delay(1000);
    digitalWrite(ledR, HIGH);
    tone(buzzer, lose);
    delay(1000);
    digitalWrite(ledR, LOW);
    noTone(buzzer);
  }
}

void ledOn(byte led1, int sound)
{
  digitalWrite(led1, HIGH);
  tone(buzzer, sound);
  delay(wait);
  digitalWrite(led1, LOW);
  noTone(buzzer);
  delay(between);
}

void ledMix(byte led2, byte led3, byte power1, int sound)
{
  analogWrite(led2, power1);
  digitalWrite(led3, HIGH);
  tone(buzzer, sound);
  delay(wait);
  noTone(buzzer);
  analogWrite(led2, 0);
  digitalWrite(led3, LOW);
  delay(between);
}



//Sample sequence
/*
void seq2()
{
  if (bool g == 1)
  {
    ledOn(ledR, soundR); //Red
    ledMix(ledB, ledR, fullPower, soundP);//Purple
    ledOn(ledB, soundB);//Blue
    ledOn(ledR, soundR);
    ledOn(ledG, soundG);//Green
    ledMix(ledG,ledR, powerG, soundY);//Yellow
    ledOn(ledB, soundB);
  }
}
*/

pattern is an array of 7 values with elements numbered 0 to 6

You call inputCheck() with pattern as a parameter

  //Test input ***********************************************************************************
  inputCheck(pattern);

inputCheck looks like this

void inputCheck(byte sequence[7])

sequence[7] is not part of the array that you populated so could hold any value

That is true, and I’ve now fixed that. My problem is before that in the code, in the section under the comment “Generate and print sequence”. It is supposed to write a random value from 1 to 5 in each position for pattern, but it leaves the array filled with 0 instead.

I tried this same code in a separate sketch, and it worked perfectly, generating random arrays of 7 values from 1 to 5 with repeating values allowed. My question is, why does this code work on its own but not in the context of my main code? (See first post for main code)

Below is the sketch that functioned as intended:

byte sequence[7];


void setup() {
  randomSeed(analogRead(0));
  Serial.begin(9600);
}

void loop() {
   for(byte index = 0; index < 7; index++)
   {
    sequence[index] = random(1,6);
   }

   for(byte printer = 0; printer < 7; printer++)
   {
    Serial.println(sequence[printer]);
   }
   Serial.println("End");
   delay(10000);
}

I've now fixed that

Fixed what exactly ?
Please post your complete fixed sketch

you have a ; of the end of the for() statement

 for (index; index < 7; index++);
  {
    pattern[index] = random(1, 6);
  }

horace:
you have a ; of the end of the for() statement

That semicolon was the problem, it works perfectly now. Thanks for spotting that!