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)
}
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.
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)...
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.
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()
{
}
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
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.