Sending large numbers via serial to Arduino

Hi there,

I’m trying to send some fairly large intergers (1000+) to my arduino from MATLAB to drive a stepper motor, but it doesn’t seem to be working. I’m pretty sure it’s because of the way the data is being send.

This is what I’ve got so far on the sending end(MATLAB):

%Initial values
angle = 1;
steps = 0;

%connection to the Arduino
arduino=serial(‘COM3’,‘BaudRate’,9600); % create serial communication object on port COM3
fopen(arduino); % initiate arduino communication

%Sending the amount of steps to take
while angle
angle=input(‘Angle = ‘); % ask user to enter value for stepper angle
steps = round(motorstepsgearratioangle/360); % calculating the amount of steps
fprintf(arduino,’%d’,steps); % send the amount of steps to take to arduino
end

fclose(arduino);

And this is what I’ve got on the receiving end:

#include <Stepper.h>
#include <stdio.h>
#include <stdlib.h>

int matlabData=0;
Stepper stepper(32, 8, 10, 9, 11);

void setup()
{
Serial.begin(9600);
stepper.setSpeed(200);

}

/* This rotates the stepper based on the input from MATLAB */

void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
stepper.step(matlabData);
}
}

Now I’m pretty sure the problem is in the ‘fprintf’ part on matlab, or the Serial.read() in the arduino code. It seems to work just fine for smaller intergers, but not for larger of negative ones.

Thanks in advance

Serial.read() takes in a byte at a time.

Did you look at Serial.parseInt()?

Oh wow, that worked! I didn't think it would be that easy. Thanks!