Pages: [1]   Go Down
Author Topic: Arduino and Matlab serial interface  (Read 681 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
ok i've set up a serial connection between my arduino uno and matlab, and i works fine
i hooked up my matlab to another serial interface with an lcd to better understand what matlab sends out
Matlab sends out single digits if for example i send out 2015 it send the 2 alone then the 0 then the 1 then the 5

on the arduino side i know arduino converts its serial read to ascii and i know it has a carriage byte at the end

my question is this
how do i reconstruct the number on arduino side ?? i know i need an array and i could use the atoi function but i just don't understand how to do it
so any help would be greatly appreciated
thanks
Logged

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

Quote
i know i need an array and i could use the atoi function but i just don't understand how to do it
Like so:

Code:
char buff[8];
byte indx = 0;

void loop()
{
   while(Serial.available() > 0)
   {
      char ch = Serial.read(); // Get the next character
      if(ch >= '0' && ch <= '9')
      {
        // The character is a digit - 0 to 9
        buff[indx++] = ch; // Store it
        buff[indx] = '\0'; // Append a NULL
     }
     else
     {
        // The character is not a digit
        int val = atoi(buff); // Convert to integer

        // Use the integer...

        indx = 0; // Reset for next time
        buff[indx] = '\0';
     }
  }
}
Logged

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

Thank You SOOOO MUCH seriously thank you :d this has been driving me crazy for a whole day smiley-grin i actually reached smthn similar but couldn't quite perfect it though like this smiley-grin thank youuu again smiley-grin
Logged

Pages: [1]   Go Up
Jump to: