hi there!
i am also searching for how to set up my arduino with matlab.
i want to use matlab to controll a servo, connected to a arduino uno.
i read every post i could find in this forum about "matlab servo", but nothing i found was helpful.
i also searched the internet, and this is what i get so far:
MATLAB CODE:
%% Arduino Servo Controller
arduino = serial('COM3','BaudRate',9600); % Set ComPort
fopen(arduino);
fprintf(arduino,'%s','120'); %Write 120 to ComPort
fwrite(arduino,120);
pause(0.02)
fclose(arduino);
delete(arduino)
clear arduino
fprintf(arduino,'%s','120');
fwrite(arduino,120);
those are two ways i found to send data, but both of them dont work....
ARDUINO CODE:
/*
Matlab-Arduino Servo Controller
*/
#include <Servo.h>
//DECLARATION---------------------
Servo servo1;
//SETUP----------------------------
void setup() {
Serial.begin(9600);
servo1.attach(6);
}
//LOOP--------------------------------
void loop() {
if( Serial.available() )
{
int x = Serial.read();
servo1.write(x);
}
}
It seems Matlab isnt sending the right data.
If is send data, arduino RX Led is blinking, but nothing happens.
SO it gets something, but not the angle (ie. 120) to controll the servo.
i hope somebody can help me with that problem.
thank you!
---moe
EDIT:
ok, i found the error!!!!
before u send data to serial with matlab you have to give arduino a little break!
just insert a:
pause(0.5) before the write commando
like this!
%% Arduino Servo Controller
arduino = serial('COM3','BaudRate',9600); % Set ComPort
fopen(arduino);
pause(0.5) % this is very important! arduino needs a little time to initialise the fopen command!
for i = 0:179 % Runs your servo from 0-179 degree
fwrite(arduino,i);
pause(0.02)
end
fclose(arduino);
delete(arduino)
clear arduino