How to send serial command to Arduino through MATLAB GUI?

6 visualizaciones (últimos 30 días)
yeang Meng Hern
yeang Meng Hern el 17 de Feb. de 2017
Comentada: melisa samad el 3 de Dic. de 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 comentario
melisa samad
melisa samad el 3 de Dic. de 2017
hai, do you get the solution for this answer? if you have, can you with me?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by