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 :) 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);
pause(3);
fopen(s);
pause(2);
fprintf(s,A,'uint8','sync');
pause(2);
fclose(s);
and here is my 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:
myStepper.setSpeed(10);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0)
{
//int a=Serial.read();
int a=Serial.parseInt();
myStepper.setSpeed(a);
Serial.flush();
}
myStepper.step(1);
}