clear;
% Connect to Arduino
a = arduino;
% Initialize flowrate archives
flowrate_archive_a = [];
flowrate_archive_b = [];
flowrate_archive_c = [];
flowrate_archive_d = [];
flowrate_archive_e = [];
flowrate_archive_f = [];
i = 1; % Counter variable
% Create two figures for separate plots
figure1 = figure('Name', 'Flow Rate Monitor A, B, C');
figure2 = figure('Name', 'Flow Rate Monitor D, E, F');
% Set up the first figure with subplots for A, B, C
subplot(3, 1, 1, 'Parent', figure1);
line1 = animatedline('Color', 'red');
title('Flowrate A (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax1 = gca;
subplot(3, 1, 2, 'Parent', figure1);
line2 = animatedline('Color', '#D95319');
title('Flowrate B (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax2 = gca;
subplot(3, 1, 3, 'Parent', figure1);
line3 = animatedline('Color', '#EDB120');
title('Flowrate C (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax3 = gca;
% Set up the second figure with subplots for D, E, F
figure(figure2);
subplot(3, 1, 1, 'Parent', figure2);
line4 = animatedline('Color', '#77AC30');
title('Flowrate D (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax4 = gca;
subplot(3, 1, 2, 'Parent', figure2);
line5 = animatedline('Color', '#4DBEEE');
title('Flowrate E (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax5 = gca;
subplot(3, 1, 3, 'Parent', figure2);
line6 = animatedline('Color', 'm');
title('Flowrate F (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax6 = gca;
% Main loop to update the plots
while true
% Read voltages from Arduino
voltage_a = readVoltage(a, 'A0');
voltage_b = readVoltage(a, 'A1');
voltage_c = readVoltage(a, 'A2');
voltage_d = readVoltage(a, 'A3');
voltage_e = readVoltage(a, 'A4');
voltage_f = readVoltage(a, 'A5');
% Convert voltages to flowrates
flowrate_a = (voltage_a - 0.8077) / 0.2876;
flowrate_b = (voltage_b - 0.8077) / 0.2876;
flowrate_c = (voltage_c - 0.8077) / 0.2876;
flowrate_d = (voltage_d - 0.8077) / 0.2876;
flowrate_e = (voltage_e - 0.8077) / 0.2876;
flowrate_f = (voltage_f - 0.8077) / 0.2876;
% Append to archives
flowrate_archive_a = [flowrate_archive_a flowrate_a];
flowrate_archive_b = [flowrate_archive_b flowrate_b];
flowrate_archive_c = [flowrate_archive_c flowrate_c];
flowrate_archive_d = [flowrate_archive_d flowrate_d];
flowrate_archive_e = [flowrate_archive_e flowrate_e];
flowrate_archive_f = [flowrate_archive_f flowrate_f];
% Update the lines with new data
addpoints(line1, i, flowrate_a);
addpoints(line2, i, flowrate_b);
addpoints(line3, i, flowrate_c);
addpoints(line4, i, flowrate_d);
addpoints(line5, i, flowrate_e);
addpoints(line6, i, flowrate_f);
% Draw updates
drawnow;
% Pause for 1 second between updates
pause(1);
% Increment counter
i = i + 1;
% Keep the X axis length to a maximum of 300 points (5 minutes)
if i > 300
set(ax1, 'XLim', [i-300 i]);
set(ax2, 'XLim', [i-300 i]);
set(ax3, 'XLim', [i-300 i]);
set(ax4, 'XLim', [i-300 i]);
set(ax5, 'XLim', [i-300 i]);
set(ax6, 'XLim', [i-300 i]);
else
set(ax1, 'XLim', [0 300]);
set(ax2, 'XLim', [0 300]);
set(ax3, 'XLim', [0 300]);
set(ax4, 'XLim', [0 300]);
set(ax5, 'XLim', [0 300]);
set(ax6, 'XLim', [0 300]);
end
% Optionally, to view the entire graph at the end,
% you could add a condition to break the loop after a certain duration:
% if i >= 28800 % for an 8-hour experiment
% break;
% end
end