reading data with serial communication between arduino and matlab problem

조회 수: 2 (최근 30일)
john white
john white 2019년 5월 26일
댓글: john white 2019년 5월 27일
Hi
I want to read position of encoder which is coded in Arduino program with serial communication in Matlab
the code in arduino program is:
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 of Matlab is:
s=serial('COM3');
fopen(s);
a=fscanf(s)
the problem is when the position is changed for example from 0 to 12 , when I use a=fscanf(s) it shows each values from 0 to 12 each times when I run this order I mean at the first time it shows 0 at the 2nd time it shows 1 at the3rd time it shows 2 etc.but I want to read the value of the position in real time how could I get this value?

답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by