How to send serial command to Arduino through MATLAB GUI?
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!