I have done the same previously so what happens actually when you click 1 or 2 or 3 or A..........something like you want within the Serial Port monitor then it sends the ASCII values to the arduino and not the 1 or 2 or 3 or A........... as you expected so programme your code with the ASCII values and not the keyboard value.
for example the ASCII value for 'A' is 65 and 'a' is 97 , so if you want servo to move some angle when you click 'a' programme your code with 97 as this is the value that would be sent to arduino on small 'a' input.
%% Arduino Servo Controller
arduino = serial('COM3','BaudRate',9600); % Set ComPort
fopen(arduino);
pause(0.01) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!
fwrite(arduino,201)
for i = 0:179 % Runs your servo from 0-179 degree
fwrite(arduino,i);
pause(0.02)
end
fclose(arduino);
delete(arduino)
clear arduino
for example,
i want to send 2 angles to 2 servos, then it should look like this in serial monitor:
201
45
202
90
servo1 should be 45 degree, and servo2 should be 90 degree
so, i think i know how to send from matlab, like the code above
but how do i seperate the different serial inputs with the arduino?
edit:
the big problem is, i cant use the serial monitor, to check what kind of data is send, it always says com3 is already in use.
is there any solution, to use the serial monitor, while sending data with com3 with matlab?
So, if the value read from the serial port is 201, you want to read another value to get the actual angle for servo 1.
If the value read from the serial port is 202, you want to read another value to get the actual angle for servo 2.
Doesn't seem that difficult, to me. Just be sure to not read anything that isn't there, yet. In other words, instead of not reading unless there is one byte or more, as you do now, you need to not read unless there are two or more bytes.
The sender now appears to send a value to define the servo and then a series of values to sweep the servo. That, of course, will need to change, too.