Go Down

### Topic: More MATLAB Trouble (Read 2504 times)previous topic - next topic

#### PaulS

#15
##### Mar 29, 2012, 11:56 pm
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: [Select]
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...".

#16
##### Mar 30, 2012, 12:18 am
Still doesn't working Pauls. I ve done all the things I am really desparate
the output is : I passed here

Code: [Select]

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))
{

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]

#### PaulS

#17
##### Mar 30, 2012, 12:54 am
Code: [Select]
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 {.

#18
##### Mar 30, 2012, 01:09 am
Here is the code with better Format. it is receiving the characters as needed when I tried print serial

Code: [Select]

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))
{
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:

#### PaulS

#19
##### Mar 30, 2012, 01:12 am
Quote
it is receiving the characters as needed when I tried print serial

And? What are you seeing?

#20
##### Mar 30, 2012, 02:39 am
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

#21
##### Mar 30, 2012, 05:58 am
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:

#22
##### Mar 30, 2012, 06:44 am
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

#### PaulS

#23
##### Mar 30, 2012, 11:12 pm
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.

#24
##### Mar 31, 2012, 12:28 amLast Edit: Mar 31, 2012, 12:30 am by fayadAli Reason: 1
Code: [Select]

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))
{
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]

#25
##### Mar 31, 2012, 12:31 am
Code: [Select]
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(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)
{

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]

#### PaulS

#26
##### Mar 31, 2012, 01:03 am
Code: [Select]
pwmInt = atof(value);
What? Perhaps you need to try
int pwmFloat = some nonsense.

#27
##### Mar 31, 2012, 01:15 amLast Edit: Mar 31, 2012, 08:48 am by fayadAli Reason: 1
Do you think this is better.
I am thinking of changing the whole thing to this algorithm
send firstValue , SecondValue;ThirdValue: /n

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

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

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

default:
inData[pos++] += inByte;

#28
##### Mar 31, 2012, 01:17 am

Code: [Select]
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

#### PaulS

#29
##### Mar 31, 2012, 01:51 am
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.

Go Up

Please enter a valid email to subscribe