Go Down

Topic: More MATLAB Trouble (Read 1 time) previous topic - next topic

fayadAli

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("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]

PaulS

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

fayadAli

#27
Mar 31, 2012, 01:15 am Last 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


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

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

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

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

fayadAli


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

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