Splitting a Serial Incoming Data Into 3 Parts -Problem

Hey Guys . I am working on a code that receives Serial Data from an Orientation Sensor connected to arduino on Proteus 8. I receive data from my mobile phone by bluetooth then the data is received by a virtual terminal on proteus. I receive data like this :

3,47,62.10675,2.5695722,-0.33250946>3,48,62.108093,2.5753582,-0.3276943>3,49,62.100525,2.557956,-0.3256003

My code receives serial data like this for example : >3,47,62.10675,2.5695722,-0.33250946 ( values of Orientation sensor)
Then the code omits any un-useful data , so my data becomes : 62.10675,2.5695722,-0.33250946
Then my code splits these data by a delimiter (,) into 3 values to become :
Value1= 62.10675 Value2 = 2.5695722 Value3 = -0.33250946

My code works fine. Then after few seconds ( about 25 seconds ) , the code stops working and proteus shows the following error :

PC=0x0590. [AVR MEMORY] External Memory Write while interface is not enabled (SRE=0): [0xDB6C]=00. [ARD1]

Any help is appreciated ...

Is there a Proteus forum? How is this an Arduino question?

Electronics,Simulation and Programming is a related domain.
This is an arduino code , maybe the code is bad so that it makes and error

Ahmad_LongJump:
This is an arduino code , maybe the code is bad so that it makes and error

Could be. But, since nobody can see your code, what exactly do you expect from the forum?

Sorry guys I forget to add the code : 

//This library is used to split the sensor values into x,y,z
#include "StringSplitter.h"

// Values of Orientation Sensor x,y,z
String value1;
String value2;
String value3;
String inByte;
String TrimedValue;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
 //int inByte = Serial1.read();
 String inByte = Serial1.readStringUntil('>');
 //Serial.println(inByte + "Hello"); } 
 TrimedValue=inByte.substring(6); 
 SplitingFxn (TrimedValue);
Serial.println(value1 + " " + value2 + " " + value3); //Serial1.flush();    
}
}

void SplitingFxn ( String input ) {
//String input1 = "A,B,C";//">3,45,333.25555,-29.234741,9.385975" ;
String strTest = input;
StringSplitter *splitter = new StringSplitter(strTest, ',', 3);  // new StringSplitter(string_to_split, delimiter, limit)
int itemCount = splitter->getItemCount();
//Serial.println("Item count: " + String(itemCount));

for(int i = 0; i < itemCount; i++){
 String item = splitter->getItemAtIndex(i);
 if (i==0) { value1=item;}
 else if (i==1) {value2=item;}
 else if (i==2) {value3=item;}    }

}

You also forgot to read How to use this forum - please read, specifically point 7 :wink:

Anyway, don't use String (capital S) and learn to use character arrays. Strings (capital S) can leave holes in your memory that can hide an elephant :smiley: Maybe that is what Proteus is complaining about.

Consider reading Robin's Serial Input Basics thread to help you on the way using a different approach.

// PS
It's far better to use a real Arduino instead of a similator.

Thank you ..

Hello guys , I am trying to get Orientation sensor values(X,Y,Z) as serial data on arduino.
I need the values and store them in variables like :
Value1= X ; Value2= Y ; Value3 = Z
My code works fine in showing the values on serial monitor ( Serial_Println) but the problem is that I can't store them in variables.
My sensor values looks like this : "221.55025,1.1260997,-0.16243964"

here is my code :

void setup() {
   //char input[] = ">3,150,221.55025,1.1260997,-0.16243964";
   char input[] = "221.55025,1.1260997,-0.16243964";
   char separator[] = ",";
   char *token;

  
   Serial.begin(9600);
   /* get the first token */
   token = strtok(input, separator);
   
  // Find any more?
   while(token != NULL) 
   {
      Serial.println(token );   
      token = strtok(NULL, separator);
   }
   Serial.println(input);  // Proof that original string is chopped up
   
}
void loop () {}

Any help is appreciated , best regards

Ahmad_LongJump:
Any help is appreciated , best regards

Check out Serial Input Basics, but here is a functional approach to your problem:

const size_t MAX_MESSAGE_LENGTH = 64;

template <class T> inline Print& operator <<(Print& obj, T arg)
{
  obj.print(arg);
  return obj;
}

struct Coordinates{
  float x;
  float y;
  float z;
};

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Serial.println("let's go!");
}

void loop() 
{
  if(const char* newMessage = checkForNewMessage(Serial, '\n'))
  {
    Serial << (F("Just Recieved:\t")) << (newMessage) << (F("\n"));
    // "221.55025,1.1260997,-0.16243964"
    char currentMssg[strlen(newMessage) + 1];
    strcpy(currentMssg, newMessage);
    Coordinates newDirection;
    newDirection.x = atof(strtok(currentMssg, ","));
    newDirection.y = atof(strtok(NULL, ","));
    newDirection.z = atof(strtok(NULL, ","));

    Serial << (F("X=")) << newDirection.x << (F("\n"));
    Serial << (F("Y=")) << newDirection.y << (F("\n"));
    Serial << (F("Z=")) << newDirection.z << (F("\n"));
  }
}

const char* checkForNewMessage(Stream& stream, const char endMarker)
{
  static char incomingMessage[MAX_MESSAGE_LENGTH] = "";
  static byte idx = 0;
  if(stream.available())
  {
    incomingMessage[idx] = stream.read();
    if(incomingMessage[idx] == endMarker)
    {
      incomingMessage[idx] = '\0';
      idx = 0;
      return incomingMessage;
    }
    else
    {
      idx++;
      if(idx > MAX_MESSAGE_LENGTH - 1)
      {
        //stream.print(F("{\"error\":\"message too long\"}\n"));  //you can send an error to sender here
        idx = 0;
        incomingMessage[idx] = '\0';
      }
    }
  }
  return nullptr;
}

All the accelerometers I have played with are I2C rather than serial?

Ahmad_LongJump:
Thank you ..

Thanks for adding code tags.

one you have a token as a string you can use atof() to convert it to a float, e.g.

void setup() {
   //char input[] = ">3,150,221.55025,1.1260997,-0.16243964";
   char input[] = "221.55025,1.1260997,-0.16243964";
   char separator[] = ",";
   char *token;
   float data[10]={0};
   int dataIndex=0;

  
   Serial.begin(9600);
   /* get the first token */
   token = strtok(input, separator);
  
  // Find any more?
   while(token != NULL)
   {
      Serial.println(token ); 
      data[dataIndex++]=atof(token);   // to float variable
      token = strtok(NULL, separator);
   }
   Serial.println(input);  // Proof that original string is chopped up
   for(int i=0;i<dataIndex;i++)
     { Serial.print("float value = "); Serial.println(data[i],6); }
  
}
void loop () {}

gives

221.55025
1.1260997
-0.16243964
221.55025
float value = 221.550247
float value = 1.126100
float value = -0.162440

reminiscent of this post

@Ahmad_LongJump, isn't this essentially the same problem as in your other Thread?

If so why have you started a new Thread? You have certainly been given the same advice twice which is wasting someone's time.

I am not going to waste time reading two Threads trying to figure out what information is in one that is pertinent to the other. I suggest you click Report to Moderator and ask to have them merged.

...R

Threads merged.

Thank for all who have helped ... I will revise the replies and then inform you with the results.
For who are asking why I asked the same problem twice , because in the first thread the code was somewhat complicated and it included a library that I didn't talk about.
So I started working on a simpler code , which was embaded in the second post.
I didnt meant to waste others time , as they are helping me for nothing.
Thank you all

Thank you guys , the code now works fine …