Reading a Voltage in Arduino using Matlab.

5 次查看(过去 30 天)
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 中查找有关 Arduino Hardware 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by