the problem is when the position is change from(for example) 0 to 12
the value of 'a' in the Matlab shows all the changes which for the first time when I type a=fscanf(s) it shows 0 for the second one it shows 1 for the third one it shows 2
but I want to the value of a be a certain amount in real time for example if the encoder is stopped at 12 position when I type a=fscanf(s) it shows 12
how I should write this code?
opens the serial port every time it wants to read a value from the Arduino. That will cause the Arduino to restart.
Your Matlab program needs to open the serial port, allow time for the Arduino to reset and then keep the serial port open until it is finished with the Arduino.
If that is not practical then an alternative may be to use a USB - TTL cable connected to the Rx Tx and GND pins of the Arduino rather than the regular USB connection.
Robin2:
I don't know Matlab but it looks like this code
s=serial('COM3');
fopen(s);
a=fscanf(s)
opens the serial port every time it wants to read a value from the Arduino. That will cause the Arduino to restart.
Your Matlab program needs to open the serial port, allow time for the Arduino to reset and then keep the serial port open until it is finished with the Arduino.
If that is not practical then an alternative may be to use a USB - TTL cable connected to the Rx Tx and GND pins of the Arduino rather than the regular USB connection.
...R
my Arduino board model is Due, is the USB-TTL cable would be matched with?
and if I use this cable then my code is correct?
Robin2:
I have never had a DUE. It is a 3.3v board so I assume you will need a USB-TTL cable that has 3.3v at the TTL end.
There is a separate section of the Forum dedicated to the DUE
...R
even I have changed the board to the Uno, but the result is the same, the problem is the fscanf(s) doesn't show the last number I mean if the serial print in arduino program is:
1
2
3
4
5
6
7
8
9
10
11
12
the fscanf(s) shows all the numbers each time the fscanf(s) is run
but I want to show the last number