MATLab with Arduino pause time

Hello,

Myself and some colleagues are writing a code to take analog voltage inputs from a variety of sensors (pressure, temperature, flow rate) and convert this data into a live chart that displays the current reading of each sensor on a neat graph all at once.

We have code that works fine, but the issue is that it takes around 1.6s for each data point to be generated, as our code is around 400 lines and all contained in a massive while true loop.

One thing that we are wondering about is the potential to reduce our pause time that we have built into the end of the code right before the while loop repeats itself. The code runs all the way and then pauses for 1 second to let the Arduino catch up and so as to not overload anything, and then continues back through the loop.

Would it be possible to reduce this time to anything lower? How would we know what the lowest feasible pause time is? Is a pause time of 1s necessary?

Thank you

Post the code, using code tags, and forum members can make useful suggestions.

Welcome to the forum

Do you think that it might help if you posted your code ?

Why do you think that you need to pause the code ?



clear;  

% Connect to Arduino  

a = arduino;  

% User input for Flowrate info  

num_pumps = input('Please enter the number of pumps you are using (ex. 4): ', 's');  

number_pumps = str2double(num_pumps);  

pump_list = cell(1, number_pumps);  

flowrate_archives = zeros(172800,6);  

for j = 1:number_pumps  

pump_input = input('Please enter the pumps in alphabetical order one at a time: ', 's');  

pump_list{j} = pump_input; 

pump_rate = input('Please enter the flow rate for this pump: ','s'); 

pump_rate_num = str2double(pump_rate); 

pump_rate_list(j) = pump_rate_num; 

end 

%User input for temperature info 

temperature_archives = zeros(172800,5); 

num_temp = input('Please enter the number of temperature sensors you are using: ','s'); 

number_temp = str2double(num_temp); 

temp_list = cell(1,number_temp); 

for j = 1:number_temp 

temp_input = input('Please enter one of temperature sensors you are using: ','s'); 

temp_list{j} = temp_input; 

temp_rate = input('Please enter the temperature you would like to keep that sensor at in C: ','s'); 

temp_rate_num = str2double(temp_rate); 

temp_rate_list(j) = temp_rate_num; 

end 

%User input for pressure info 

pressure_archives = zeros(172800,5); 

num_pressure = input('Please enter the number of pressure sensors you are using (ex. 4): ', 's');  

number_pressure = str2double(num_pressure);  

pressure_list = cell(1, number_pressure);  

for j = 1:number_pressure  

pressure_input = input('Please enter the pressure sensors you are using in alphabetical order one at a time: ', 's');  

pressure_list{j} = pressure_input; 

pressure_rate = input('Please enter the pressure for this sensor in psi: ','s'); 

pressure_rate_num = str2double(pressure_rate); 

pressure_rate_list(j) = pressure_rate_num; 

end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% Positions and colors for the text elements  

Legend_Positions = [0.87, 0.83, 0.79, 0.75, 0.71, 0.67]; 

colors = {'black','red','#EDB120','#77AC30','#4DBEEE','m'}; 

pump_ids = {'A','B','C','D','E','F'};  

pump_letters = 'ABCDEF'; 

temp_numbers = '12345'; 

pressure_letters = 'ABCEE'; 

% Create figures for flowrate data 

figure1 = figure('Name', 'Flowrate Data');  

subplot(2, 1, 1);  

PumpA = animatedline('Color', 'black');  

PumpB = animatedline('Color', 'red');  

PumpC = animatedline('Color', '#EDB120');  

PumpD = animatedline('Color', '#77AC30'); 

PumpE = animatedline('Color', '#4DBEEE'); 

PumpF = animatedline('Color', 'm'); 

title('All Flowrate Data Points');  

xlabel('# of Samples');  

ylabel('Flowrate (mL/min)');  

ylim([0, 11]);  

% Timer for Flowrate  

TimerText = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized', ...  

'Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00'); 

% Legend for flowrate data  

Legend_Text = cell(1, length(pump_ids));  

Pos_Update = 1;  

for idx = 1:length(pump_ids)  

if ismember(pump_ids(idx), pump_list)  

Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Pump_A_Legend = Legend_Text{1};  

Pump_B_Legend = Legend_Text{2};  

Pump_C_Legend = Legend_Text{3};  

Pump_D_Legend = Legend_Text{4};  

Pump_E_Legend = Legend_Text{5};  

Pump_F_Legend = Legend_Text{6};  

% Flowrate Average Plot 

subplot(2,1,2); 

PumpA_Avg = animatedline('Color', 'black');  

PumpB_Avg = animatedline('Color', 'red');  

PumpC_Avg = animatedline('Color', '#EDB120');  

PumpD_Avg = animatedline('Color', '#77AC30'); 

PumpE_Avg = animatedline('Color', '#4DBEEE'); 

PumpF_Avg = animatedline('Color', 'm'); 

title('Average (5sec) Flowrate');  

xlabel('# of Samples');  

ylabel('Flowrate (mL/min)');  

ylim([0, 11]); 

% Create figures for pressure data  

figure2 = figure('Name', 'Pressure Data');  

subplot(2, 1, 1);  

pressure_1 = animatedline('Color','black'); 

pressure_2 = animatedline('Color','red'); 

pressure_3 = animatedline('Color','#EDB120'); 

pressure_4 = animatedline('color','#77AC30'); 

pressure_5 = animatedline('color','#4DBEEE'); 

title('Overall Pressure');  

xlabel('# of Samples'); 

ylabel('Pressure (psi)');  

ylim([0, 200]); 

%Lenegd for pressure data 

Pressure_Legend_Text = cell(1, length(pressure_letters));  

Pos_Update = 1;  

for idx = 1:length(pressure_letters)  

if ismember(pressure_letters(idx), pressure_list)  

Pressure_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Pressure_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Pressure_A_Legend = Pressure_Legend_Text{1};  

Pressure_B_Legend = Pressure_Legend_Text{2};  

Pressure_C_Legend = Pressure_Legend_Text{3};  

Pressure_D_Legend = Pressure_Legend_Text{4};  

Pressure_E_Legend = Pressure_Legend_Text{5};  

%Timer on pressure graph 

pressureTimer = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized','Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00');  

subplot(2,1,2); 

pressure_1_avg = animatedline('color','black'); 

pressure_2_avg = animatedline('color','red'); 

pressure_3_avg = animatedline('color','#EDB120'); 

pressure_4_avg = animatedline('color','#77AC30'); 

pressure_5_avg = animatedline('color','#4DBEEE'); 

title('Average (5sec) Pressure');  

xlabel('# of Samples'); 

ylabel('Pressure (psi)');  

ylim([0, 200]); 

%Create figures for temperature data  

figure3 = figure('Name', 'Temperature Data');  

subplot(2, 1, 2);  

temp_1_avg = animatedline('Color','Black'); 

temp_2_avg = animatedline('color','red'); 

temp_3_avg = animatedline('color','#EDB120'); 

temp_4_avg = animatedline('color','#77AC30'); 

temp_5_avg = animatedline('color','#4DBEEE'); 

title('Average (5sec) Temperature');  

xlabel('# of Samples'); ylabel('Degrees Celcius');  

ylim([0, 100]);  

% Legend for the temperature graphs 

Temp_Legend_Text = cell(1, length(temp_numbers));  

Pos_Update = 1;  

for idx = 1:length(temp_numbers)  

if ismember(temp_numbers(idx), temp_list)  

Temp_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Temp_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Temp_A_Legend = Temp_Legend_Text{1};  

Temp_B_Legend = Temp_Legend_Text{2};  

Temp_C_Legend = Temp_Legend_Text{3};  

Temp_D_Legend = Temp_Legend_Text{4};  

Temp_E_Legend = Temp_Legend_Text{5}; 

%timer on temperature graph 

tempTimer = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized','Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00');  

subplot(2, 1, 1);  

temp_1 = animatedline('Color','Black'); 

temp_2 = animatedline('color','red'); 

temp_3 = animatedline('color','#EDB120'); 

temp_4 = animatedline('color','#77AC30'); 

temp_5 = animatedline('color','#4DBEEE'); 

title('Temperature Overall'); 

xlabel('# of Samples');  

ylabel('Degrees Celcius');  

ylim([0, 100]);  

% Figure 4 to display error messages 

figure4 = figure('Name','Flowrate Error'); 

flowrate_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.2 0.6 0.6], 'String', ' '); 

pressure_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.4 0.6 0.6], 'String', ' '); 

temp_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.6 0.6 0.6], 'String', ' '); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% Main loop to update the plots  

out_of_range = zeros(1, length(pump_list)); 

i = 1; 

% Initialize start time at the last moment before the loop starts 

startTime = tic; 

while true  

% Read voltages from Arduino  

flow_voltages = zeros(1,6); 

for j = 1:6 

flow_voltages(j) = readVoltage(a,['A',num2str(j-1)]); 

end 

%Read temp voltages from Arduino 

temp_voltages = zeros(1,5); 

for j = 1:5 

temp_voltages(j) = readVoltage(a,['A',num2str(j+5)]); 

end 

%Read pressure voltages from Arduino 

pressure_voltages = zeros(1,5); 

for j = 1:5 

pressure_voltages(j) = readVoltage(a,['A',num2str(j+10)]); 

end 

% Convert voltages to flowrates  

flowrates = (flow_voltages - 0.8077) / 0.2876;  

flowrate_archives(i,:) = flowrates; 

%Convert voltages to temperatures 

temperatures = (temp_voltages-1.25)/0.005; 

temperature_archives(i,:) = temperatures; 

%Convert voltages to pressure 

pressures = (pressure_voltages-0.8613)/0.0176; 

pressure_archives(i,:) = pressures; 

%Flowrate +- 10% check 

flowrate_warning_message = ' '; 

for j= 1:length(pump_list) 

lower_bound = pump_rate_list(j) * 0.9; 

upper_bound = pump_rate_list(j) * 1.1; 

if flowrates(j)<lower_bound || flowrates(j)>upper_bound 

out_of_range = out_of_range + 1; 

%fprintf("PUMP %s OUT OF +-10% RANGE FOR THE PAST %s SAMPLES",flowrates(j),out_of_range) 

flowrate_warning_message = sprintf('PUMP %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',pump_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(flowrate_warning_message) 

set(flowrate_warning_text, 'string', ' '); 

else 

set(flowrate_warning_text, 'string', flowrate_warning_message); 

end 

%Pressure +/-10% check 

pressure_warning_message = ' '; 

for j= 1:length(pressure_list) 

lower_bound = pressure_rate_list(j) * 0.9; 

upper_bound = pressure_rate_list(j) * 1.1; 

if pressures(j)<lower_bound || pressures(j)>upper_bound 

out_of_range = out_of_range + 1; 

%fprintf("PUMP %s OUT OF +-10% RANGE FOR THE PAST %s SAMPLES",flowrates(j),out_of_range) 

pressure_warning_message = sprintf('PRESSURE SENSOR %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',pressure_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(pressure_warning_message) 

set(pressure_warning_text, 'string', ' '); 

else 

set(pressure_warning_text, 'string', pressure_warning_message); 

end 

%Temperature +/-10% check 

temp_warning_message = ' '; 

for j= 1:length(temp_list) 

lower_bound = temp_rate_list(j) * 0.9; 

upper_bound = temp_rate_list(j) * 1.1; 

if temperatures(j)<lower_bound || temperatures(j)>upper_bound 

out_of_range = out_of_range + 1; 

temp_warning_message = sprintf('TEMPERATURE SENSOR %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',temp_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(temp_warning_message) 

set(temp_warning_text, 'string', ' '); 

else 

set(temp_warning_text, 'string', temp_warning_message); 

end 

% Update animated lines for Flowrates 

Exp_Flow_Lines = [PumpA,PumpB,PumpC,PumpD,PumpE,PumpF]; 

Average_Flow_lines = [PumpA_Avg,PumpB_Avg,PumpC_Avg,PumpD_Avg,PumpE_Avg,PumpF_Avg]; 

%overall flowrate 

for j = 1:length(pump_letters)  

if ismember(pump_letters(j), pump_list)  

pump_index = find(strcmp(pump_list, pump_letters(j))); 

if ~isempty(pump_index) 

addpoints(Exp_Flow_Lines(j), i, flowrates(j));  

else  

addpoints(Exp_Flow_Lines(j), i, 0);  

end 

else 

addpoints(Exp_Flow_Lines(j),i,0) 

end 

end  

%Average flowrate 

if i >= 5  

rolling_flowrate_averages = zeros(1, length(pump_letters));  

for j = 1:length(pump_letters)  

rolling_flowrate_averages(j) = mean(flowrate_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(pump_letters) 

if ismember(pump_letters(j), pump_list) 

addpoints(Average_Flow_lines(j), i, rolling_flowrate_averages(j)); 

else 

addpoints(Average_Flow_lines(j), i, 0); 

end 

end 

end  

%Update Animated Lines for Temperature 

Exp_temp_lines = [temp_1,temp_2,temp_3,temp_4,temp_5]; 

Avg_temp_lines = [temp_1_avg,temp_2_avg,temp_3_avg,temp_4_avg,temp_5_avg]; 

%Overall temperature 

for j = 1:length(temp_numbers) 

if ismember(temp_numbers(j), temp_list)  

temp_index = find(strcmp(temp_list, temp_numbers(j))); 

if ~isempty(temp_index) 

addpoints(Exp_temp_lines(j), i, temperatures(j));  

else  

addpoints(Exp_temp_lines(j), i, 0);  

end 

else 

addpoints(Exp_temp_lines(j),i,0) 

end 

end  

%Average temperature 

if i >= 5  

rolling_temperature_averages = zeros(1, length(temp_numbers));  

for j = 1:length(temp_numbers)  

rolling_temperature_averages(j) = mean(temperature_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(temp_numbers) 

if ismember(temp_numbers(j), temp_list) 

addpoints(Avg_temp_lines(j), i, rolling_temperature_averages(j)); 

else 

addpoints(Avg_temp_lines(j), i, 0); 

end 

end 

end 

Exp_pressure_lines = [pressure_1,pressure_2,pressure_3,pressure_4,pressure_5]; 

Avg_pressure_lines = [pressure_1_avg,pressure_2_avg,pressure_3_avg,pressure_4_avg,pressure_5_avg]; 

%overall pressure 

for j = 1:length(pressure_letters) 

if ismember(pressure_letters(j), pressure_list)  

pressure_index = find(strcmp(pressure_list, pressure_letters(j))); 

if ~isempty(pressure_index) 

addpoints(Exp_pressure_lines(j), i, pressures(j));  

else  

addpoints(Exp_pressure_lines(j), i, 0);  

end 

else 

addpoints(Exp_pressure_lines(j),i,0); 

end 

end  

%Average pressure 

if i >= 5  

rolling_pressure_averages = zeros(1, length(pressure_letters));  

for j = 1:length(pressure_letters)  

rolling_pressure_averages(j) = mean(pressure_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(pressure_letters) 

if ismember(pressure_letters(j), pressure_list) 

addpoints(Avg_pressure_lines(j), i, rolling_pressure_averages(j)); 

else 

addpoints(Avg_pressure_lines(j), i, 0); 

end 

end 

end 

% Draw updates  

drawnow;  

%Pause for 1 second between updates  

pause(1);  

% X axis for flow rate 

if i > 180  

for j = 1:length(pump_letters)  

set(Average_Flow_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(pump_letters)  

set(Average_Flow_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end  

% X axis for pressure 

if i > 180  

for j = 1:length(temp_numbers)  

set(Avg_pressure_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(temp_numbers)  

set(Avg_pressure_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end 

%x axis for temp 

if i > 180  

for j = 1:length(pressure_letters)  

set(Avg_temp_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(pressure_letters)  

set(Avg_temp_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end 

%Current Values for flowrate 

set(Pump_A_Legend,'String', sprintf('Pump A (Black): %.4g', flowrate_archives(i,1))); 

set(Pump_B_Legend,'String', sprintf('Pump B (Red): %.4g',flowrate_archives(i,2))); 

set(Pump_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',flowrate_archives(i,3))); 

set(Pump_D_Legend,'String', sprintf('Pump D (Green): %.4g',flowrate_archives(i,4))); 

set(Pump_E_Legend,'String', sprintf('Pump E (Blue): %.4g',flowrate_archives(i,5))); 

set(Pump_F_Legend,'String', sprintf('Pump F (Pink): %.4g',flowrate_archives(i,6))); 

%current values for presure 

set(Pressure_A_Legend,'String', sprintf('Pump A (Black): %.4g', pressure_archives(i,1))); 

set(Pressure_B_Legend,'String', sprintf('Pump B (Red): %.4g',pressure_archives(i,2))); 

set(Pressure_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',pressure_archives(i,3))); 

set(Pressure_D_Legend,'String', sprintf('Pump D (Green): %.4g',pressure_archives(i,4))); 

set(Pressure_E_Legend,'String', sprintf('Pump E (Blue): %.4g',pressure_archives(i,5))); 

%current values for temperature 

set(Temp_A_Legend,'String', sprintf('Pump A (Black): %.4g', temperature_archives(i,1))); 

set(Temp_B_Legend,'String', sprintf('Pump B (Red): %.4g',temperature_archives(i,2))); 

set(Temp_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',temperature_archives(i,3))); 

set(Temp_D_Legend,'String', sprintf('Pump D (Green): %.4g',temperature_archives(i,4))); 

set(Temp_E_Legend,'String', sprintf('Pump E (Blue): %.4g',temperature_archives(i,5))); 

elapsedTime = toc(startTime);  

% Convert elapsed time to hours, minutes, and seconds  

hours = floor(elapsedTime / 3600);  

minutes = floor(mod(elapsedTime, 3600) / 60);  

seconds = floor(mod(elapsedTime, 60));  

% Format the time as a string  

time2String = sprintf('%d:%02d:%02d', hours, minutes, seconds);  

% Update text display  

set(TimerText, 'String', time2String);  

set(pressureTimer, 'String', time2String); 

set(tempTimer, 'String', time2String); 

drawnow; % Force update of the display  

% Pause for the update interval  

i = i+1; 

end 

The pause is near the end, but not right at the comment at the end that says it is there. The comment is leftover from an earlier pause location. We are not very good at MATLab coding and have been going through the ringer.



clear;  

% Connect to Arduino  

a = arduino;  

% User input for Flowrate info  

num_pumps = input('Please enter the number of pumps you are using (ex. 4): ', 's');  

number_pumps = str2double(num_pumps);  

pump_list = cell(1, number_pumps);  

flowrate_archives = zeros(172800,6);  

for j = 1:number_pumps  

pump_input = input('Please enter the pumps in alphabetical order one at a time: ', 's');  

pump_list{j} = pump_input; 

pump_rate = input('Please enter the flow rate for this pump: ','s'); 

pump_rate_num = str2double(pump_rate); 

pump_rate_list(j) = pump_rate_num; 

end 

%User input for temperature info 

temperature_archives = zeros(172800,5); 

num_temp = input('Please enter the number of temperature sensors you are using: ','s'); 

number_temp = str2double(num_temp); 

temp_list = cell(1,number_temp); 

for j = 1:number_temp 

temp_input = input('Please enter one of temperature sensors you are using: ','s'); 

temp_list{j} = temp_input; 

temp_rate = input('Please enter the temperature you would like to keep that sensor at in C: ','s'); 

temp_rate_num = str2double(temp_rate); 

temp_rate_list(j) = temp_rate_num; 

end 

%User input for pressure info 

pressure_archives = zeros(172800,5); 

num_pressure = input('Please enter the number of pressure sensors you are using (ex. 4): ', 's');  

number_pressure = str2double(num_pressure);  

pressure_list = cell(1, number_pressure);  

for j = 1:number_pressure  

pressure_input = input('Please enter the pressure sensors you are using in alphabetical order one at a time: ', 's');  

pressure_list{j} = pressure_input; 

pressure_rate = input('Please enter the pressure for this sensor in psi: ','s'); 

pressure_rate_num = str2double(pressure_rate); 

pressure_rate_list(j) = pressure_rate_num; 

end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% Positions and colors for the text elements  

Legend_Positions = [0.87, 0.83, 0.79, 0.75, 0.71, 0.67]; 

colors = {'black','red','#EDB120','#77AC30','#4DBEEE','m'}; 

pump_ids = {'A','B','C','D','E','F'};  

pump_letters = 'ABCDEF'; 

temp_numbers = '12345'; 

pressure_letters = 'ABCEE'; 

% Create figures for flowrate data 

figure1 = figure('Name', 'Flowrate Data');  

subplot(2, 1, 1);  

PumpA = animatedline('Color', 'black');  

PumpB = animatedline('Color', 'red');  

PumpC = animatedline('Color', '#EDB120');  

PumpD = animatedline('Color', '#77AC30'); 

PumpE = animatedline('Color', '#4DBEEE'); 

PumpF = animatedline('Color', 'm'); 

title('All Flowrate Data Points');  

xlabel('# of Samples');  

ylabel('Flowrate (mL/min)');  

ylim([0, 11]);  

% Timer for Flowrate  

TimerText = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized', ...  

'Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00'); 

% Legend for flowrate data  

Legend_Text = cell(1, length(pump_ids));  

Pos_Update = 1;  

for idx = 1:length(pump_ids)  

if ismember(pump_ids(idx), pump_list)  

Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Pump_A_Legend = Legend_Text{1};  

Pump_B_Legend = Legend_Text{2};  

Pump_C_Legend = Legend_Text{3};  

Pump_D_Legend = Legend_Text{4};  

Pump_E_Legend = Legend_Text{5};  

Pump_F_Legend = Legend_Text{6};  

% Flowrate Average Plot 

subplot(2,1,2); 

PumpA_Avg = animatedline('Color', 'black');  

PumpB_Avg = animatedline('Color', 'red');  

PumpC_Avg = animatedline('Color', '#EDB120');  

PumpD_Avg = animatedline('Color', '#77AC30'); 

PumpE_Avg = animatedline('Color', '#4DBEEE'); 

PumpF_Avg = animatedline('Color', 'm'); 

title('Average (5sec) Flowrate');  

xlabel('# of Samples');  

ylabel('Flowrate (mL/min)');  

ylim([0, 11]); 

% Create figures for pressure data  

figure2 = figure('Name', 'Pressure Data');  

subplot(2, 1, 1);  

pressure_1 = animatedline('Color','black'); 

pressure_2 = animatedline('Color','red'); 

pressure_3 = animatedline('Color','#EDB120'); 

pressure_4 = animatedline('color','#77AC30'); 

pressure_5 = animatedline('color','#4DBEEE'); 

title('Overall Pressure');  

xlabel('# of Samples'); 

ylabel('Pressure (psi)');  

ylim([0, 200]); 

%Lenegd for pressure data 

Pressure_Legend_Text = cell(1, length(pressure_letters));  

Pos_Update = 1;  

for idx = 1:length(pressure_letters)  

if ismember(pressure_letters(idx), pressure_list)  

Pressure_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Pressure_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Pressure_A_Legend = Pressure_Legend_Text{1};  

Pressure_B_Legend = Pressure_Legend_Text{2};  

Pressure_C_Legend = Pressure_Legend_Text{3};  

Pressure_D_Legend = Pressure_Legend_Text{4};  

Pressure_E_Legend = Pressure_Legend_Text{5};  

%Timer on pressure graph 

pressureTimer = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized','Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00');  

subplot(2,1,2); 

pressure_1_avg = animatedline('color','black'); 

pressure_2_avg = animatedline('color','red'); 

pressure_3_avg = animatedline('color','#EDB120'); 

pressure_4_avg = animatedline('color','#77AC30'); 

pressure_5_avg = animatedline('color','#4DBEEE'); 

title('Average (5sec) Pressure');  

xlabel('# of Samples'); 

ylabel('Pressure (psi)');  

ylim([0, 200]); 

%Create figures for temperature data  

figure3 = figure('Name', 'Temperature Data');  

subplot(2, 1, 2);  

temp_1_avg = animatedline('Color','Black'); 

temp_2_avg = animatedline('color','red'); 

temp_3_avg = animatedline('color','#EDB120'); 

temp_4_avg = animatedline('color','#77AC30'); 

temp_5_avg = animatedline('color','#4DBEEE'); 

title('Average (5sec) Temperature');  

xlabel('# of Samples'); ylabel('Degrees Celcius');  

ylim([0, 100]);  

% Legend for the temperature graphs 

Temp_Legend_Text = cell(1, length(temp_numbers));  

Pos_Update = 1;  

for idx = 1:length(temp_numbers)  

if ismember(temp_numbers(idx), temp_list)  

Temp_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 Legend_Positions(Pos_Update) 0.15 0.04], 'ForegroundColor', colors{idx});  

Pos_Update = Pos_Update + 1;  

else  

Temp_Legend_Text{idx} = uicontrol('Style', 'text', 'FontSize', 7, 'Units', 'normalized', 'Position', [0.15 9999 0.15 0.04]);  

end  

end  

% Assign text handles to individual variables if needed  

Temp_A_Legend = Temp_Legend_Text{1};  

Temp_B_Legend = Temp_Legend_Text{2};  

Temp_C_Legend = Temp_Legend_Text{3};  

Temp_D_Legend = Temp_Legend_Text{4};  

Temp_E_Legend = Temp_Legend_Text{5}; 

%timer on temperature graph 

tempTimer = uicontrol('Style', 'text', 'FontSize', 10, 'Units', 'normalized','Position', [0.72 0.93 0.15 0.05], 'String', '0:00:00');  

subplot(2, 1, 1);  

temp_1 = animatedline('Color','Black'); 

temp_2 = animatedline('color','red'); 

temp_3 = animatedline('color','#EDB120'); 

temp_4 = animatedline('color','#77AC30'); 

temp_5 = animatedline('color','#4DBEEE'); 

title('Temperature Overall'); 

xlabel('# of Samples');  

ylabel('Degrees Celcius');  

ylim([0, 100]);  

% Figure 4 to display error messages 

figure4 = figure('Name','Flowrate Error'); 

flowrate_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.2 0.6 0.6], 'String', ' '); 

pressure_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.4 0.6 0.6], 'String', ' '); 

temp_warning_text = uicontrol('Style', 'text', 'FontSize', 15, 'Units', 'normalized','Position', [0.2 0.6 0.6 0.6], 'String', ' '); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% Main loop to update the plots  

out_of_range = zeros(1, length(pump_list)); 

i = 1; 

% Initialize start time at the last moment before the loop starts 

startTime = tic; 

while true  

% Read voltages from Arduino  

flow_voltages = zeros(1,6); 

for j = 1:6 

flow_voltages(j) = readVoltage(a,['A',num2str(j-1)]); 

end 

%Read temp voltages from Arduino 

temp_voltages = zeros(1,5); 

for j = 1:5 

temp_voltages(j) = readVoltage(a,['A',num2str(j+5)]); 

end 

%Read pressure voltages from Arduino 

pressure_voltages = zeros(1,5); 

for j = 1:5 

pressure_voltages(j) = readVoltage(a,['A',num2str(j+10)]); 

end 

% Convert voltages to flowrates  

flowrates = (flow_voltages - 0.8077) / 0.2876;  

flowrate_archives(i,:) = flowrates; 

%Convert voltages to temperatures 

temperatures = (temp_voltages-1.25)/0.005; 

temperature_archives(i,:) = temperatures; 

%Convert voltages to pressure 

pressures = (pressure_voltages-0.8613)/0.0176; 

pressure_archives(i,:) = pressures; 

%Flowrate +- 10% check 

flowrate_warning_message = ' '; 

for j= 1:length(pump_list) 

lower_bound = pump_rate_list(j) * 0.9; 

upper_bound = pump_rate_list(j) * 1.1; 

if flowrates(j)<lower_bound || flowrates(j)>upper_bound 

out_of_range = out_of_range + 1; 

%fprintf("PUMP %s OUT OF +-10% RANGE FOR THE PAST %s SAMPLES",flowrates(j),out_of_range) 

flowrate_warning_message = sprintf('PUMP %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',pump_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(flowrate_warning_message) 

set(flowrate_warning_text, 'string', ' '); 

else 

set(flowrate_warning_text, 'string', flowrate_warning_message); 

end 

%Pressure +/-10% check 

pressure_warning_message = ' '; 

for j= 1:length(pressure_list) 

lower_bound = pressure_rate_list(j) * 0.9; 

upper_bound = pressure_rate_list(j) * 1.1; 

if pressures(j)<lower_bound || pressures(j)>upper_bound 

out_of_range = out_of_range + 1; 

%fprintf("PUMP %s OUT OF +-10% RANGE FOR THE PAST %s SAMPLES",flowrates(j),out_of_range) 

pressure_warning_message = sprintf('PRESSURE SENSOR %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',pressure_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(pressure_warning_message) 

set(pressure_warning_text, 'string', ' '); 

else 

set(pressure_warning_text, 'string', pressure_warning_message); 

end 

%Temperature +/-10% check 

temp_warning_message = ' '; 

for j= 1:length(temp_list) 

lower_bound = temp_rate_list(j) * 0.9; 

upper_bound = temp_rate_list(j) * 1.1; 

if temperatures(j)<lower_bound || temperatures(j)>upper_bound 

out_of_range = out_of_range + 1; 

temp_warning_message = sprintf('TEMPERATURE SENSOR %s IS OUT OF +- 10%% RANGE FOR THE PAST %d SAMPLES\n',temp_list{j}, out_of_range(j));  

else 

out_of_range(j) = 0; 

end 

end 

if isempty(temp_warning_message) 

set(temp_warning_text, 'string', ' '); 

else 

set(temp_warning_text, 'string', temp_warning_message); 

end 

% Update animated lines for Flowrates 

Exp_Flow_Lines = [PumpA,PumpB,PumpC,PumpD,PumpE,PumpF]; 

Average_Flow_lines = [PumpA_Avg,PumpB_Avg,PumpC_Avg,PumpD_Avg,PumpE_Avg,PumpF_Avg]; 

%overall flowrate 

for j = 1:length(pump_letters)  

if ismember(pump_letters(j), pump_list)  

pump_index = find(strcmp(pump_list, pump_letters(j))); 

if ~isempty(pump_index) 

addpoints(Exp_Flow_Lines(j), i, flowrates(j));  

else  

addpoints(Exp_Flow_Lines(j), i, 0);  

end 

else 

addpoints(Exp_Flow_Lines(j),i,0) 

end 

end  

%Average flowrate 

if i >= 5  

rolling_flowrate_averages = zeros(1, length(pump_letters));  

for j = 1:length(pump_letters)  

rolling_flowrate_averages(j) = mean(flowrate_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(pump_letters) 

if ismember(pump_letters(j), pump_list) 

addpoints(Average_Flow_lines(j), i, rolling_flowrate_averages(j)); 

else 

addpoints(Average_Flow_lines(j), i, 0); 

end 

end 

end  

%Update Animated Lines for Temperature 

Exp_temp_lines = [temp_1,temp_2,temp_3,temp_4,temp_5]; 

Avg_temp_lines = [temp_1_avg,temp_2_avg,temp_3_avg,temp_4_avg,temp_5_avg]; 

%Overall temperature 

for j = 1:length(temp_numbers) 

if ismember(temp_numbers(j), temp_list)  

temp_index = find(strcmp(temp_list, temp_numbers(j))); 

if ~isempty(temp_index) 

addpoints(Exp_temp_lines(j), i, temperatures(j));  

else  

addpoints(Exp_temp_lines(j), i, 0);  

end 

else 

addpoints(Exp_temp_lines(j),i,0) 

end 

end  

%Average temperature 

if i >= 5  

rolling_temperature_averages = zeros(1, length(temp_numbers));  

for j = 1:length(temp_numbers)  

rolling_temperature_averages(j) = mean(temperature_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(temp_numbers) 

if ismember(temp_numbers(j), temp_list) 

addpoints(Avg_temp_lines(j), i, rolling_temperature_averages(j)); 

else 

addpoints(Avg_temp_lines(j), i, 0); 

end 

end 

end 

Exp_pressure_lines = [pressure_1,pressure_2,pressure_3,pressure_4,pressure_5]; 

Avg_pressure_lines = [pressure_1_avg,pressure_2_avg,pressure_3_avg,pressure_4_avg,pressure_5_avg]; 

%overall pressure 

for j = 1:length(pressure_letters) 

if ismember(pressure_letters(j), pressure_list)  

pressure_index = find(strcmp(pressure_list, pressure_letters(j))); 

if ~isempty(pressure_index) 

addpoints(Exp_pressure_lines(j), i, pressures(j));  

else  

addpoints(Exp_pressure_lines(j), i, 0);  

end 

else 

addpoints(Exp_pressure_lines(j),i,0); 

end 

end  

%Average pressure 

if i >= 5  

rolling_pressure_averages = zeros(1, length(pressure_letters));  

for j = 1:length(pressure_letters)  

rolling_pressure_averages(j) = mean(pressure_archives(max(1, i-4):i,j)); 

end 

for j = 1:length(pressure_letters) 

if ismember(pressure_letters(j), pressure_list) 

addpoints(Avg_pressure_lines(j), i, rolling_pressure_averages(j)); 

else 

addpoints(Avg_pressure_lines(j), i, 0); 

end 

end 

end 

% Draw updates  

drawnow;  



% X axis for flow rate 

if i > 180  

for j = 1:length(pump_letters)  

set(Average_Flow_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(pump_letters)  

set(Average_Flow_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end  

% X axis for pressure 

if i > 180  

for j = 1:length(temp_numbers)  

set(Avg_pressure_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(temp_numbers)  

set(Avg_pressure_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end 

%x axis for temp 

if i > 180  

for j = 1:length(pressure_letters)  

set(Avg_temp_lines(j).Parent, 'XLim', [i-180, i]);  

end  

else  

for j = 1:length(pressure_letters)  

set(Avg_temp_lines(j).Parent, 'XLim', [0, max(1, i)]);  

end  

end 

%Current Values for flowrate 

set(Pump_A_Legend,'String', sprintf('Pump A (Black): %.4g', flowrate_archives(i,1))); 

set(Pump_B_Legend,'String', sprintf('Pump B (Red): %.4g',flowrate_archives(i,2))); 

set(Pump_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',flowrate_archives(i,3))); 

set(Pump_D_Legend,'String', sprintf('Pump D (Green): %.4g',flowrate_archives(i,4))); 

set(Pump_E_Legend,'String', sprintf('Pump E (Blue): %.4g',flowrate_archives(i,5))); 

set(Pump_F_Legend,'String', sprintf('Pump F (Pink): %.4g',flowrate_archives(i,6))); 

%current values for presure 

set(Pressure_A_Legend,'String', sprintf('Pump A (Black): %.4g', pressure_archives(i,1))); 

set(Pressure_B_Legend,'String', sprintf('Pump B (Red): %.4g',pressure_archives(i,2))); 

set(Pressure_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',pressure_archives(i,3))); 

set(Pressure_D_Legend,'String', sprintf('Pump D (Green): %.4g',pressure_archives(i,4))); 

set(Pressure_E_Legend,'String', sprintf('Pump E (Blue): %.4g',pressure_archives(i,5))); 

%current values for temperature 

set(Temp_A_Legend,'String', sprintf('Pump A (Black): %.4g', temperature_archives(i,1))); 

set(Temp_B_Legend,'String', sprintf('Pump B (Red): %.4g',temperature_archives(i,2))); 

set(Temp_C_Legend,'String', sprintf('Pump C (Yellow): %.4g',temperature_archives(i,3))); 

set(Temp_D_Legend,'String', sprintf('Pump D (Green): %.4g',temperature_archives(i,4))); 

set(Temp_E_Legend,'String', sprintf('Pump E (Blue): %.4g',temperature_archives(i,5))); 

elapsedTime = toc(startTime);  

% Convert elapsed time to hours, minutes, and seconds  

hours = floor(elapsedTime / 3600);  

minutes = floor(mod(elapsedTime, 3600) / 60);  

seconds = floor(mod(elapsedTime, 60));  

% Format the time as a string  

time2String = sprintf('%d:%02d:%02d', hours, minutes, seconds);  

% Update text display  

set(TimerText, 'String', time2String);  

set(pressureTimer, 'String', time2String); 

set(tempTimer, 'String', time2String); 

drawnow; % Force update of the display  

%Pause for 1 second between updates  

pause(1);  

i = i+1; 

end 

The above is the code with the pause moved to the end for ease of viewing.

How about you post your Arduino code? After all, this is an Arduino forum, not a MATLAB forum :thinking:

There is no arduino code, we are merely using the arduino to interpret the voltage reading from our sensors. I am on the arduino forum because this is a question about the arduino, not the code. We are concerned about overloading arduino without a pause or without a sufficient pause.

You are mistaken. An Arduino cannot communicate with sensors or Matlab without running code.

How fast is the baud rate for connecting to the Arduino? That is probably the limiting factor for transferring data

There is an Arduino support package for MATLAB that allows MATLAB to use the Arduino simply for it's ability to read voltages/send outputs etc. etc. However, that is besides the point. I just want want to know if you are able to read voltage from the 16 analog input ports on the Arduino every second or less without overheating it. The only thing the Arduino is doing is reading voltage.

Checking this now

what Arduino are you using? I don't know of any arduino that has 16 analog pins. It sounds like you have some custom board?

Edit: my bad - Mega 2560 has 16 analog ports

Mega 2560

Please state which Arduino you have.

The default analog read rate is typically some microseconds per channel (110 us for the Mega), and if analog input voltages are within the safe limits, there is absolutely no concern about heating.

1 Like

Did this Mega 2560 come with MATLAB or something? Even if there is a "support package", you still need to program the Mega so it reads/reports the analog pins.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.