I am measuring voltage from a pot on my Arduino Uno. My time vector grows but all the index values of my voltage vector are the same as I vary the pot.

조회 수: 2 (최근 30일)
I need to store the individual voltage values as I vary the pot. I included the file, any help is appreciated.

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 26일
I could not understand the reason for using a for loop inside the while loop. Try the following code. I have also made some other changes e.g. you were calling plot() inside while loop which can make the code slow. I have used figure handle to update the same figure.
%Use a flag so that you can "append" new data to the end of it
time=0;
t=[]; % although pre-allocation is good, but the number of samples are not known in advance.
% If your data size is small, it will not matter much.
voltage=[];
f = gcf; % instead of creating new axis inside the while loop. Create a figure, and update its handle
ax = gca;
l = line();
l.XData = [];
l.YData = []
ylim([0 5]);
xlabel('time (s)');
ylabel('voltage (v)');
title('analog data from the potentiometer');
while (flag==0)
t=[t time];
time=time+1;
voltage = [voltage readVoltage(a,pot)];
if (readDigitalPin(a,button)==1) %button is pressed
break; % nor need for flag, just break the loop
end
c=clock;
fprintf(fid,'%4.0d %2.0d %2.0d %2.0d %2.0d %6.4d %d\n',c,sin(c(6)));
pause(1);
% Plot the data
l.XData = t;
l.YData = voltage;
end
Also, you are not writing the voltage value in the file, you might want to look into that.
  댓글 수: 5
Alan Clemenson
Alan Clemenson 2018년 5월 27일
Thank you, I'm pretty excited about being able to recall and update a figure this way too. That's everything.

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

추가 답변 (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