Communication between Arduino and Matlab: Why is it failling?

조회 수: 1 (최근 30일)
Andreia dos Santos
Andreia dos Santos 2017년 11월 14일
댓글: Andreia dos Santos 2017년 11월 16일
Hello, I have this code on arduino
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 40, 100); // scale it to use it with the servo (value between 0 and 180)
if(timer == 4000){
myservo1.write(val); // sets the servo position according to the scaled value
//delay(100); // waits for the servo to get there
}
if(timer == 8000){
myservo1.write(0); // sets the servo position according to the scaled value
timer = 0;
//delay(100); // waits for the servo to get there
}
timer++;
Vsensor = analogRead(analogPin); // read the input pin
if(Serial.available() > 0 ) Serial.println(Vsensor); // debug value
}
And I have the following code on Matlab:
clear all
close all
clc
s = serial('COM7','BAUD', 9600);
fopen (s);
fwrite(s, '1');
pause(0.01);
if get(s,'BytesAvailable') > 0
Volt = fscanf(s, '%f');
fprintf ('%d\n', Volt);
end;
It seems s.BytesAvailable is always equal to 0. If I run s it says "sent 1", but it did not receive anything at all ("receive 0"). Am I missing something? I am using Arduino UNO.
Thanks for the help.
  댓글 수: 2
Madhu Govindarajan
Madhu Govindarajan 2017년 11월 15일
What happens if you remove the rest of the code on the arduino and just have serial portion on both sides to see if you can write from MATLAB to arduino and then back?
Andreia dos Santos
Andreia dos Santos 2017년 11월 16일
It sends to arduino, however arduino doesn't reply.

댓글을 달려면 로그인하십시오.

답변 (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