More MATLAB Trouble

Anymore cross-posting and you will get a time-out.

Try to use some simply code at first, such as using MATLAB to control a LED through Arduino.

      input_line[input_pos] = input_line[input_pos] + inByte;

This is novel. I don't think I've ever seen anyone ADD the byte read to the existing value in the array.

char input_line [50];

It's a local variable, so it is not initialized to anything meaningful, and yet you add the new data to the existing garbage in the array. Like I said, novel.

    char inByte = Serial.read ();
    while(inByte != '\n')
    {
      input_line[input_pos] = input_line[input_pos] + inByte;
      if (inByte ==',')
      input_pos++;
    }

Suppose you read an 'A' from the serial port. That's not a '\n', so you enter the while loop. What will allow you to exit that loop? As written, as soon as you read something other than a carriage return from the serial port, you enter an infinite loop.

The reason for having a while loop there completely escapes me. An if statement would make sense. A while statement does not.

Assuming that you change the while to if, you'll have this:

  while (Serial.available () > 0) 
    {
    char inByte = Serial.read ();
    // Do something with the character
    Serial.print(input_line[1]);
 }

So, as long as there is serial data available, print the character in the second position of the array.

Exactly how useful is that?

The output from the serial port is
Arduino heard you saying I am not getting any values

And, now you know why.

Sorry for cross posting.

thanks Pauls for replying. Well so do you mean that this novel won't work " input_line[input_pos] = input_line[input_pos] + inByte;"
whats wrong with it? I am adding the incoming ascii character to a string stored in an array instead of adding it to an array of characters.
I tried the other way I received the charaters and stored it to a char array. then in another method I parsed the character array to strings. It worked but the code seems too complicated to use. This one is much simpler

Well so do you mean that this novel won't work

Yes. I mean that there is a big difference between

input_line[input_pos] = input_line[input_pos] + inByte;

and

input_line[input_pos] = inByte;

I am adding the incoming ascii character to a string stored in an array instead of adding it to an array of characters.

input_line is an array of characters, not an array of strings.

This one is much simpler

But it's complete nonsense.

Ok I will keep then the complex way but I need help too
how to pass three obtained Strings from my parse_data function to the loop is my main concern
then how to change those string values to double values

my code is :

void setup ()
{
  Serial.begin(9600);
 
   
  
} /////////////////////////////////////////////////////////////////////////////end of setup

void process_data (char * data)
  {
  // for now just display it
  Serial.println();
  Serial.println (data);
  delay(1000);
  }  // end of process_data
/////////////////////////////////////////////////////////////////////////process data serially   
void parse_data(char * data)
{
  int s = 1;
      int j = 0;
      char value[15];
      for(int k =0 ; k<15;k++){value[k]=0;}
      String servo;
      String pwm;
      String pressure;
      
      for(int i = 0; i <100;i++)
      {
        if(data[i] == ',')
        {
           switch(s){
             case 1:
               servo = value;
               for(int k =0 ; k<15;k++){value[k]=0;}
               s = 2;
               break;
             case 2:
              pwm = value;
                for(int k =0 ; k<15;k++){value[k]=0;}
              s = 3;
               break;
             case 3:
              pressure = value;
              for(int k =0 ; k<15;k++){value[k]=0;}
              s= 4;
              break;  
           }
          j = 0;  
        }
        else{
    
          value[j] = data[i];
          j++;
        }
        
       
      }
      
    
     Serial.println(); 
     Serial.print(servo);
     Serial.println();
     Serial.print(pwm);
     Serial.println();
     Serial.print(pressure);
     
     delay(1000); 
      
}
  

void loop()
{
  int i= 0;
static char input_line [50];
char datas[10];

static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        parse_data(input_line);
       
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
     
      
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (50))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch
      
  }  // end of incoming data
  ////////////////////////////////////////////////////////////////////////////////////////////string received and saved to input_line
   ////////////////////////////////////////////////////////////////////////////////////////Parsing the string'

Here is the output I got through the serial port
7.9669,60,40.6355, (this is the array of characters obtained from the serial port)

7.9669 (these are each string parsed and saved to a variable) I need to pass these to the loop
60
40.6355

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I appreciate all the help from you guys especially Pauls

Several comments. First, you are collecting the data into a char array. Then, you are wrapping that char array in a String. Why?

There is no reason for hiding the character data in a String object, especially as the atof() function has no idea how to deal with a String.

Second, it is clear that you do not understand the relationship between a char array and a string (with a lower case s). Functions like atof() and the String constructor expect strings. A string is a NULL terminated array of chars. value is an array of chars, but it is not a string, because it is not NULL terminated.

Until you NULL terminate the array of chars, you can not pass the char array to functions like atof() or the String constructor and expect correct things to happen.

If you did understand this relationship, you would know that functions that expect strings stop reading the character data at the NULL, so

               for(int k =0 ; k<15;k++){value[k]=0;}

is unnecessary. Simply set value[0] to NULL ('\0').

Your parse_data() function declaration should look something like this:

void parse_data(char *data, float &servo, float &pwm, float &pressure)
{
}

Paul for(int k =0 ; k<15;k++){value[k]=0;} this is to reset all the char in the array so when the next value is read old values doesn't exist. I tried setting the first value of the char array to 0 it didn't work i got the second number continued by the first number.
I was thinking of using strtod() to convert strings to double and then use it.
The code seems to complex to me so I switched to the one I started with i am working on both now.
I have a great improvement on the first one and my novel worked!

String input_line [50];


 int input_pos = -1;
 int x = 0;
if (Serial.available() >0){
      Serial.print("Arduino heard you say: ");

  while ((Serial.available () > 0)&& ( x!= 1)) 
    {
    char inByte = Serial.read ();
    switch (inByte)
      {
      case '\n':   // end of text
       x= 1;
        // reset buffer for next time
        break;
      
      case ',':   
      input_pos++;
        break;
  
      default:
          input_line[input_pos] = input_line[input_pos] + inByte;
        break;

      }  // end of switch
   }
 }
if(x ==1)
{
 Serial.println();
 Serial.print(input_line[0]);
 Serial.println();
 Serial.print(input_line[1]);
 Serial.println();
 Serial.print(input_line[2]);
 Serial.println();

though I am getting the last 2 values only the first one is not there here is the output

Arduino heard you say: Arduino heard you say: Arduino heard you say: Arduino heard you say:

60
40.6355
Arduino heard you say:

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I tried setting the first value of the char array to 0 it didn't work i got the second number continued by the first number.

Because you are not adding data to the array correctly.

This is an example of how it should be done:

char inData[15];
byte index = 0;

inData[index] = '\0';

while(Serial.available() > 0)
{
   char aChar = Serial.read();
   if(aChar == ",")
   {
      // Use the index characters in inData

      // Get ready for next value
      index = 0;
      inData[index] = '\0';
   }
   else
   {
      inData[index++] = aChar; // Add the char to the array
      inData[index}; // Add a NULL after that character
   }
}
String input_line [50];

Wow. Talk about a sledge-hammer solution...

I changed the code to this and still i am not getting the values.

void setup ()
{
  Serial.begin(9600);
 
   
  
} /////////////////////////////////////////////////////////////////////////////end of setup
////////////////////////////////////////////////////////////////////////////// here to process incoming serial data after a terminator received [/c]

  void loop()
{

char inData[15];
int index,i = 0;
char servo[15], pwm[15], pressure[15];
servo[0], pwm[0], pressure[0] = '\0';

inData[i] = '\0';
int x = 0;

while((Serial.available() > 0) && (x !=1))  
{
   char aChar = Serial.read();
   if(aChar == ',')
   {
       switch (index){
        case 0:
        for (int k =0; k <15; k++){servo[k] = inData[k];}
        index =1;
        inData[0] = '\0';
        i =0;
        case 1:
        for (int k =0; k <15; k++){pwm[k] = inData[k];}
        index = 2;
        inData[0] = '\0';
        i =0;
        case 2:
        for (int k =0; k <15; k++){servo[k] = inData[k];}
        inData[0] ='0';
        index =3;
        i = 0;
       }
        if (aChar == '\n')
       {
         x = 1;
       }
   }
   
   else
   {
   inData[i++] = aChar; // Add the char to the array
   
   }
if(index ==3)
{
 Serial.print("I passed here: ");

 Serial.println();
 String xl= servo;
 Serial.print(xl);
 
 Serial.println();
 Serial.print(pwm);
 Serial.println();
 Serial.print(pressure);
 Serial.println();
 index++;
}  
   
   
   
}
servo[0], pwm[0], pressure[0] = '\0';

Tell me what you think this is doing?

int index

The variable index is a local variable, with no initial value. So, it contains some random garbage.

       switch (index){

So, you want to switch based on random garbage?

   if(aChar == ',')
   {
<snip>
        if (aChar == '\n')
       {
         x = 1;
       }
   }

So, under what circumstances is the value in aChar going to morph from a command to a carriage return?

PaulS:

servo[0], pwm[0], pressure[0] = '\0';

Tell me what you think this is doing?

I think it is setting the character array to null so no garbage is in it.

int index = 0;
its initial value is 0 not garbage I am switching from 0 to 1 thats what is written in there.

nt index = 0;
its initial value is 0 not garbage I am switching from 0 to 1 thats what is written in there.

Where? Here?

int index,i = 0;

That is creating two variables, index and i, and assigning i an initial value of 0. index does NOT get assigned an initial value.

I think it is setting the character array to null so no garbage is in it.

No. It is assigning only pressure[0] a value.

Comma does NOT mean "do this to all these variables...".

Still doesn't working Pauls. I ve done all the things I am really desparate
the output is : I passed here

void setup ()
{
  Serial.begin(9600);
 
   
  
} /////////////////////////////////////////////////////////////////////////////end of setup
////////////////////////////////////////////////////////////////////////////// here to process incoming serial data after a terminator received

 
  void loop()
{
char inData[15];
inData[0] ='\0';

int index = 0;
int i =0;

char servo[15], pwm[15], pressure[15];
servo[0] = '\0';
pwm[0] = '\0';
pressure[0] = '\0';
int x = 0;


while((Serial.available() > 0) && (x== 0))  
{
   char aChar = Serial.read();


   if (aChar == '\n'){x = 1;}
   if(aChar == ',')
   {
       switch (index){
        case 0: for (int k =0; k <15; k++){servo[k] = inData[k];}
                index =1;
                inData[0] = '\0';
                i =0;
        case 1: for (int k =0; k <15; k++){pwm[k] = inData[k];}
                index = 2;
                inData[0] = '\0';
                i =0;
        case 2: for (int k =0; k <15; k++){servo[k] = inData[k];}
                inData[0] ='\0';
                index =3;
                i = 0;
        default:  inData[0] = '\0';
                  i = 0;        
       }
       
   }
   
   else
   {
   inData[i++] = aChar; // Add the char to the array
   }
if(x == 1)
{
 Serial.print("I passed here: ");
 Serial.println();
 Serial.print(servo);
 Serial.println();
 Serial.print(pwm);
 Serial.println();
 Serial.print(pressure);
 Serial.println();
}  
   
   
   
}
 
}

[\code]
   char aChar = Serial.read();
Serial.print("Hey, lookee here. I got a [");
Serial.print(aChar);
Serial.println("]");

should give you whole lot more output, and a clue.

Before posting any more code, PLEASE!!! use Tools + Auto Format. I'm tired of guess which } goes with which {.

Here is the code with better Format. it is receiving the characters as needed when I tried print serial

void setup ()
{
  Serial.begin(9600);



} /////////////////////////////////////////////////////////////////////////////end of setup
/////////////////////////////////////////////////// here to process incoming serial data after a terminator received



void loop()
{
  char inData[15];
  inData[0] ='\0';

  int index = 0;
  int i =0;

  char servo[15], pwm[15], pressure[15];
  servo[0] = '\0';
  pwm[0] = '\0';
  pressure[0] = '\0';
  int x = 0;


  while((Serial.available() > 0) && (x== 0))  
  {
    char aChar = Serial.read();
    Serial.print(aChar);

    if (aChar == '\n'){
      x = 1;
    }
    if(aChar == ',')
    {
      switch (index){
      case 0: 
        for (int k =0; k <15; k++){
          servo[k] = inData[k];
        }
        index =1;
        inData[0] = '\0';
        i =0;
      case 1: 
        for (int k =0; k <15; k++){
          pwm[k] = inData[k];
        }
        index = 2;
        inData[0] = '\0';
        i =0;
      case 2: 
        for (int k =0; k <15; k++){
          servo[k] = inData[k];
        }
        inData[0] ='\0';
        index =3;
        i = 0;
      default:  
        inData[0] = '\0';
        i = 0;        
      }

    }

    else
    {
      inData[i++] = aChar; // Add the char to the array
    }
    if(x == 1)
    {
      Serial.print("I passed here: ");
      Serial.println();
      Serial.print(servo);
      Serial.println();
      Serial.print(pwm);
      Serial.println();
      Serial.print(pressure);
      Serial.println();
    }  



  }

}

[\code]
the output is

7.9669,60,40.6355
I passed here: 




I passed here:

it is receiving the characters as needed when I tried print serial

And? What are you seeing?

I can receive the characters but its not separating them in servo, pwm and pressure I knew it received them
I am seeing
[Here look at this: characters]
I passed here
"the values should be separated here but nothing
"second value"
"third value"
I passed here