Go Down

Topic: Arduino to MATLAB serial limitation workaround (Read 860 times) previous topic - next topic

sachin-nayak

I'm working on a setup to sense LED on/off changes and send this info to MATLAB as a bit stream. Here I have a problem if the number of on/off's are much more than 64 and i'm getting only 64 bytes in MATLAB. This might be due to the fact that the serial buffer in Arduino is only 64bytes long. Is there any way to tackle this problem?
P.s: I tried flushing the serial buffer after every 60 transmissions to avoid the problem. It didn't work. :smiley-roll:. Any solutions?

PaulS

Quote
Here I have a problem if the number of on/off's are much more than 64 and i'm getting only 64 bytes in MATLAB.

We probably need to see your code.

Quote
P.s: I tried flushing the serial buffer after every 60 transmissions to avoid the problem. It didn't work.

If you are using Arduino 1.0, any attempt to overfill the buffer will result in blocking until there is room in the buffer. Explicitly blocking until the buffer is empty is not necessary.

sachin-nayak

int st;
int count=0;
void setup()
{
  pinMode(A0,INPUT);   //input from sensor

   Serial.begin(9600);     
}


void loop()
{
       while(Serial.available() == 0)
     {
       ;
     }                                       
     
      int num= Serial.read();                     

 
      if (num == '1') // sending '1' from matlab to initiate reception                         
      {
               
       if(analogRead(A0)>800)
       {
         st=1;
        count=count+1;  // setting count to overwrite 64 byte problem
       }
         else
         {
           st=0;
         count=count+1;
       }
      if(count<60)
{     
 
  Serial.write(st);
      delay(100);
}
else
{
Serial.flush();
count=0;
        }
            } 
      }
}

                             
 

robtillaart

please use the CTRL-T in the IDE to auto indent the code to make it more readable
and please, use the # button to get code tags around it.

Why are you communicating at 9600 baud?   Arduino can do 115200  (12x as fast!) keeping the buffers quite empty.

here a variation of your code that sends the value of analogRead /4 to mathlab so mathlab can check what the value was.
It uses a higher baudrate.

Code: [Select]

void setup()
{
  Serial.begin(115200);     
}

void loop()
{
  while(Serial.available() == 0);  // wait for mathlab
  int num = Serial.read();                     
  if (num == '1') // sending '1' from matlab to initiate reception                         
  {
    int value = analogRead(A0)/4;
    Serial.write(value);
    delay(5);
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up