Serial Connection between matlab and arduino for Stepper Motor

7 次查看(过去 30 天)
I try to connect matlab to arduino where later I will design GUI on matlab to control arduino code that controls a Stepper Motor. But now I have a problem. I send from the matlab 3 variables (speed , angle , direction) but the arduino only reads the "angle" variable and the other two variables gives always 0 (when they are sent to arduino through serial connection) not the values I intially sent from matlab. It gives zero after the conversion from string to int in Arduino. I don't understand where is the problem. I put the values in string and then convertnot int bec. it gives me error also.
MATLAB Code:
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
clc;
clear;
close all;
%establish connection
s = serial('COM4', 'BaudRate', 115200);
fopen(s);
pause(3);
fprintf("Connection established\n")
%Calculate data
angle="360";
dir="1";
speed="2";
%send data to arduino
fprintf(s, "%s\n", angle);
fprintf(s, "%s\n", dir);
fprintf(s, "%s\n", speed);
pause(3);
out1 = fscanf(s, "%d\n");
pause(0.3);
out2 = fscanf(s, '%d\n');
pause(0.3);
out3 = fscanf(s, '%d\n');
Arduino Code :
#define dirPin 2
#define stepPin 3
String angle , dir, speed1;
void setup()
{
Serial.begin(115200);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
// read data
angle=Serial.readString();
dir=Serial.readString();
speed1=Serial.readString(); //Serial.readString();
//steps=Serial.read();
int a=angle.toInt();
int s=speed1.toInt();
int d=dir.toInt();
Serial.println(d);
Serial.println(s);
int r;
r=3500;
// put your main code here, to run repeatedly:
float stepsNumber=0;
stepsNumber=a/1.8;
//motor starts 5 rounds
for (int j=0; j<5; j++){
if (d==1){ //clockwise
digitalWrite(dirPin, HIGH);
for (int i = 0; i < stepsNumber; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(r);
digitalWrite(stepPin, LOW);
delayMicroseconds(r);
};
}
else{ //anticlockwise
digitalWrite(dirPin, LOW);
for (int i = 0; i < stepsNumber; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(r);
digitalWrite(stepPin, LOW);
delayMicroseconds(r);
}
}
}
Serial.println(a);
Serial.println(d);
Serial.println(r);
}
}
  1 个评论
Ahmed
Ahmed 2024-11-1,7:51
I assume you were able to find a solution. In which case I would be really interested to know because I have the same problem. Thanks.

请先登录,再进行评论。

回答(1 个)

Brahmadev
Brahmadev 2024-11-6,11:08
I believe the issue you're experiencing seems to be related to how "Serial.readString()" is used within the void loop. This function continuously runs, and as soon as data is available in the serial buffer, it is interpreted as an angle with the line:
angle = Serial.readString();
To address this, consider the following approaches:
  1. Flag System: Implement flags to manage which values have already been read. Once all the required values are read, reset the flags to false and restart reading "angle" once again. Ensure that subsequent code sections execute only after all values are read, or all flags are set to "true".
  2. Concatenated Data with Delimiters: Send data from MATLAB in a concatenated format with delimiters. On the Arduino side, read this concatenated string and parse it to extract the three required integer values. This method guarantees that all values are transmitted together.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by