Go Down

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

PaulS

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

fayadAli

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

PaulS

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

fayadAli

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

PaulS

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

And? What are you seeing?

Go Up