So I have a code that converts CAN messages to text. All works good until it reaches the last array position. That is the moment that the conversion goes weird, and starts going in some sort of a loop, because it does not exit the function, as it should.
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
String frequency_box;
String function_name;
String endString = ":end_string";
void setup() {
Serial.begin(2000000);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
}
//Channel number function
String chan_number(char recv_char){
String ch_nr = "0";
if (recv_char == 0x10) ch_nr = "1";
if (recv_char == 0x11) ch_nr = "2";
if (recv_char == 0x12) ch_nr = "3";
if (recv_char == 0x13) ch_nr = "4";
if (recv_char == 0x14) ch_nr = "5";
if (recv_char == 0x15) ch_nr = "6";
if (recv_char == 0x46) ch_nr = "7";
if (recv_char == 0x47) ch_nr = "8";
if (recv_char == 0x48) ch_nr = "9";
if (recv_char == 0x49) ch_nr = "10";
if (recv_char == 0x4A) ch_nr = "11";
if (recv_char == 0x4B) ch_nr = "12";
return ch_nr;
}
String icons(char input){
String iconOutput;
if (input == 0x01) iconOutput = "Audio_settings";
if (input == 0x02) iconOutput = "Phone_settings";
if (input == 0x03) iconOutput = "System_settings";
if (input == 0x04) iconOutput = "Bluetooth";
if (input == 0x05) iconOutput = "Default_settings";
if (input == 0x12) iconOutput = "Music_note";
if (input == 0x13) iconOutput = "Vehicle_phonebook";
if (input == 0x14) iconOutput = "Mobile_phonebook";
if (input == 0x15) iconOutput = "Call_history";
if (input == 0x16) iconOutput = "Mail_box";
if (input == 0x17) iconOutput = "Directory_management";
if (input == 0x20) iconOutput = "Put_on_hold";
if (input == 0x21) iconOutput = "USB";
if (input == 0x24) iconOutput = "Volume";
if (input == 0x25) iconOutput = "Ringtone";
if (input == 0x26) iconOutput = "Cercle_empty";
if (input == 0x27) iconOutput = "Cercle_full";
if (input == 0x62) iconOutput = "Emergency";
return iconOutput;
}
void gridSort3x2(bool counter){
int arraySize = 6;
String array3x2[arraySize];
bool start_titles = false;
bool counter_0x00 = false;
int rowNr = 1;
int columnNr = 1;
int arrayPos = 0;
int arrayElements = 0;
char character;
while (counter == false){
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
if (canMsg.can_id == 1313 && canMsg.data[0] == 0x74){
Serial.println("endOfMessage-1313");
//Check how many strings are in the 3x3 array
for (int i = 0; i < arraySize; i++){
if (array3x2[i].length() > 0) arrayElements++;
}
//Check how many strings are in the 3x3 array
//Serial transmit the titles depending of number
if (arrayElements == 3){
//Insert below the 3 items identifier
Serial.print("complex_grid1x2:");
Serial.print(array3x2[2]);
Serial.print(":");
Serial.print(array3x2[5]);
Serial.println(endString);
return;
}
if (arrayElements == 6){
//Insert below the 3 items identifier
Serial.print("complex_grid3x2:");
Serial.print(array3x2[1]);
Serial.print(":");
Serial.print(array3x2[2]);
Serial.print(":");
Serial.print(array3x2[3]);
Serial.print(":");
Serial.print(array3x2[4]);
Serial.print(":");
Serial.print(array3x2[5]);
Serial.print(":");
Serial.print(array3x2[6]);
Serial.println(endString);
return;
}
}
if (canMsg.can_id == 289 && canMsg.data[0] ==0x23){
start_titles = true;
Serial.println("Starting text reading");
}
if (canMsg.can_id == 289 && start_titles == true){
//Serial.println(arrayElements);
for (int i=1; i<=7; i++){
if (canMsg.data[i] == 0x0D){
if (rowNr == 3){
//rowNr = 1;
}else rowNr++;
}
if (canMsg.data[i] == 0x00 && i != 0){
if (counter_0x00 == true){
columnNr++;
rowNr = 1;
counter_0x00 = false;
} else counter_0x00 = true;
} else counter_0x00 = false;
//Checking where on the 3x2 array whe are to save the string
if (rowNr == 1 && columnNr == 1) arrayPos = 1;
if (rowNr == 2 && columnNr == 1) arrayPos = 2;
if (rowNr == 3 && columnNr == 1) arrayPos = 3;
if (rowNr == 1 && columnNr == 2) arrayPos = 4;
if (rowNr == 2 && columnNr == 2) arrayPos = 5;
if (rowNr == 3 && columnNr == 2) arrayPos = 6;
//Checking where on the 3x2 array whe are to save the string
//Serial.println(arrayPos);
//Saving the string on the coresponding 3x3 array position
if (columnNr == 1){
if (canMsg.data[i] <= 0x7F && canMsg.data[i] != 0x0D){
//Save the icon name in the first array column
array3x2[arrayPos] = icons(canMsg.data[i]);
}
}
if (columnNr == 2){
if (canMsg.data[i] >= 0x20 && canMsg.data[i] <= 0x7E){
character = canMsg.data[i];
array3x2[arrayPos].concat(character);
Serial.print("columnNr:");
Serial.print(columnNr);
Serial.print(" rowNr:");
Serial.print(rowNr);
Serial.print(" arrayPos:");
Serial.print(arrayPos);
Serial.print(" Text:");
Serial.println(array3x2[arrayPos]);
}
}
}
//Saving the string on the coresponding 3x2 array position
}
}
}
}
void loop(){
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
if(canMsg.can_id == 289){
if (canMsg.data[0] == 0x10 && canMsg.data[2] == 0x25){
Serial.println("Check1");
if (canMsg.data[4] == 0x09){
Serial.println("Check2");
if (canMsg.data[6] == 0x40){
Serial.println("Entered GridSort3x2");
gridSort3x2(false);
Serial.println("Exited GridSort3x2");
}
}
}
}
}
}
Below is the text that gets out. Pay attention that when it reaches arrayPos:6 it does not save the correct character, however, the "character" variable does have the correct character saved. Usually it goes further than the character "S", this time it stucked here.
Entered GridSort3x2
Starting text reading
columnNr:2 rowNr:1 arrayPos:4 Text:A
columnNr:2 rowNr:1 arrayPos:4 Text:Au
columnNr:2 rowNr:1 arrayPos:4 Text:Aud
columnNr:2 rowNr:1 arrayPos:4 Text:Audi
columnNr:2 rowNr:1 arrayPos:4 Text:Audio
columnNr:2 rowNr:1 arrayPos:4 Text:Audio
columnNr:2 rowNr:1 arrayPos:4 Text:Audio s
columnNr:2 rowNr:1 arrayPos:4 Text:Audio se
columnNr:2 rowNr:1 arrayPos:4 Text:Audio set
columnNr:2 rowNr:1 arrayPos:4 Text:Audio sett
columnNr:2 rowNr:1 arrayPos:4 Text:Audio setti
columnNr:2 rowNr:1 arrayPos:4 Text:Audio settin
columnNr:2 rowNr:1 arrayPos:4 Text:Audio setting
columnNr:2 rowNr:1 arrayPos:4 Text:Audio settings
columnNr:2 rowNr:2 arrayPos:5 Text:P
columnNr:2 rowNr:2 arrayPos:5 Text:Ph
columnNr:2 rowNr:2 arrayPos:5 Text:Pho
columnNr:2 rowNr:2 arrayPos:5 Text:Phon
columnNr:2 rowNr:2 arrayPos:5 Text:Phone
columnNr:2 rowNr:2 arrayPos:5 Text:Phone
columnNr:2 rowNr:2 arrayPos:5 Text:Phone s
columnNr:2 rowNr:2 arrayPos:5 Text:Phone se
columnNr:2 rowNr:2 arrayPos:5 Text:Phone set
columnNr:2 rowNr:2 arrayPos:5 Text:Phone sett
columnNr:2 rowNr:2 arrayPos:5 Text:Phone setti
columnNr:2 rowNr:2 arrayPos:5 Text:Phone settin
columnNr:2 rowNr:2 arrayPos:5 Text:Phone setting
columnNr:2 rowNr:2 arrayPos:5 Text:Phone settings
columnNr:2 rowNr:3 arrayPos:6 Text:
289 53 S
columnNr:2 rowNr:3 arrayPos:6 Text: