Go Down

Topic: serial communication with Due and Matlab (Read 1 time) previous topic - next topic

koronus

Hi
I use the Due Arduino to read the position of the encoder and I want to have a serial communication this position in Matlab so I write this code:
Code: [Select]

#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 in the Matlab I have this one:
Code: [Select]

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 have asked my question in project guide and one Member answered me that I have to use USB-TTL with connecting Rx Tx and GND to the Arduino board instead of ordinary USB cable connection so is it right way for DUE board?

Go Up