Go Down

Topic: Arduino and Matlab serial interface (Read 722 times) previous topic - next topic

FadiMoussa

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

PaulS

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: [Select]
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';
     }
  }
}

FadiMoussa

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

Go Up