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 Center および File ExchangeMATLAB 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