I'm fairly new to working with Arduino and I'm working on a project to Mod my r/c transmitter. I've got my program working so far but was hoping for some help on optimizing the code. I'd like to put this into a for loop such that it continues to look for arrays until it reaches a programmed integer limiting the number of arrays. I can write a for loop, but I'm having a hard time figuring out how to change the selected array each time it iterates.
TLDR: I want a for loop to also loop through char variables array1, array2, array{number specified in a int}
if (arrayselect == 1)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array1[n];
}
}
if (arrayselect == 2)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array2[n];
}
}
if (arrayselect == 3)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array3[n];
}
}
if (arrayselect == 4)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array4[n];
}
}
if (arrayselect == 5)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array5[n];
}
}
if (arrayselect == 6)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array6[n];
}
}
This demos a concept of using a table/array of pointers to your arrays and using arrayselect to select which one will be copied to arraytop.
It also uses memset and memcpy. If you're not familiar with these look em up
#define ARRAY_SIZE 22
//define the "top" array
byte
ArrayTop[ARRAY_SIZE];
//define each of the "sub" arrays
byte
Array1[ARRAY_SIZE],
Array2[ARRAY_SIZE],
Array3[ARRAY_SIZE],
Array4[ARRAY_SIZE],
Array5[ARRAY_SIZE],
Array6[ARRAY_SIZE];
//create an array of pointers to arrays here. Each entry will be a pointed the "sub" arrays
//declared above
const byte
*Arrays[6] =
{
Array1,
Array2,
Array3,
Array4,
Array5,
Array6
};
//and an array select variable for demo purposes
int
arrayselect;
void setup()
{
//fill the arrays with different data to illustrate concept
for( int i=0; i<6; i++ )
memset( Arrays[i], i, ARRAY_SIZE );
//serial port
Serial.begin(9600);
//init array select
arrayselect = 0;
}//setup
void loop()
{
//this copies ARRAY_SIZE bytes from the Array pointed to by Arrays[arrayselect] to ArrayTop
//a little less clunky than a million for loops and if statements
memcpy( ArrayTop, Arrays[arrayselect], ARRAY_SIZE );
//for proof, print out the contents of ArrayTop each pass
for( int i=0; i<22; i++ )
Serial.print( ArrayTop[i], DEC );Serial.print(" ");
Serial.println();
//bump the arrayselect value, making sure we remain from 0-5 (valid #)
arrayselect++;
if( arrayselect == 6 )
arrayselect = 0;
//make it human readable
delay(1000);
}//loop
While you guys were writing I took my stab at multi dimensional arrays, I'm certain it won't work, but here it is. Was I on the track?
// cut from setup
int nummodes = 6; // number of possible flight modes
char array1[nummodes][23]={" Stabilize "},{" Auto Horizon "},{"Return to Launch "},{" Mission "},{" Loiter "},{" Auto Horizon "};
char arraytop[23];
char arraybottom[23];
int n;
int i;
[hr]
// cut from loop
for (int t = 1; t < nummodes + 1; t++){
if (arrayselect == t)
{
for (n = 0; n < 22; n++)
{
arraytop[n]=array1[t][n];
}
}
}