I have a code in which I am generating random number of Colums = numCols and random number of Rows = numRows. The multiple of nomRows and numCols = numCells. I want to color each of the Cell by a different color, I know the total number of Cells I have, because that is numCells. Therefore I have an array "colors", which has 6 values. Every number in that array represents and how many times I have that color in that array. I have a loop which generates a randomm number for each color, but always making sure the total of the numbers in the array is never larger then NumCells. You cant have total 23 values for color and only 10 cells to fit it into. Now this works fine, the total amout of number in the array is never bigger than numCells.
So I have an array colors[] which contains 6 numbers, I want to push that array into mysound2[] and after I push it create a nother loop which will add 2 more numbers into that array to end up with an array mysound2[] which containts total number of 8 numbers.
But I can't get it working, either I get an error: invalid types 'int [8][int [6]]' for array subscript Which I guess arduino is not happt that into array of 8 I am only trying to add 6 numbers.
Or if I try something else the code generates 0,0,0.
Please how can I push colors[] into mysoun2[] and add 2 more random numbers?
// CODE FOR COLORS
``` int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0
``` int AnalogPin1 = A1; //Declare an integer variable, hooked up to analog pin 1
void setup() {
Serial.begin(9600); //Begin Serial Communication with a baud rate of 9600
randomSeed(analogRead(0));
}
void loop() {
// 1)Gray, 2)White, 3)Yellow, 4)Blue, 5)Black, 6)Red
int numCols1 = random(1,4);
int numCols2 = random(2,5);
int numCols = numCols1 + numCols2;
int numRows1 = random(2,5);
int numRows2 = random(2,6);
int numRows = numRows1 + numRows2;
int numCells = numCols * numRows;
int colors[] = {5,3,2,1,0,0};
int numAlloc = colors[0] + colors[1] + colors[2] + colors[3] + colors[4] + colors[5];
for (int i=0; i<numCells - numAlloc; i++)
{
int color = random(0,7);
color[colors]++;
}
/*The Serial.print() function does not execute a "return" or a space
Also, the "," character is essential for parsing the values,
The comma is not necessary after the last variable.*/
Serial.print(colors [0]);
Serial.print(",");
Serial.print(numCols);
Serial.print(",");
Serial.print(numRows);
Serial.print(",");
//Serial.print(numCells);
//Serial.print(",");
Serial.println();
delay(5000); // For illustration purposes only. This will slow down your program if not removed
}
//CODE FOR ADDING 8 NUMBERS INTO ONE ARRAY WITH 2 LOOPS
# define Parameters 8
int mysound2[Parameters];
int randNumber =0;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
for (int thisPin = 0; thisPin < Parameters-2; thisPin++) {
randNumber = random(1, 100); // generates between 0 and 127
mysound2[thisPin]= randNumber;
}
for (int thisPin = Parameters-2; thisPin < Parameters; thisPin++) {
randNumber = random(100, 200); // generates between 0 and 127
mysound2[thisPin]= randNumber;
}
}
void loop() {
for (int thisPin = 0; thisPin < Parameters; thisPin++) {
Serial.println(mysound2[thisPin]);
delay(5000);
}
}
//ME TRYING TO COMBINE THE TWO CODES
// 1)Gray, 2)White, 3)Yellow, 4)Blue, 5)Black, 6)Red
# define Parameters 8
int numCols1 = random(1,4);
int numCols2 = random(2,5);
int numCols = numCols1 + numCols2;
int numRows1 = random(2,5);
int numRows2 = random(2,6);
int numRows = numRows1 + numRows2;
int numCells = numCols * numRows;
int colors[] = {5,3,2,1,0,0};
int numAlloc = colors[0] + colors[1] + colors[2] + colors[3] + colors[4] + colors[5];
int color = 0;
int mysound1[Parameters];
int randNumber1 =0;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
for (int thisPin = 0; thisPin < Parameters-2; thisPin++)
{
for (int i=0; i<numCells - numAlloc; i++)
{
int color = random(0,6);
color[colors]++;
}
mysound1[color];
}
for (int thisPin = Parameters-2; thisPin < Parameters; thisPin++) {
randNumber1 = random(100, 200); // generates between 0 and 127
mysound1[thisPin]= randNumber1;
}
}
void loop() {
for (int thisPin = 0; thisPin < Parameters; thisPin++) {
Serial.println(mysound1[color]);
delay(2000);
}
}
I understand that you want to copy an array into another array. You can do that with a function like this:
void copyArray(int* source, int* destination, int length) {
for (int i = 0; i < length; i++) {
*destination++ = *source++;
}
}
So, if you want to copy 6 elements in colors
into mysound2
,
copyArray(colors, mysound2, 6);
would do it.
If you want to add 2 more elements, just do something like this.
mysound2[6] = randNumber1;
mysound2[7] = randNumber2;
Edit: In the comment section, you mentioned that you want to combine two arrays into one. A function like this would do that.
void copyArray(int* source, int* destination, int destStartIndex, int len) {
for (int i = 0; i < len; i++) {
destination[destStartIndex + i] = *source++;
}
}
The code below will copy colors
to the first 6 bytes of mysound2
and copy thisPin
to the following 2 bytes of mysound2
.
copyArray(colors, mysound2, 0, 6);
copyArray(thisPin, mysound2, 6, 2);