Hi. I have been fiddling now for two days on how to get the arduino to generate a random permutation of the numbers 1-4(or 0-3). However I've been vastly unlucky.
I found a script online that does pretty much what I want, it creates a permutation and sends it to the serial window but I cannot for the life of me get it to be stored in my combination[] byte.
Can anyone give me some pointers as to what I am doing wrong?
uint8_t data[] = {1, 2, 3, 4};
byte combination[] = "1234"; // set your combination here Pink, Green, Green, Red, Blue, Pink
byte userInput[6]; // Change 5 to whatever number of keys/presses you need to get your code right (13324 is five presses, 1334 is four).
void swapEntries(byte a, byte b)
{
byte temp = data[a];
data[a] = data[b];
data[b] = temp;
}
void printData()
{
for (const auto& v : data) {
Serial.print(v);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 4; i++) swapEntries(random(1, 4), random(1, 4)); // perform 10 swaps
printData();
delay(1000);
}
I am being confused by the const auto& in the for loop. If it was a regular for loop, I would've just written somethings along the lines of
void printData()
{
for (const auto& v : data) {
combination[n] = v;
}
Serial.print("The new combination is");
for (int n = 0; n <= 4; n++) {
Serial.print(combination[n]
}
Serial.println();
}
let me say that this is quite similar at "for each" statement in other laguages like C#...so you can read: "for each value contained in data array do..."
works also for Object array, Struct array and others coplex data type...
Yes, it would help if my goal was to write out the combination I wrote in that array, but I want the values/that combination to change every time a button is pressed...
If that makes sense.
The way you have loop() coded you are generating permutations that always start with 1. If you want to include all possibilities change:
for (int i = 0; i < 4; i++) swapEntries(random(1, 4), random(1, 4)); // perform 10 swaps
to
for (int i = 0; i < 4; i++) swapEntries(random(0, 4), random(0, 4)); // perform 10 swaps
Why are you initializing combination to an ASCII string whereas data is initialized to straight binary data? Do you need combination to be in ASCII? If so I would declare it with char instead of byte.
I don't know. You wanted to know how to "get it to be stored in my combination[] byte".
I see no where else in the sketch where you are using combination so I can't possibly know what you are ultimately going to do with it. I assumed you sketch was incomplete.
Because - this is for an escape room prop. Down the road I will have a button that the players press to initiate a chain of lights(showing the permutation). However, this can also be triggered elsewhere. If they did not press that first button, I need a "default" combination defined somewhere.
What you are going to use the combination array for does not matter. Defining the value of a byte array with a string is bizarre, especially when it does not need to be initialised