Send values to arduino using serial monitor

조회 수: 26 (최근 30일)
Rohan Savakar
Rohan Savakar 2019년 6월 24일
답변: Rohan Savakar 2019년 6월 25일
The following is my arduino code which has been uploaded to a Arduino Uno hardware fitted with a adafruit motor shield v1.
#include <AFMotor.h>
AF_Stepper m1(200, 1);
AF_Stepper m2(200, 2);
int data = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
Serial.print(data);
m1.setSpeed(10);
m2.setSpeed(10);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
data = Serial.read();
delay(100);
if (data == '1') {
m1.step(500, FORWARD, SINGLE);
}
if (data == '2') {
m2.step(500, FORWARD, SINGLE);
}
}
}
The code works to take serial input from the monitor and according rotate a respective motor
I have done some calculations in matlab and would like to rotate a motor give an input from the matlab interface. Thus i would like to talk to this serial monitor using the matlab interface. In trying to do the same i have come up with the following code
delete(instrfind);
serial_Port=serial('COM6','BaudRate',19200);
fopen(serial_Port);
fprintf(serial_Port,'%i', 1);
pause(2);
fclose(serial_Port);
This has not yielded the required result of rotating motor 1.
Also as noticed in the above arduino code it is seen that i have printed '0' in the serial monitor.
Tweaking the matlab code to read from the serial monitor has not yielded me the same value it gave me a different value every time the script was run.
A warning which popped up was
Warning: The specified amount of data was not returned within the Timeout period. 'serial' unable to read all requested data. For more information on possible reasons, see Serial Read Warnings.
I have taken care that both the serial port are of the same bit rate. And the connection COM is that as shown from the device manager application.
This is one of my first questions asked, kindly teach me more.
Thank you.

채택된 답변

Rohan Savakar
Rohan Savakar 2019년 6월 25일
I have successfully been able to send values as in my code below.
Note:
fwrite send values as characters themselves so if i try to send "hello" it will send data through the serial port as
  • h
  • e
  • l
  • l
  • o
whereas if you send values through the fprintf function as string type the same "hello" would be sent as
  • h
  • e
  • l
  • l
  • o
  • \n
thus an extra character would be sent to indicate the end of the sent ASCI values
Find Below my code on arduino and MATLAB
MATLAB
clear
s=serial('COM6','BaudRate',9600); %note your port and bitrate
fopen(s);
text=fscanf(s);
fprintf(s,"2200"); %firsrt character for motor number and 2-4 character number the number of steps
pause(5);
fclose(s);
Arduino
#include <AFMotor.h>
AF_Stepper m1(200, 1);
AF_Stepper m2(200, 2);
char data[4];
byte numByteAvailable;
String Steps = "";
int val = 0;
int steps;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
m1.setSpeed(10);
m2.setSpeed(10);
char a = 'b';
//Serial.println(" hello world ");
}
void loop() {
// put your main code here, to run repeatedly:
delay(100);
byte numByteAvailable = Serial.available();
//delay(100);
char garbage;
if (numByteAvailable > 1) {
//Serial.println("start");
int i;
for (i = 0; i < numByteAvailable-1; ++i) {
char raw = Serial.read();
data[i] = raw;
Serial.println(raw);
}
garbage = Serial.read();
//Serial.println("end if in");
steps= (data[1]-'0')*100+(data[2]-'0')*10+(data[3]-'0')*1;
//Serial.println(Serial.available());
//Serial.println(steps);
if (data[0] == '1') {
m1.step(steps, FORWARD, SINGLE);
}
else if (data[0] == '2') {
m2.step(steps, FORWARD, SINGLE);
}
//Serial.println("end if");
}
//Serial.println(data[val]);
//Serial.println(data[0]); Serial.println(data[1]); Serial.println(data[2]); Serial.println(data[3]);
}
Several part have been commented out you can use them to debug the code.
the variable garbage is required for removing the last value which is put through the MATLAB function fprintf.
IT IS VERY IMPORTANT TO PUT UP THE DELAY COMMAND AS IT TAKES TIME TO ENTER VALUES INTO THE HARDWARE'S BUFFER.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by