Communication between Arduino and Matlab: Why is it failling?
1 view (last 30 days)
Show older comments
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 Comments
Madhu Govindarajan
on 15 Nov 2017
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?
Answers (0)
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!