Pages: [1] 2 3   Go Down
Author Topic: More MATLAB Trouble  (Read 1447 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello i am new to this forums and I couldn't post a new post So I am asking my question here
I wrote this code to get the 3 string values ( I will use strtod later) reading serially from matlab but I am not sure know why its not working.
The output from the serial port is
Arduino heard you saying I am not getting any values

here is the code

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

  void loop()
{

char input_line [50];

char datas[10];

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

  while (Serial.available () > 0)
    {
    char inByte = Serial.read ();
    while(inByte != '\n')
    {
      input_line[input_pos] = input_line[input_pos] + inByte;
      if (inByte ==',')
      input_pos++;
    }
    Serial.print(input_line[1]);
 }
 Serial.print(input_line[1]);
 Serial.println();
 Serial.print("I reached here: ");
 Serial.print(input_line[0]);
 Serial.println();
 Serial.print(input_line[2]);
}
}


Moderator edit: [code] [/code] tags added.
« Last Edit: March 29, 2012, 03:01:27 pm by fayadAli » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Anymore cross-posting and you will get a time-out.
« Last Edit: March 29, 2012, 03:18:58 am by Coding Badly » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      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.

Code:
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.

Code:
    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:
Code:
  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?

Quote
The output from the serial port is
Arduino heard you saying I am not getting any values
And, now you know why.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well so do you mean that this novel won't work
Yes. I mean that there is a big difference between
Code:
input_line[input_pos] = input_line[input_pos] + inByte;
and
Code:
input_line[input_pos] = inByte;

Quote
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.

Quote
This one is much simpler
But it's complete nonsense.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

Code:
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: [code] [/code] tags added.
« Last Edit: March 29, 2012, 01:04:14 pm by Coding Badly » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I appreciate all the help from you guys especially Pauls
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
               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:
Code:
void parse_data(char *data, float &servo, float &pwm, float &pressure)
{
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Code:
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: [code] [/code] tags added.
« Last Edit: March 29, 2012, 01:05:04 pm by Coding Badly » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
Code:
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
   }
}

Code:
String input_line [50];
Wow. Talk about a sledge-hammer solution...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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++;
}  
  
  
  
}
« Last Edit: March 29, 2012, 03:02:32 pm by fayadAli » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
servo[0], pwm[0], pressure[0] = '\0';
Tell me what you think this is doing?

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

Code:
       switch (index){
So, you want to switch based on random garbage?

Code:
   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?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: