Arduino Uno with Matlab and Stepper

Hey guys!

I use an Arduino Uno with a Rev3 Motor Shield and a bipolar stepper.

So I want to create a program with Matlab in which i can change the stepper speed with a slider and when i click the button matlab give the variable to the arduino which uses it as speed.The speed value is between 1 and 200.I hope that you can help me and find the error :slight_smile: Thanks a lot to everyone who tries!

If I try to use the following code the stepper stops and still have the old speed.

So here is my Matlab code:

A = get(handles.slider1,'Value');
A = int2str(A);
disp(['A= ',A]);
pause on;
s = serial('COM3','BaudRate',9600);

And the Arduino Code

#include <Stepper.h>
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int stepsPerRevolution = 200;
int a;
Stepper myStepper(stepsPerRevolution, 12,13); 
void setup() {
  // put your setup code here, to run once:
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
    int a=Serial.parseInt();

You can't use Serial.parseInt() with that sort of program because it blocks until it receives the data or it timesout and that will interfere with how often myStepper.step() gets called

Use one of the examples in serial input basics - preferrably the 3rd example.

If you use myStepper.step(1) the speed you have set is irrelevant because the motor only moves 1 step.

Either instruct the motor to move several (perhaps hundreds) of steps or control the speed with the interval between subsequent instructions to move 1 step.

The AccelStepper library has more functionality than the stepper library.

Serial.flush() is only used when the Arduino is sending data - it waits until all the data has been sent.

I don't know anything about Matlab.

...R Stepper Motor Basics