Hi, for a project iìam working on there is an arduino that reads data from a bounch of sensors and then sends the data as a strings to another one that's supposed to store them in separate variables. The transmitter code is this:
void sendTelemetry() {
Serial.print(Altitude); Serial.print(" ");
Serial.print(Speed); Serial.print(" ");
Serial.print(Separation); Serial.print(" ");
Serial.print(Pitch); Serial.print(" ");
Serial.print(Roll); Serial.print(" ");
Serial.print(Yaw); Serial.print(" ");
Serial.print(GPSLat); Serial.print(" ");
Serial.print(GPSLon); Serial.print(" ");
Serial.print(Volt); Serial.print(" ");
Serial.println();
}
The problem is that i do not know how to separate (on the receiver side) the single numbers from the string.
Any ideas?
Thanks. L
brescioz:
Any ideas?
See Serial input basics - updated for some ideas
horace
April 25, 2024, 9:35am
3
why do you require two Arduino devices ?
won't one do the job?
gcjr
April 25, 2024, 10:09am
4
here's an approach using strtok() that separates the input line into separate strings and values
output
0 Alt
0 Spd
0 Sep
0 Pit
0 Rol
0 Yaw
0 Lat
0 Lon
0 Vol
100 100
34 34
5 5
9 9
11 11
-7 -7
40 40
-75 -75
2 2
char s [80];
// -----------------------------------------------------------------------------
#define MaxTok 10
char *toks [MaxTok];
int vals [MaxTok];
int
tokenize (
char *s,
const char *sep )
{
unsigned n = 0;
toks [n] = strtok (s, sep);
vals [n] = atoi (toks [n]);
for (n = 1; (toks [n] = strtok (NULL, sep)); n++)
vals [n] = atoi (toks [n]);
return n;
}
// -----------------------------------------------------------------------------
void dispToks (
char * toks [])
{
char s [40];
for (unsigned n = 0; toks [n]; n++) {
sprintf (s, " %6d %s", vals [n], toks [n]);
Serial.println (s);
}
Serial.println ();
}
// -----------------------------------------------------------------------------
void loop ()
{
if (Serial.available ()) {
int n = Serial. readBytesUntil ('\n', s, sizeof(s)-1);
s [n] = 0; // terminate string
tokenize (s, " ");
dispToks (toks);
}
}
// -----------------------------------------------------------------------------
const char * hdr = "Alt Spd Sep Pit Rol Yaw Lat Lon Vol";
const char * data = "100 34 5 9 11 -7 40 -75 2";
void setup ()
{
Serial.begin (9600);
strcpy (s, hdr);
tokenize (s, " ");
dispToks (toks);
strcpy (s, data);
tokenize (s, " ");
dispToks (toks);
}
system
Closed
October 22, 2024, 10:09am
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.