Filter löschen
Filter löschen

Communication between Arduino and Matlab: Why is it failling?

1 Ansicht (letzte 30 Tage)
Andreia dos Santos
Andreia dos Santos am 14 Nov. 2017
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 Kommentare
Madhu Govindarajan
Madhu Govindarajan am 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?
Andreia dos Santos
Andreia dos Santos am 16 Nov. 2017
It sends to arduino, however arduino doesn't reply.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by