Communication between Arduino and Matlab: Why is it failling?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Madhu Govindarajan
Madhu Govindarajan il 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 il 16 Nov 2017
It sends to arduino, however arduino doesn't reply.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by