Arduino Uno with Matlab and Stepper

2 次查看(过去 30 天)
Sven Lo
Sven Lo 2015-7-26
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);
}
  1 个评论
Geoff Hayes
Geoff Hayes 2015-7-26
Sven - shouldn't the format for the data come before the command/variable (unless you are using a different version of MATLAB)? If I look at http://www.mathworks.com/help/matlab/ref/serial.fprintf.html then I tend to think that your call
fprintf(s,A,'uint8','sync');
is a little off and should have the data type (format) before the A variable. I may even do something like
fprintf(s,'%u',A,'sync');

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by