I have four four term character arrays:
char table1Orders[4]; // Character array for table 1 order
char table2Orders[4]; // Character array for table 2 order
char table3Orders[4]; // Character array for table 3 order
I also have another 12 term character array that was sent across an XBee connection and interpreted on the receiving end to be:
rrr0bgr0bggr
I am then running these through this sketch:
Note that I can't find how to initialize the character string normally. It was iterated with a for loop to be what I mentioned, an explanation on how to initialize a character string would be great. I can't figure how to initialize it with proper syntax.
int stateVal = 0;
int fourBlockTable;
char table1Orders[4]; // Character array for table 1 order
char table2Orders[4]; // Character array for table 2 order
char table3Orders[4]; // Character array for table 3 order
char orders[13] = {"rrr0bgr0bggr"};
void setup(){
Serial.begin(9600);
}
void loop(){
// If the user sent c, calibrate the sensors
if(orders[0] == 'c'){
stateVal = 1; // Calibrate the robot
}
// If the user sent r, reset the robot
else if(orders[0] == 'e'){
Serial.println("Emergency stop activated, shutting down robot.");
stateVal = -1; // Reset the robot
}
// Otherwise the orders are for the competition, parse the data and organize into tables
else{
for(int i = 0; i <= 3; i++){
table1Orders[i] = orders[i]; // Store block order to table array
}
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.print("The orders for table 1 are: "); Serial.println(table1Orders);
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
for(int i = 4; i <= 7; i++){
table2Orders[(i-4)] = orders[i]; // Store block order to table array
}
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.print("The orders for table 2 are: "); Serial.println(table2Orders);
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
for(int i = 8; i <= 11; i++){
table3Orders[(i-8)] = orders[i]; // Store block order to table array
Serial.println(table3Orders[(i-8)]);
}
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.print("The orders for table 3 are: "); Serial.println(table3Orders);
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
// Identify which table is getting four blocks
if(table1Orders[3] != '0'){
fourBlockTable = 1;
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.println("The table requiring four blocks is table 1");
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
}
else if(table2Orders[3] != '0'){
fourBlockTable = 2;
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.println("The table requiring four blocks is table 2");
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
}
else{
fourBlockTable = 3;
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
Serial.println("The table requiring four blocks is table 1");
// DEBUGGING SERIAL PRINTS -------------------------------------------------------------------------------
}
}
}
When printing out the table1/2/3Orders variables I'm getting the following output:
The orders for table 1 are: rrr0
The orders for table 2 are: bgr0rrr0
The orders for table 3 are: bggrbgr0rrr0
This would imply that table2Orders is growing to an 8 bit char array that includes table1Orders, and likewise table3Orders has grown to a 12 bit array that includes table1Orders and table2Orders.
I have been messing with character arrays for a while and have been trying to wrap my head around their behavior. A new quirk seems to come up every time I think I have it figured out. How is printing out table1Orders also printing the other two orders? Or have table2Orders and table3Orders really been grown somehow?