i am not a super good programmer. but i got alot of figured out for my hobby program.
but can anyone help me figure this out?
SER123,375,123,375,123,375,123,375IAL
SER = arduino understand we started a new update cycle.
IAL = arduino understand update cycle complete. and ready for receiving the next cycle.
and then split the digits per 3 on the , char.
what is the best solution to split it out? (as is it a every serial update cycle it updates it self.
preferbly whit // remarks. so i can actualy understands it as i realy try to understand it.
sorry if this is not the way to ask but all topics i read on this i dont get it in my head even tough the rest of the programming on arduino sinks it relative easy.
Here is an example showing how to split the parts of a c-string out using the strtok() function. Splitting at the commas.
Now what do you want to do with the parts?
char array[] = "SER123,375,123,375,123,375,123,375IAL";
char *strings[10]; // make room for the parts
char *ptr = NULL; // a point to keep track of our place in the array
void setup()
{
Serial.begin(9600);
//Serial.print(array);
byte index = 0;
ptr = strtok(array, ",");
while(ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ",");
}
//Serial.println(index);
for(int n = 0; n < index; n++)
{
Serial.println(strings[n]);
}
}
void loop()
{
// put your main code here, to run repeatedly:
}
Note that the original string (array in the example) will be modified by the strtok() function. If you want to keep the original string, copy it and use strtok() on the copy.
The following sketch accepts and stores the "comma separated data items of this string: SER123,375,123,375,123,375,123,375IAL" into the array named myData[] provided that the preamble ("SER") and the postamble ("IAL") are detected.
//SER123,375,123,375,123,375,123,375IAL
char myStart[4] = "";;
bool flag = false;
char myData[35] = "";
char myStop[4] = "";
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
{
if (flag == false)
{
if ( n == 3)
{
myStart[0] = Serial.read();
myStart[1] = Serial.read();
myStart[2] = Serial.read();
myStart[3] = 0x00; //NULL byte
if (strcmp(myStart, "SER") == 0) //START pattern is found
{
Serial.println(myStart);
flag = true;
memset(myStart, 0, 4);
}
}
}
else
{
byte m = Serial.readBytesUntil('\n', myData, 34);
//----------------------------------------------
myStop[3] = 0x00;
myStop[2] = myData[m - 1];
myStop[1] = myData[m - 2];
myStop[0] = myData[m - 3];
if (strcmp(myStop, "IAL") == 0)//STOP pattern is found
{
Serial.println(myStop);
memset(myStop, 0, 4);
//-------------------
myData[m] = 0x00;
Serial.println(myData);
memset(myData, 0, 34);
flag = false;
}
else
{
flag = false;
memset(myStop, 0, 4);
memset(myData, 0, 34);
}
}
}
}
Now, codes could be added with the above sketch to extract the comma separated data items using atoi() function.
Wow, sadly i am in the car off to work.
but i want to thank the people who helped already. i didnt expect a answer so fast let alone 4!
@golam@ground u two are the best. i wil toy around whit it after i wake up from night shift.
but in a quick peekie i did now i think i might understand already a little bit!
i wil inform u guys how it worked out as soon as posseble.
Here is how I split incoming strings. Based on space
You can adapt it to your comma situation.
void DataSplit() // for the tablet string
{
String c = FromTablet;
// String c = FromTablet.remove(0,2);
String M1, M2, M3, M11, M12, M13, M5 , M6, M7;
//String M14;
String Time_To_Complete_Str;
int var = 0;
if (c[0] == 'g') { // if the command trigger word is detect
Serial.println(c.length()); // display start of message
for (unsigned int i = 1; i <= c.length(); i++) { // a for loop is set to run as many time as the # of characters in a string
if (c[i] == ' ') { // SPace is used for Separation
var++;
// Serial.println (var); // Everytime space is detected, the variable index is incread
}
if (var == 1) { // variable index one is used for base
M1 = M1 + c[i]; // characters are added as a string to represent base valu
}
if (var == 2) { ///ect....
M2 = M2 + c[i];
}
if (var == 3) { // ect...
M3 = M3 + c[i];
}
if (var == 4) { // ect...
M11 = M11 + c[i];
}
if (var == 5) { // ect...
M12 = M12 + c[i];
}
if (var == 6) { // ect...
M13 = M13 + c[i];
}
if (var == 7) { // ect...
M5 = M5 + c[i];
}
if (var == 8) { // ect...
M6 = M6 + c[i];
}
if (var == 9) { // ect...
M7 = M7 + c[i];
}
if (var == 10) { // ect...
Time_To_Complete_Str = Time_To_Complete_Str + c[i];
}
}
/// once these joints are done, variable index are set back to 0, you can add more index for horizontal wrist and gripper
var = 0;
GolamMostafa:
The following sketch accepts and stores the "comma separated data items of this string: SER123,375,123,375,123,375,123,375IAL" into the array named myData[] provided that the preamble ("SER") and the postamble ("IAL") are detected.
void loop()
{
byte n = Serial.available();
{
if (flag == false)
{
if ( n == 3)
{
myStart[0] = Serial.read();
myStart[1] = Serial.read();
myStart[2] = Serial.read();
myStart[3] = 0x00; //NULL byte
if (strcmp(myStart, "SER") == 0) //START pattern is found
{
Serial.println(myStart);
flag = true;
memset(myStart, 0, 4);
}
}
}
else
{
byte m = Serial.readBytesUntil('\n', myData, 34);
//----------------------------------------------
myStop[3] = 0x00;
myStop[2] = myData[m - 1];
myStop[1] = myData[m - 2];
myStop[0] = myData[m - 3];
if (strcmp(myStop, "IAL") == 0)//STOP pattern is found
{
Serial.println(myStop);
memset(myStop, 0, 4);
//-------------------
myData[m] = 0x00;
Serial.println(myData);
memset(myData, 0, 34);
flag = false;
}
else
{
flag = false;
memset(myStop, 0, 4);
memset(myData, 0, 34);
}
}
}
}

Now, codes could be added with the above sketch to extract the comma separated data items using atoi() function.
i have been studieng this.
char * token = strtok(myData, ",");
for (int i = 0; i <= 12; i++) {
myDatas* = token;*
}* would this be the properway to turn mydata into a array?
char * token = strtok(myData, ",");
for (int i = 0; i <= 12; i++) {
myDatas* = token;*
}* would this be the properway to turn mydata into a array? [/quote] I think this will work (not tested). It is based on the parse example in Serial Input Basics ```
* char myData[] = {"123,375,123,375,123,375,123,375"} ;
char * strtokIndx; // this is used by strtok() as an index
int token[8];
strtokIndx = strtok(myData,","); // get the first part
token[0] = atoi(strtokIndx); // convert the first value to an integer and store in token[0]
// then work through the remaining 7 values
for (byte n = 1; n < 8; n++) {
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
token[n] = atoi(strtokIndx);
}*