arduino matlab plotting problem

hi,
I have used my ultrasonic sensor with arduino to display the distance now I want to plot these data in matlab the code I used for arduino is
#define ECHOPIN 3
#define TRIGPIN 2
void setup()
{
Serial.begin(9600);
pinMode(ECHOPIN,INPUT);
pinMode(TRIGPIN,OUTPUT);
}
void loop()
{
// Start Ranging -Generating a trigger og 10us burst
digitalWrite(TRIGPIN ,LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);
float distance =pulseIn(ECHOPIN,HIGH);
distance=distance/58;
Serial.print(distance);
Serial.print("cm");
delay(200);
// put your main code here, to run repeatedly:
}
and the code I used for matlab is
close all
clc
arduino=serial('COM3','BaudRate',9600);
fopen(arduino); % initiate arduino communication
CM(1)=0;
time(1)=0;
i=1;
tic;
while (toc<=20)
CM(2)=fscanf(arduino,'%f');
time(2)=toc;
figure(1);
grid on;
axis([toc-10, toc+10, 0, 35])
h(i)=plot(time,CM,'b','LineWidth',4);
hold on
CM(1)=CM(2);
time(1)=time(2);
if(i >= 300)
delete(h(i-299));
end
i=i+1;
end
fclose(arduino); % end communication with arduino
the problem is that the plot i got is not a real time plot ,it does not display the continous data of the ultrasonic senor
I want to know if my matlab code is correct ?
Im new to matlab and i want a help plz

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Asked:

on 5 Feb 2016

Edited:

on 5 Feb 2016

Community Treasure Hunt

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

Start Hunting!