Pages: 1 [2] 3   Go Down
Author Topic: More MATLAB Trouble  (Read 1521 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

 

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 {.
Logged

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

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

Code:

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it is receiving the characters as needed when I tried print serial
And? What are you seeing?
Logged

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

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 
Logged

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

Here is the output after I try it

Hey, lookee here. I got a [7]
Hey, lookee here. I got a [.]
Hey, lookee here. I got a [9]
Hey, lookee here. I got a [6]
Hey, lookee here. I got a [6]
Hey, lookee here. I got a [9]
Hey, lookee here. I got a [,]
Hey, lookee here. I got a [6]
Hey, lookee here. I got a
Hey, lookee here. I got a [,]
Hey, lookee here. I got a [4]
Hey, lookee here. I got a
Hey, lookee here. I got a [.]
Hey, lookee here. I got a [6]
Hey, lookee here. I got a [3]
Hey, lookee here. I got a [5]
Hey, lookee here. I got a [5]
Hey, lookee here. I got a [
]
I passed here:



Hey, lookee here. I got a [
]
I passed here:
Logged

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

I tried printing right after each variable I got this
I am here9I am here96I am here966I am here9669I am here9669«I am here9669«I am here9669««I am here9669««I am here9669««I am here9669««I am here9669««I am here9669««I am here9669««I am here9669««I am here9669««This is PWMThis is PWMThis is PWMThis is
The servo starts to save the values after the second charachter received. PWM and pressure never get assigned any values
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here is the output after I try it
OK. So, we have learned a great deal from this. We know that data is properly being sent to the Arduino, and we know that the Arduino is properly reading the data. We can see that the input stream does contain commas and a carriage return and a line feed.

Quote
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 
So, you made a code change to learn this. I can't see what the code looks like, now.

Quote
I tried printing right after each variable I got this
More changes. No code posted.

Please post the code you have now. You are very close to getting this to work.
Logged

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

Code:
char servo[15], pwm[15], pressure[15];
void setup ()
{
  Serial.begin(9600);
  
  servo[0] = '\0';
  pwm[0] = '\0';
  pressure[0] = '\0';



} /////////////////////////////////////////////////////////////////////////////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;
  
  int x = 0;


  while((Serial.available() > 0) && (x== 0))  
  {
    char aChar = Serial.read();
    Serial.print("Hey, lookee here. I got a [");
    Serial.print(aChar);
    Serial.println("]");

    if (aChar == '\n'){
      x = 1;
    }
    if(aChar == ',')
    {
      switch (index){
      case 0:
        for (int k =0; k <15; k++){ ///copy indata to servo
          servo[k] = inData[k];
          Serial.print("I am here");
          Serial.print(servo);
        }
        index =1;
        inData[0] = '\0';
        i =0;
      case 1:
        for (int k =0; k <15; k++){
          pwm[k] = inData[k];
           Serial.print("This is PWM");
          Serial.print(pwm);
        }
        index = 2;
        inData[0] = '\0';
        i =0;
      case 2:
        for (int k =0; k <15; k++){
          servo[k] = inData[k];
            Serial.print(" Pressure");
          Serial.print(pwm);
        }
        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]
« Last Edit: March 30, 2012, 05:30:43 pm by fayadAli » Logged

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

Code:
I gave up on that and I am using
This one it works fine and I am able to receive and separate the values. all I need to now is how to pass the float values to the void loop
I am trying to put them in a float array and return that but I couldn't
[code]
float servoInt = 0;
  float pwmInt = 0;
  float pressureInt =0;
void setup ()
{
  Serial.begin(9600);
}

void process_data (char * data)
{

  Serial.println();
  Serial.println (data);
  delay(1000);
}    
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;
        servoInt = atof(value);
          Serial.print("servoInt is:");
          Serial.print(servoInt);
        for(int k =0 ; k<15;k++){
          value[k]=0;
        }
        s = 2;
        break;
      case 2:
      pwmInt = atof(value);
      Serial.print(" the actuator angle received in float is ");
      Serial.print(pwmInt);
        pwm = value;
        for(int k =0 ; k<15;k++){
          value[k]=0;
        }
        s = 3;
        break;
      case 3:
        pressureInt = atof(value);
    
        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("I got the final values");
  Serial.println();
  Serial.print("Servo Angle:");
  Serial.print(servoInt);
  Serial.println();
  Serial.print(" the actuator angle received in float is: ");
  Serial.print(pwmInt);
  Serial.println();
  Serial.print("the presssure received:");
  Serial.print(pressureInt);

  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':  
      input_line [input_pos] = 0;  


      process_data (input_line);
      parse_data(input_line);


      input_pos = 0;  
      break;

    case '\r':  


      break;

    default:

      if (input_pos < (50))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  }
}
[\code]

[/code]
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      pwmInt = atof(value);
What? Perhaps you need to try
int pwmFloat = some nonsense.
Logged

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

Do you think this is better.
I am thinking of changing the whole thing to this algorithm
send firstValue , SecondValue;ThirdValue: /n


read serial
switch inByte
case ,
firstValue = atof(arraychar);
reset (arraychar);

case ';' :
secondValue = atof(arraychar);
reset value;

case ':' :
thirdValue = atof(arrayChar);

default:
inData[pos++] += inByte;
« Last Edit: March 31, 2012, 01:48:29 am by fayadAli » Logged

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

Code:
      pwmInt = atof(value);
What? Perhaps you need to try
int pwmFloat = some nonsense.

Well I was using Int earlier thats why the name is pwmInt

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well I was using Int earlier thats why the name is pwmInt
It's not an int now. You can, and should, change the name.
Logged

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