How to plot live data from arduino on current HH:MM x axis
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I can plot live data however I am now trying to plot on an x axis that reflects the time of day that the data was recorded. I have had an attempt (seen below) can anyone see why it isn't working or know how to achieve my task?
%User Defined Properties
a = arduino('Com3') % define the Arduino Communication port
plotTitle = 'Solar Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Power (W)'; % y-axis label
yMax = 5.5 %y Maximum Value
yMin = 0 %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 5.5; % set y-max
delay = 1;
%Define Function Variables
elapsed_hours = delay*60;
elapsed_min = delay;
t_1 = [08, 00]; % Start time 08:00
t_2 = [t_1(1) + elapsed_hours, t_1(2) + elapsed_min*60]; % End time 10:30
HH_1 = t_1(1); % Hour digits of t_1
MM_1 = t_1(2); % Minute digits of t_1
HH_2 = t_2(1); % Hour digits of t_2
MM_2 = t_2(2); % Minute digits of t_2
time = HH_1/24+MM_1/1440:1/1440:HH_2/24+MM_2/1440
%time = 0;
data = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,'-r' ) % every AnalogRead needs to be on its own
Plotgraph
datetick('x','HH:MM')
hold on %hold on makes sure all of the channels are
plotted
title(plotTitle,'FontSize',15);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([yMin yMax min max]);
grid(plotGrid);
tic
while ishandle(plotGraph) %Loop when Plot is Active will run until plot is closed
Vin = readVoltage(a,'A0');
Vout = readVoltage(a,'A1');
Vin = Vin*5.00501002 *1.789;
Vout;
Power = Vin * Vout;
dat = Power %Data from the arduino
count = count + 1;
time(count) = toc;
data(count) = dat(1);
%This is the magic code
%Using plot will slow down the sampling time.. At times to over 20
%seconds per sample!
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
%Update the graph
pause(delay);
end
0 commentaires
Réponse acceptée
dpb
le 24 Sep 2017
Create the initial figure and a line handle something like
figure
hL=plot(t,nan,'datetimetickformat','HH:mm');
hold on
that creates the figure and axes and a line handle to modify. You can specify other properties as desired of course to get the appearance wanted.
Then, in the loop similar to what you've got...
coeff=5.00501002*1.789; % scale factor -- is this right or should be linearization????
while _condition_
Vin = coeff*readVoltage(a,'A0'); % assuming is just scale factor
Vout = readVoltage(a,'A1');
Power = Vin*Vout;
count = count + 1;
t(count)=datetime(clock); % get the current clock time of the acquisition
data(count) = Power;
set(hL,'XData',t,'YData',data);
pause(delay);
end
The above has the issue that you've not preallocated arrays t and data so they are growing dynamically -- preallocate some large number of elements first then you can add to it by the counted index and it'll be much quicker as the number grows.
You may want to add niceties of only keeping last N points and such, but the above should get going.
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!