Hi there! I am trying to assign a string named as radiopacket with array of 5 elements.
But it is not working and showing this error above.
String radiopacket[5] ;
radiopacket += "Q ";
radiopacket += ",";
radiopacket += node;
radiopacket += ",";
radiopacket += des_ID;
radiopacket += ",";
radiopacket += "Hello";
Appreciate if anyone can help on this!
stephanie9:
Hi there! I am trying to assign a string named as radiopacket with array of 5 elements.
But it is not working and showing this error above.
String radiopacket[5] ;
radiopacket += "Q "
radiopacket += ",";
radiopacket += node;
radiopacket += ",";
radiopacket += des_ID;
radiopacket += ",";
radiopacket += "Hello";
Appreciate if anyone can help on this!
donno what 'node' and 'des_ID' are but assuming they are integers, is this what you are trying to achieve?
void setup() {
int node=0 , des_ID = 123;
String radiopacket = "" ;
Serial.begin(115200);
radiopacket += "Q ";
radiopacket += ",";
radiopacket += String(node);
radiopacket += ",";
radiopacket += String(des_ID);
radiopacket += ",";
radiopacket += "Hello";
Serial.println(radiopacket);
}
void loop() {
}
hope that helps....
Ya it is! But now I am facing another problem, I wanted to find the array [3] for example, it shows "," which is not giving "Hello" .Below is my code:
Serial.print("Sending "); Serial.println(radiopacket);Serial.println(radiopacket[3]); delay(10);
Please post ALL the code (in code tags) AND ALL the error message(s)
Below is the related part, I can't post complete one becos of the length:
void loop()
{
Serial.println("Sending to rf95_server");
// Send a message to rf95_server
String radiopacket [5] ;
radiopacket += "Q ";
radiopacket += ",";
radiopacket += node;
radiopacket += ",";
radiopacket += des_ID;
radiopacket += ",";
radiopacket += "Hello";
Serial.print("Sending "); Serial.println(radiopacket);Serial.println(radiopacket.length());Serial.println(radiopacket[3]); delay(10);
rf95.send((uint8_t*)radiopacket.c_str(), radiopacket.length()+1);
We still don't know what node and des_ID are.
Also, radiopacket is an array - you appear to have forgotten the subscript.
Please remember to use CODE TAGS
node and des_ID are integer types. May I know what subscript u mean?
stephanie9:
node and des_ID are integer types. May I know what subscript u mean?
I mean the subscript like this radiopacket [<subscript>] += "Q ";
Do I need subscripts for every things I add on the string?even the ",". Because I wanted to make them as 4 elements in the array, and I can call any of them by radiopacket [3] for example. so the "," should be ignored as an elements.
stephanie9:
Do I need subscripts for every things I add on the string?
You have five Strings.
Which one did you want to use?
And yes, if you have an array, you have to use a subscript.
May I know what is the fifth strings? Because if I call radiopacket [3] now, it shows "," instead of the des_ID, the comma should be ignored and not counted as elements in my case..
Could you pls show some example on how do I add the subcsript? I get confused still..
not too sure how you would do that using Strings but this is how I would do it using c-string
void setup() {
int node = 0, des_ID = 123;
char radiopacket[7][6] = {"Q ", ",", "node--", ",", "des_ID", ",", "Hello"};
Serial.begin(115200);
for (int i = 0; i < 7; ++i) {
Serial.print(radiopacket[i]);
}
Serial.println("");
//update 'node' and 'des_ID' values
sprintf(&radiopacket[2][0], "%d", node);
sprintf(&radiopacket[4][0], "%d", des_ID);
for (int i = 0; i < 7; ++i) {
Serial.print(radiopacket[i]);
}
}
void loop() {
}
7 = number of strings elements
6 = MAXIMUM length any of the strings is expected to be (ideally you would want to be ALWAYS less than the maximum length)
hope that helps....
I have to use String because of this line:
rf95.send((uint8_t*)rao.c_str(), rao.length()+1);
So is there any other way to solve??
stephanie9:
May I know what is the fifth strings?
These five **S**trings
String radiopacket [5] ;
They are radiopacket [0]
, radiopacket [1], radiopacket [2], radiopacket [3] and radiopacket [4].
If you don't want array of five **S**trings, just use
String radiopacket;
stephanie9:
I have to use String because of this line:
rf95.send((uint8_t*)rao.c_str(), rao.length()+1);
So is there any other way to solve??
lol!
(uint8_t*)rao.c_str() = that is converting String to c-string!
rao.length()+1 = that is the length of the string being sent ie same as strlen(rao) +1, where rao would be a c-string
I have to use String because of this line:
That line screams that you don't have to use String.
Because when I tried to remove that line, another error came out,thats why I assumed it as the problem. 
May I know how to modify the line?
or maintain it as string but put it as array like what I wanted to do at the first?
stephanie9:
I have to use String because of this line:
rf95.send((uint8_t*)rao.c_str(), rao.length()+1);
I think this is probably all you need to do
char radiopacket[25];
sprintf(radiopacket, "Q ,%d,%d,Hello", node, des_ID); //where node and des_ID are integers
rf95.send(radiopacket, strlen(radiopacket)+1);
hope that helps...
int node =11;
int Des_ID = 22;
It works but, when I tried to call radiopacket[3], it shows "1", is not showing "11" instead.
11 is the desired one..
stephanie9:
int node =11;
int Des_ID = 22;
It works but, when I tried to call radiopacket[3], it shows "1", is not showing "11" instead.
11 is the desired one..
when I tried to call radiopacket[3], it shows "1", is not showing "11" instead.
That IS correct!
radiopacket[3] means 4th character (since we start with Zero) which is '1'.
try printing out radiopacket[3] andradiopacket[4] and you should get your much wanted "11" 