Hi guys, now i'm handling a project which involve control of a stepper motor.
First of all, all the processing will be done in the MATLAB, whereas the Stepper motor which drive by the driver of TB6600, will receive the information from the MATLAB and trigger the stepper motor to rotate. But, not matter what method i used to convert the Serial read value which is in bits back to the integer which suppose be the same as the MATLAB value (ReqS0), and it doesn't show what i want.
Another question is, while the Arduino establish serial communication with MATLAB, the serial monitor will not work. Therefore, i don't know what kind of data is transmitted to the Arduino. Anyway to sort this out?
Here is my MATLAB code:
A0 = evalin('base','A0');
ad=serial('COM4'); % assign serial port object
set(ad, 'BaudRate', 9600); % set BaudRate to 9600
set(ad, 'Parity', 'none'); % set Parity Bit to None
set(ad, 'DataBits', 8); % set DataBits to 8
set(ad, 'StopBit', 1); % set StopBit to 1
set(ad, 'OutputBufferSize', 8);
set(ad, 'InputBufferSize', 8);
set(ad,'Timeout', 5);
%display the properties of serial port object in MATLAB Window
ReqS0 = round(A0/0.1125);fopen(ad);
pause(2);
fprintf(ad, '%s',ReqS0);
fclose(ad);
Here is my Arduino code:
// defines pins numbers
const int stepPin = 5;
const int dirPin = 7;
const int enPin = 6;unsigned int integerValue=0; // Max value is 65535
char incomingByte;
int value;void setup()
{
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}void loop()
{
if (Serial.available() > 0) { // something came across serial
integerValue = 0; // throw away previous integerValue
while(1) { // force into a loop until 'n' is received
incomingByte = Serial.read();
if (incomingByte == '\n') break; // exit the while(1), we're done receiving
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
integerValue *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
integerValue = ((incomingByte - 48) + integerValue);
}
Serial.println(integerValue); // Do something with the value
}
}
Even though the method i found from online source, but it still not working... Anyway, thank you for your concern. ![]()