How to send serial command to Arduino through MATLAB GUI?

조회 수: 6 (최근 30일)
yeang Meng Hern
yeang Meng Hern 2017년 2월 17일
댓글: melisa samad 2017년 12월 3일
Hello,
I am currently doing a project to control the servo motor through the keyboard input using MATLAB GUI. I met some problem in sending serial commands to the arduino when the GUI is running. Please help me. T.T. Below is the code for MATLAB and arduino.
MATLAB s=serial('COM3','BAUD', 9600); % Make sure the baud rate and COM port is % same as in Arduino IDE fopen(s);
switch eventdata.Key case 'a' fprintf(s,100);
case 's'
fprintf(s,101);
end
Arduino Code
#include<Servo.h> // include server library
Servo servomotor;
int poserm = 0;
int val; // initial value of input
void setup() {
Serial.begin(9600); // Serial comm begin at 9600bps
servomotor.attach(6);
}
void loop() {
if (Serial.available()) // if serial value is available
{
val = Serial.read();// then read the serial value
if (val == 100) //if value input is equals to d
{
poserm += 1; //than position of servo motor increases by 1 ( anti clockwise)
servomotor.write(poserm);// the servo will move according to position
delay(10);//delay for the servo to get to the position
}
if (val == 101) //if value input is equals to a
{
poserm -= 1; //than position of servo motor decreases by 1 (clockwise)
servomotor.write(poserm);// the servo will move according to position
delay(10);//delay for the servo to get to the position
}
  댓글 수: 1
melisa samad
melisa samad 2017년 12월 3일
hai, do you get the solution for this answer? if you have, can you with me?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by