How to send serial command to Arduino through MATLAB GUI?

6 visualizzazioni (ultimi 30 giorni)
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
}

Risposte (0)

Categorie

Scopri di più su Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by