I have actually a very similar problem to the original posting: I would like to control 5 servos at real-time (or as fast as possible I can get to this) via Matlab`s serial port (instead of using the Arduino I/O package).
What I have achieved so far is the following:
- Connecting my Arduino mega 2560 via USB (com5)
- Connecting my servos to pwm pins 2,4,6,8,10
- Controlling my servos via Matlab using the Arduino I/O package. --> This works really fine, apart from the insufficient timing. So I am sure that every servo is connected properly and that the arduino board is accessible via Matlab.
- Establishing a general communication between Arduino and Matlab via serial port, following this video: http://www.matlabarduino.org/serial-communication.html
--> This also works.
However, what I am struggling with is how to properly write (Matlab) and read (Arduino) data from the serial port. Based on your suggestions here, my idea was to use integers for serial port communication between matlab and arduino: The first integer (nr) to say arduino which servo shall be moved, and the second integer (position) to say arduino to which degree the servo shall be moved.
This is the Matlab code that I have so far (trying to move the servo on pin 2 from 40 to 80 degree, in a for loop):
delete(instrfind('Type', 'serial')); % Assure that no serial port is open anymore
s = serial('com5'); % Create new serial port object.
%set(s,'DataBits',8); % ???
%set(s,'StopBits',1); % ???
set(s,'BaudRate',9600); % Use same baudrate specified in Arduino
%set(s,'Parity','none'); % ???
fopen(s); % Open Serial port
nr = '2'; % servo to be moved
position = '40'; % degree
% Try to move servo 2 every second
if rem(i,2) % if equal
position = '80';
fprintf(s, '%i',nr); % sent servo number (in integer format)
fprintf(s, '%i',position); % sent servo position (in integer format)
else % if unequal
position = '40';
pause(1); % wait one second
And this is the corresponding arduino code:
servo10.write(60); // This works!!!
if( Serial.available() >=2)
int nr = Serial.read(); // read in servo number
int position = Serial.read(); // read in servo position
servo6.write(60); // default position
//sleep(100); // give the servo's some time --> Throws an "'sleep' was not declared in this scope"-error!!!
The problem is now that although matlab seems to be connecting with the serial port, nothing really happens (apart from moving the servos once to the default position). Hence, it seems to be the case that the arduino cannot really read the integers that I sent by Matlab.
Does anybody know what is wrong with my code?
I would be very very thankful if someone could help me out and give me some advice.
PS: I have also two further questions:
1. As I stated I would like to have as few delays as possible (We would like to compete with the nervous system). Can you give me some recommendations regarding the optimal baude rate for serial port communication?
2. the sleep command (last line in the arduino code) does not compile. Has anyone an idea why this is the case