필터 지우기
필터 지우기

Reading a Voltage in Arduino using Matlab.

조회 수: 3 (최근 30일)
Edmond
Edmond 2013년 2월 1일
Hi,
I am trying to read a voltage in Arduino with help from a Potentiometer or a piezo. Until now I'am having some trouble to make the Program run into Matlab. I already used the software from Arduino so the Controller is not the problem since the Serial monitor is reading the values. But I can't actually bring these values to a Graph.
Can somebody help me?
Thank you.
The Code:
clear all
clc
%clear the screen
delete(instrfind({'Port'},{'/dev/tty.usbmodem641'}));
%create the object(serie)
s = serial('/dev/tty.usbmodem641','BaudRate',9600,'Terminator','CR/LF');
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
%abrir puerto
fopen(s);
% parameters.
tmax = 10; % tiempo de captura en s
rate = 33; % resultado experimental (comprobar)
% figure
f = figure('Name','Captura');
a = axes('XLim',[0 tmax],'YLim',[0 5.1]);
l1 = line(nan,nan,'Color','r','LineWidth',2);
l2 = line(nan,nan,'Color','b','LineWidth',2);
xlabel('Tiempo (s)')
ylabel('Voltaje (V)')
title('Captura de voltaje en tiempo real con Arduino')
grid on
hold on
% initialize
v1 = zeros(1,tmax*rate);
v2 = zeros(1,tmax*rate);
i = 1;
t = 0;
tic
while t<tmax
t = toc;
% read the port
a = fscanf(s,'%f,%f');
v1(i)=a(1)*5/1024;
v2(i)=a(2)*5/1024;
% dibujar en la figura
x = linspace(0,i/rate,i);
set(l1,'YData',v1(1:i),'XData',x);
set(l2,'YData',v2(1:i),'XData',x);
drawnow
% seguir
i = i+1;
end
% results
clc;
fprintf('%g s de captura a %g cap/s \n',t,i/t);
%%clean the screen.
fclose(s);
delete(s);
clear s;
  댓글 수: 2
Ryan G
Ryan G 2013년 2월 1일
Are you able to verify the parameters in your workspace?
John Petersen
John Petersen 2013년 2월 4일
Yes, put a breakpoint at v1(i) and verify that 'a' is correct.

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

답변 (0개)

카테고리

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