Hy guys. I'm having an issue with the concatenation of multiple strings.
As you can see in the function bellow, there are two ways I am trying to send through serial a message. The second method where I am printing the string one after the other, it works fine.
void musical_atmosphere(bool counter, char signs){
int arraySize = 11;
String musical_atmosphere = String("");
String array3x2[arraySize];
bool start_titles = false;
bool curselect = false;
bool counter_set = false;
int rowNr = 1;
int columnNr = 1;
int arrayPos = 0;
int curent_selected = 0;
bool counter_0x00 = false;
bool iconBit = false;
char character;
while (counter == false){
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
if ((canMsg.can_id == 1313 && canMsg.data[0] == 0x74) || arrayPos >= 10 ){
//Serial transmit the titles
// String ex: musical_atmosphere : 2 : off : on : : title_1 : title_2 : : + : 5 : - : 2 : end_string
// Highlighted portion 21/1=text box ; 22/2=bass ; 23/3=treble
/*
musical_atmosphere = String("musical_atmosphere:") + curent_selected;
for (int i = 1; i <= 10; i++){
musical_atmosphere = String(musical_atmosphere) + String(":") + array3x2[i];
}
musical_atmosphere = musical_atmosphere + endString;
lastMessage = musical_atmosphere;
Serial.println(musical_atmosphere);
*/
///////////////////////////////////////////
/*
Serial.print("musical_atmosphere:");
//Highlighted portion 21/1=text box ; 22/2=bass ; 23/3=treble
Serial.print(curent_selected);
for (int i = 1; i <= 10; i++){
Serial.print(":");
Serial.print(array3x2[i]);
}
Serial.println(endString);
*/
return;
}
}
}
The first one, where I tried adding the string to a single one, gives me the following messages
:4:end_string
::::end_string
:+:0:::end_string
:::::end_string
:::::end_string
:::::end_string
:+:4:+:4:end_string
:::::end_string
:::::end_string
:::::end_string
:+:4:+:4:end_string
::::end_string
::::end_string
::::end_string
I know what you will say, the String array does not have any values saved in it, but I checked to see if there are using Serial.print() to show me all the array before concatenating, and all the correct values are there.I tried removing the int variable, still no luck. I tried declaring the musical_atmosphere without any value, with String() and with String(""); . Tried using a variable for the ':' character, tried adding it with char(':'), then with String(":"). Still no luck.
I know it would be easyer to just use the Serial.print() and be done with it, but I have other function that has a lot more strings that are not saved in an array, and it does not look pretty.
Edit: I attached the whole code bellow (beside other functions that does the same thing as the one above)
#include <mcp2515.h>
#include <Keyboard.h>
#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_AVR_MICRO)
#define BOARD "Keyboard"
#endif
struct can_frame canMsg;
MCP2515 mcp2515(10);
int high_box = 0;
String frequency_box;
String function_name;
String endString = ":end_string";
String incomingByte;
String lastSource;
String lastMessage;
String lastVolume;
unsigned long delayTime = 250;
void setup() {
Serial.begin(250000);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
}
//Musical atmosphere function
void musical_atmosphere(bool counter, char signs){
int arraySize = 11;
String musical_atmosphere = String("");
String array3x2[arraySize];
bool start_titles = false;
bool curselect = false;
bool counter_set = false;
int rowNr = 1;
int columnNr = 1;
int arrayPos = 0;
int curent_selected = 0;
bool counter_0x00 = false;
bool iconBit = false;
char character;
while (counter == false){
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
if ((canMsg.can_id == 1313 && canMsg.data[0] == 0x74) || arrayPos >= 10 ){
//Serial transmit the titles
// String ex: musical_atmosphere : 2 : off : on : : title_1 : title_2 : : + : 5 : - : 2 : end_string
// Highlighted portion 21/1=text box ; 22/2=bass ; 23/3=treble
// function name
///*
//musical_atmosphere = String("musical_atmosphere:") + curent_selected;
musical_atmosphere = String("musical_atmosphere:");
for (int i = 1; i <= 10; i++){
musical_atmosphere = String(musical_atmosphere) + String(":") + array3x2[i];
}
musical_atmosphere = musical_atmosphere + endString;
lastMessage = musical_atmosphere;
Serial.println(musical_atmosphere);
//*/
///////////////////////////////////////////
/*
Serial.print("musical_atmosphere:");
//Highlighted portion 21/1=text box ; 22/2=bass ; 23/3=treble
Serial.print(curent_selected);
for (int i = 1; i <= 10; i++){
Serial.print(":");
Serial.print(array3x2[i]);
}
Serial.println(endString);
*/
return;
}
if (canMsg.can_id == 289 && canMsg.data[0] == 0x22 && curselect == false){
if (canMsg.data[4] == 0x21) curent_selected = 2;
if (canMsg.data[4] == 0x22) curent_selected = 3;
if (canMsg.data[4] == 0x23) curent_selected = 4;
curselect = true;
}
if (canMsg.can_id == 289 && canMsg.data[0] >=0x23){
start_titles = true;
}
if (canMsg.can_id == 289 && start_titles == true){
////////// Counter for separating the titles //////////
for (int i=1; i<=7; i++){
if (canMsg.data[i] == 0x0D){
rowNr++;
columnNr = 1;
}
//Finish with the grid, check treble and bass values
if (canMsg.data[i] == 0x00 && i != 0 && counter_set == false){
if (counter_0x00 == true){
arrayPos = 7;
//Serial.println(arrayPos);
counter_0x00 = false;
counter_set = true;
continue;
} else counter_0x00 = true;
} else counter_0x00 = false;
//Finish with the grid, check treble and bass values
//Checking where on the 3x2 array whe are to save the string
if (arrayPos < 7) {
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
if (canMsg.data[i] == 0xF1 && i < 7) {
array3x2[arrayPos] = icons(canMsg.data[i+1]);
columnNr++;
continue;
}
if (canMsg.data[i] == 0XF1 && i == 7){
iconBit = true;
continue;
}
if (iconBit == true && i == 1 && i < 7){
array3x2[arrayPos] = icons(canMsg.data[i]);
iconBit = false;
columnNr++;
continue;
}
if (columnNr == 2 && arrayPos < 7 && canMsg.data[i] != 0x26 && canMsg.data[i] != 0x27){
if (canMsg.data[i] >= 0x20 && canMsg.data[i] <= 0x7E){
character = canMsg.data[i];
array3x2[arrayPos].concat(character);
}
}
if (signs == 0x5A && canMsg.data[0] == 0x2B){
if (canMsg.data[5] <= 0x09) array3x2[7] = "+";
if (canMsg.data[5] >= 0xF7) array3x2[7] = "-";
//-------------------------------------------
if (canMsg.data[6] <= 0x09) array3x2[9] = "+";
if (canMsg.data[6] >= 0xF7) array3x2[9] = "-";
if (canMsg.data[5] <= 0x09) array3x2[8] = canMsg.data[5];
if (canMsg.data[5] >= 0xF7) array3x2[8] = 256 - canMsg.data[5];
//------------------------------------------------------------
if (canMsg.data[6] <= 0x09) array3x2[10] = canMsg.data[6];
if (canMsg.data[6] >= 0xF7) array3x2[10] = 256 - canMsg.data[6];
}
}
}
}
}
}
void loop(){
String messageRecv ;
if (Serial.available()){
Serial.print("enteredSerialBuffer:");
Serial.print(incomingByte);
Serial.println(endString);
Serial.println(lastSource);
Serial.println(lastVolume);
char c = Serial.read();
incomingByte.concat(c);
if (incomingByte == "reqMsg"){
Serial.println(lastSource);
Serial.println(lastVolume);
Serial.println(lastMessage);
}
}
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
if(canMsg.can_id == 289){
//Volume function
if (canMsg.data[0] == 0x10 && canMsg.data[2] == 0x35){
volume_text(false, false);
}
else if (canMsg.data[0] == 0x03 && canMsg.data[2] == 0x08){
volume_text(false, true);
}
//Highlighted box
else if(canMsg.data[0] == 0x10 && canMsg.data[1] == 0x0E && canMsg.data[2] == 0x20 && canMsg.data[3] == 0x00){
//In the frame with the first bit=21 the 7'th bit represents the position of the highlighted
//box. The first box has the value 20, the second 21 and so on.
//Serial.println("ENTER HIGHLIGHTED BOX");
high_box = high_box_function(false);
//Serial.println("EXIT");
//Serial.println();
}
else if (canMsg.data[0] == 0x10 && canMsg.data[2] == 0x25){
if ((canMsg.data[3] == 0xC1 || canMsg.data[3] == 0xC3) && canMsg.data[4] == 0x09 ){
if (canMsg.data[7] == 0x00){
frequency_box = frequency_type_box(false);
}
else gridSort3x2(false); // CHECKED
}
else if (canMsg.data[4] == 0x17){
gridSortRadio(false); // CHECKED
}
else if (canMsg.data[4] == 0x13 && canMsg.data[5] == 0x03){
gridPTY3x2(false, canMsg.data[3]);
}
else if (canMsg.data[4] == 0x07 && canMsg.data[6] == 0x20){
gridSort3x3(false); // CHECKED
}
else if (canMsg.data[3] == 0x70 && canMsg.data[4] == 0x00 && canMsg.data[5] == 0x04 && canMsg.data[6] == 0x40){
menu_volume(false, function_name);
}
else if (canMsg.data[3] == 0x73 && canMsg.data[4] == 0x02 && canMsg.data[6] == 0x40){
musical_atmosphere(false, canMsg.data[1]);
}
else if (canMsg.data[3] == 0x63 && canMsg.data[4] == 0x09){
confirm_cancel_function(false);
}
else if (canMsg.data[3] == 0x52 && canMsg.data[4] == 0x09){
info_box(false);
}
else if (canMsg.data[3] == 0x41 && canMsg.data[4] == 0x13 && canMsg.data[5] == 0x01 && canMsg.data[6] == 0x20 && canMsg.data[7] == 0x80){
sources();
}
}
}
/*
else if (BOARD == "Keyboard" && canMsg.can_id == 1597){
keyboardButtons(canMsg.data[0]);
}
else if (BOARD == "Keyboard" && canMsg.can_id == 1598){
keyboardJoystick(canMsg.data[0], canMsg.data[1]);
}
/*
else if (canMsg.can_id == 1423){
satelliteButtons(canMsg.data[1] == 0x01, canMsg.data[2]);
}
*/
}
}