How to send serial command to Arduino through MATLAB GUI?

6 Ansichten (letzte 30 Tage)
yeang Meng Hern
yeang Meng Hern am 17 Feb. 2017
Kommentiert: melisa samad am 3 Dez. 2017
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 Kommentar
melisa samad
melisa samad am 3 Dez. 2017
hai, do you get the solution for this answer? if you have, can you with me?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by