Use a for() loop to establish a global sketch variable...

I have a question that may either be super easy to resolve, or totally impossible. I have spent the better part of a day researching and experimenting with various methods (including EEPROM get/put and so on), to no avail.

I have modified a very solid sketch (by others) to shuffle [12] predetermined digits (in my case 0's and 1's only). The sketch uses a for() loop to do this and when inside the loop (of course) will output a reliably random new array (within the limits of the maths).

I would like to have access to this new shuffle (constructed in setup) for use in the sketch (loop); for instance to control 12 led's (maybe off or on; or red/green with some additional work). In previous projects I have dedicated 12 I/O pins and digitalWrite them inside the for() loop. Then once I exit I have to digitalRead them to create activity on other siamese pins. I know this is inefficient and cumbersome. I definitely don't program for a living, just hobby projects and such.

Here's my simple code, any advice would be appreciated. Also I want to thank this forum for a great many years of research and learning about Arduino's and programming them. I have accomplished some crazy stuff thanks to the time the "guru's" spend engaging the "learners" in the community. So much so that I have never had to make a post of my own until now. Cheers.

//Sketch takes a set of [12] predetermined numbers {array} and shuffles them

int game[12] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
int xfer; // Could be "byte"? I only need one placeholder per digit.

void setup()
{
  Serial.begin(9600);
  Serial.println("Project v3.0");

  randomSeed(analogRead(0));
  for (int i = 0; i < 12; i++)
  {
    int pos = random(12);
    int t = game[i];
    game[i] = game[pos];
    game[pos] = t;
    xfer = game[i]; //xfer is the shuffled data I want to use globally outside the for() loop.
    Serial.print(xfer); //this prints the shuffle perfectly and random every time (within maths)
  }

  Serial.print(xfer); //this clearly won't work outside the for() loop
}

void loop()
{
  //control 12 LED's with the new shuffled set of binary values (from for() loop)
}

You said quite a lot, but didn't mention what the problem is. I'll assume it crashes, because:

    int pos = random(12);

The parameter to random is inclusive. That means that 'pos' can point outside the array.

I thought it was exclusive?

Either way, what is the issue about scope here?

TheMemberFormerlyKnownAsAWOL:
I thought it was exclusive?

Either way, what is the issue about scope here?

Oops that's true, I misread it.

This is clearly wrong, xfer has no index

    xfer = game[i]; //xfer is the shuffled data I want to use globally outside the for() loop.

...beat me to it...

    xfer = game[i]; //xfer is the shuffled data I want to use globally outside the for() loop

Which element of the xfer array would you like set to the value of game with this line ?

game"i" truly has the shuffled digits I want to work with. I created xfer as a means to capture it as a full set, not incremented set (ergo "i"). I could just be ridiculous and it not be needed. The highest level explanation of the project is: shuffle six 0's and 1's (upon reset button), then activate led's with the fresh set of data.

I appreciate the acknowledgement of my post. I will study hard the info/advice received...

C/C++ has no intrinsic ability to copy arrays. You have to use custom code or something from the standard utilities, like strcpy() or memcpy() to do that. I suggest that you check your code for opportunities to populate 'xfer' at the same time as you do the shuffle.

game"i" truly has the shuffled digits I want to work with

game [i] is just a single bit - one of twelve.

TheMemberFormerlyKnownAsAWOL:
game [i] is just a single bit - one of twelve.

Thanks guys for the feedback. Indeed game [i] is a bit in an array of twelve. I want to execute activity against this array generated in the for() loop, elsewhere in the sketch. The primary question being; can variables created inside of a for() loop be transferred to a global status? That is the essence of my situation. I have yet to find an example of such published (probably that's my answer right there)...

he primary question being; can variables created inside of a for() loop be transferred to a global status?

No, but it's not necessary, because global variables can be modified inside a for loop.

txprotolab:
Thanks guys for the feedback. Indeed game [i] is a bit in an array of twelve. I want to execute activity against this array generated in the for() loop, elsewhere in the sketch. The primary question being; can variables created inside of a for() loop be transferred to a global status? That is the essence of my situation. I have yet to find an example of such published (probably that's my answer right there)...

No, variables within a for loop code block cannot be used elsewhere.
However, your variable xfer has global scope, so can be used everywhere.

What's weird is that you talk about 'xfer' as if it should hold 12 values, for example

I created xfer as a means to capture it as a full set

but declare it as a single variable. See also reply #7. What is it used for? What does it represent? Certainly, the name doesn't tell me much.

int xfer was something I made up while messing with things.

Serial.print (game[i]);

offers the same (serial) output result. When I run this code snip the serial monitor results:

Project v3.0
010010110100

It is that twelve digit number set (randomized after every reset) that I am trying to package up and use in other functions in the sketch. I am not able to see or use the shuffled array outside of the for() loop and this is likely not possible per previous advice.

I appreciate the (su)peer(ior) review of this issue, I may need to attack the problem in a different way. It's very enjoyable to learn this stuff, even if it's challenging.

I am not able to see or use the shuffled array outside of the for() loop

Puzzled. 'game[]' is available to use globally, because it's declared globally. Why not just read it? You can access any of its members in either loop() or setup().

aarg:
Puzzled. 'game[]' is available to use globally, because it's declared globally. Why not just read it? You can access any of its members in either loop() or setup().

Yes I am baffled why a for() loop would populate a global array, but the data no longer be accessible if called outside the for() loop that created it. I'm obsessing on this and wish I could stop but it doesn't seem right! Let's just say, what's wrong with this simple code sketch (why doesn't it Serial.print array outside of the for() loop)? *FYI-Previously I had to digitalWrite twleve pins inside the for() loop, and the read them back (into an array) elsewhere in the sketch to use the "data". There has to be a way to carry this array around and use it without writing to pins...

int game[12];
int xfer[12];

void setup()
{
  Serial.begin(9600);
  Serial.println("Project v3.0");

  randomSeed(analogRead(A0));
  for (int i = 0; i < 12; i++)
  {
    game[i] = random(0, 2);
    Serial.print(game[i]);
    game[i] = xfer[12];
  }
  Serial.println();
  Serial.print(xfer[12]); // Doesn't work with (game[12]) either...
  Serial.println();
}

void loop()
{
}

xfer[12] does not exist. Only xfer[0] to xfer[11]. Same for game [12].

Yes I have toiled with this also; the concept of place holders but that start with zero not one. It matters not if I call for

Serial.print(xfer[11]);

I still do not get the array to print, nor the 11th place generated in the above for() loop.

Project v3.0
000110010101
0

I know when I get this figured out I will be a bit wiser... haha

I'm going to end it here with the solution I found; it seems so ridiculous simple that I have a brain freeze from too much examination. All of the help comments were effectively correct in their own way. It was fun to deliberate in this forum; thanks again for the support.

Here's the code that generates a favorable response in the serial monitor. I left it highly verbose as that is how I discovered it (thanks to the help I got here) and will transmit it over infrastructure. At any time I can roll it back up in an incrementing for() loop to optimize the sketch (on any board(node)).
Serial Output

Project v3.0
101011101000
************
101011101000
************
HLHLHHHLHLLL

Sketch

int game[12];

void setup()
{
  Serial.begin(9600);
  Serial.println("Project v3.0");

  randomSeed(analogRead(A0));
  for (int i = 0; i < 12; i++)
  {
    game[i] = random(0, 2);
    Serial.print(game[i]);
  }
  Serial.println();
  Serial.println("************");
  Serial.print(game[0]);
  Serial.print(game[1]);
  Serial.print(game[2]);
  Serial.print(game[3]);
  Serial.print(game[4]);
  Serial.print(game[5]);
  Serial.print(game[6]);
  Serial.print(game[7]);
  Serial.print(game[8]);
  Serial.print(game[9]);
  Serial.print(game[10]);
  Serial.print(game[11]);
  Serial.println();
  Serial.println("************");
  delay (2500);
}

void loop()
{
  if (game[0] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[1] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[2] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[3] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[4] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[5] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[6] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[7] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[8] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[9] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[10] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[11] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  Serial.println();
}

Cheers.

Serial.print(game[1]);
  Serial.print(game[2]);
  Serial.print(game[3]);
  Serial.print(game[4]);
  Serial.print(game[5]);
  Serial.print(game[6]);
  Serial.print(game[7]);
  Serial.print(game[8]);
  Serial.print(game[9]);
  Serial.print(game[10]);
  Serial.print(game[11]);

Oh, seriously? You used a for loop elsewhere, why not here?

  if (game[0] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }
  if (game[1] == LOW) {
    Serial.print("L");
  }

... also here. Any time you see repetition like that, it's time for a loop. Hint:

  if (game[i] == LOW) {
    Serial.print("L");
  }
  else {
    Serial.print("H");
  }

At any time I can roll it back up in an incrementing for() loop to optimize the sketch (on any board(node)).

I suggest that the time is now (actually the right time would be before you created it, but better late than never). That time is any time you want someone else to read or use it.

Seeing that code hurts :wink:

void loop()
{
  for(int i = 0; i < 12; i++)
  {
    if (game[i] == LOW)
    {
      Serial.print("L");
    }
    else 
    {
      Serial.print("H");
    }
  }
}