Communication between Arduino and Matlab: Why is it failling?
1 次查看(过去 30 天)
显示 更早的评论
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
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?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!