Arduino + Matlab + Serial.read() + Multiple digits

Hello, I am using my Arduino to power a stepper motor and I am using Matlab to interface with the Serial port. I would like to read in integers with multiple digits into the serial port, like 78 or 234. I understand that the Serial.read() function in the Arduino environment reads integers in from the Serial port one at a time and hopefully my code makes up for that:

int value() {
int val [] = { 0, 0, 0 };
int n = 0;
while( Serial.available() > 0 && n <= 2)
//Serial.available() > 0 && n == 2)
{
val[n] = Serial.read();
val[n] -=48;
n++;
}
if (val[0] > 0 && val[1] > 0 && val[2] > 0) {
val[0] *= 100;
val[1] *= 10;
}
else if (val[0] > 0 && val[1] > 0 && val[2] < 0 ) {
val[0] *= 10;
val[2] = 0;
}
else if (val[0] > 0 && val[1] < 0 ) {
val[1] = 0;
val[2] = 0;
}

return new_val;
}

the matlab code looks like:
s = serial('COM3');
data = 15; % Number of times to blink. Currently only 0-9 supported.
fclose(s);
%clear all;

%s = serial('/dev/tty.usbmodemfd111');
s.BaudRate=9600;
s.ReadAsyncMode = 'continuous';
s.BytesAvailableFcnMode = 'byte';
%s.TimerPeriod=1;
%set(s,'Timeout',600);
s.Terminator='CR';
try
fopen(s);
s.BytesAvailable
display('Opened')
fscanf(s);
display('Scanned')
fprintf(s, '%d', data);
% fprintf(s, '*IDN?');

s.BytesAvailable
%fscanf(s, '%d')
%out = fscanf(s,'%d');
display('Sent')

fclose(s);
display('Closed')
catch problem
% fclose(s);
display('Problem Caught')
end

I am running into a very important problem. The stepper motor runs fine when I tell it to rotate 0-9 steps, but when I tell it to run 10 or more steps, it messes up! It turns out it adds up the digits and moves that many times around. I guess this is how Serial.read() works, right? It takes all of the numbers it recieves and adds them up and spits it back out? How do you get it to stop doing that?

I tried on Matlab's end to turn the number into a string, but I ended up with the same result. I also tried different data types with no avail.

How can I fix this problem?

lcr2139:
I guess this is how Serial.read() works, right? It takes all of the numbers it recieves and adds them up and spits it back out? How do you get it to stop doing that?

You can't "stop it from doing that". It only sends 1 character at a time. That's just how serial works.

When posting code, please make use of the ... tags.

lcr2139:

int value() {

int val [] = {
    0, 0, 0  };
  int n = 0;
  while( Serial.available() > 0 && n <= 2)
    //Serial.available() > 0 && n == 2)
  {
    val[n] = Serial.read();
    val[n] -=48;
    n++;
  }
  if (val[0] > 0 && val[1] > 0 && val[2] > 0) {
    val[0] *= 100;
    val[1] *= 10;
  }
  else if (val[0] > 0 && val[1] > 0 && val[2] < 0 ) {
    val[0] *= 10;
    val[2] = 0;
  }
  else if (val[0] > 0 && val[1] < 0 ) {
    val[1] = 0;
    val[2] = 0;
  }

return new_val;
}

Where does "new_val" get assigned anything? What happens if you send the string "100", but the 2nd and third characters (each "0") don't arrive before the while() loop is checked again? You would be better off defining an EOL character (like CR or NL) to let your function know serial is done sending characters.

  else if (val[0] > 0 && val[1] > 0 && val[2] < 0 ) {
        val[0] *= 10;
        val[2] = 0;
  }
  else if (val[0] > 0 && val[1] < 0 ) {

val[ n ] is set to 0. Nothing ever sets it to a negative number. Why are you expecting that val[1] and/or val[2] might be negative?

James provided the right answer. You need some kind of end-of-packet marker to indicate that all the data has been received.

Where does "new_val" get assigned anything? What happens if you send the string "100", but the 2nd and third characters (each "0") don't arrive before the while() loop is checked again? You would be better off defining an EOL character (like CR or NL) to let your function know serial is done sending characters.
[/quote]

this isnt really the issue I care about...what im mainly concerned with is how Serial.read() works with Matlab. I was told it reads in one digit, then the next, then the next, but I found that it actually adds the digits together, then outputs the result. How can I rearrange or add to the code so I can get it to read the numbers in correctly?

...what im mainly concerned with is how Serial.read() works with Matlab. I was told it reads in one digit, then the next, then the next, but I found that it actually adds the digits together, then outputs the result. How can I rearrange or add to the code so I can get it to read the numbers in correctly?

For matlab questions, you can search the forum for previous matlab discussions, or maybe a matlab forum. The arduino doesn't really care what application is sending to its serial input. Below is simple code that captures an incomming string of characters and converts the captured string into a number for use in controlling a servo. Might be something similar to what you want to do.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

For matlab questions, you can search the forum for previous matlab discussions, or maybe a matlab forum. The arduino doesn't really care what application is sending to its serial input. Below is simple code that captures an incomming string of characters and converts the captured string into a number for use in controlling a servo. Might be something similar to what you want to do.

[/quote]

this isnt a matlab question because it has to do with using an arduino command. the matlab command is fine. my questions are about the arduino command.