Real Time Data Acquisition With Arduino Analog Input

조회 수: 2 (최근 30일)
Darryl
Darryl 2013년 7월 4일
편집: Mehmet Bulun 2017년 11월 2일
I'm in the process of trying to visualise my basic analog light sensor using the plotting function within MatLab.
This is my code so far:
a = arduino('/dev/tty.usbmodem411');
i = 1;
axis = 1;
while (i == 1)
b = a.analogRead(0);
axis = axis +1;
plot(axis, b);
pause(1);
drawnow;
end
I figured I'd keep it simple by using a sample rate of 1Hz.
My Y axis is moving up and down dependant on the light sensor value and the x axis is incrementing by 1 every time the loop increments, but I can't actually see the graph actually drawing anything.

답변 (2개)

David Sanchez
David Sanchez 2013년 7월 4일
Assuming the code on the Arduino side is sending something ( check it out, please ), and that the arduino object points to the right port, try this changes:
a = arduino('/dev/tty.usbmodem411');
i = 1;
axis = 1;
while (i == 1)
b = a.analogRead(0);
axis = axis +1;
plot(axis, b,axis, b,'r*');
drawnow;
pause(1);
end
  댓글 수: 1
Darryl
Darryl 2013년 7월 4일
편집: Darryl 2013년 7월 4일
Thanks for the reply. Yes 100% its on the correct port and the sensor is working using the values 0 - 1023.
I've just tried your code but all I get is a static red star right in the middle of the graph whilst the x and y axis are moving. Has "hold on" got to be activated?

댓글을 달려면 로그인하십시오.


Mehmet Bulun
Mehmet Bulun 2017년 11월 2일
편집: Mehmet Bulun 2017년 11월 2일
Try below code.
a = arduino('/dev/tty.usbmodem411');
interval = 1000;
passo = 1;
t=1;
x=0;
while (t<interval)
b = a.analogRead(0);
x=[x,b];
plot(x);
axis([,interval,500,500]);%500 and 500 are axis limits
t=t+passo;
drawnow;
end

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by