Hi All,
I'm seeing a strange behavior in a struct array I created. The code looks like below, when I input an index beyond what is formed in the instantiation I am getting a (1,0,0,0) result.
For example when I have 4 objects in the array (index 0-3), the 5th (non-existent) object (index 4) is (1,0,0,0). When I have 5 objects (index 0-4), the 6th (index 5) is (1,0,0,0). Any calls to an object beyond that is (0,0,0,0). I was expecting errors, nulls, etc, possibly all 0s, but definitely not (1,0,0,0).
Any insight or assistance in eliminating this behavior and/or creating a "default" or "doesn't exist" case would be appreciated.
Below is my struct and array instantiation and usage:
//Declare a struct to contain Directions
struct MotorDirection{
int m1;
int m2;
int m3;
int m4;
};
//Directions setup
MotorDirection forward = {1,1,1,1};
MotorDirection left = {-1,1,1,-1};
MotorDirection backward = {-1,-1,-1,-1};
MotorDirection right = {1,-1,-1,1};
//Compile Directions into an array
MotorDirection Directions[] {forward,left,backward,right};
//Print a string of all of the array values
void loop()
{
printDirection(Directions[0], 0);
printDirection(Directions[1], 1);
printDirection(Directions[2], 2);
printDirection(Directions[3], 3);
printDirection(Directions[4], 4);
printDirection(Directions[5], 5);
}
//Parse out a string to give feedback on the array values
void printDirection(MotorDirection md, int index)
{
Serial.print("Index: ");
Serial.print(index);
Serial.print(", Motor Directions: ");
Serial.print(md.m1);
Serial.print(", ");
Serial.print(md.m2);
Serial.print(", ");
Serial.print(md.m3);
Serial.print(", ");
Serial.println(md.m4);
}