Pages: [1]   Go Down
Author Topic: Arduino to MATLAB serial limitation workaround  (Read 826 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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;
        }
            } 
      }
}

                             
   
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13670
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
  }
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: