reading the data value with serial communication in real time

Hi
I want to read the value of the position of the encoder with serial communication in Matlab
the code of reading the position of the encoder is:

#define outputA 6
#define outputB 7

int counter = 0;
int aState;
int aLastState;

void setup() {
  pinMode (outputA, INPUT);
  pinMode (outputB, INPUT);
  Serial.begin(9600);
  aLastState = digitalRead(outputA);

}

void loop() {
aState = digitalRead(outputA);
if (aState != aLastState) {
  if (digitalRead(outputB) != aState) {
    counter ++;
  } else {
    counter --;
  }
  Serial.print("Position: ");
  Serial.println(counter);
}
aLastState = aState;
}
  }

and the code in Matlab is:

s=serial('COM3');
fopen(s);
a=fscanf(s)

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?

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

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?

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

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

koronus:
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 reckon you need to ask your question on a Matlab Forum. Your Arduino seems to be working properly.

...R