clear all; close all;
a = arduino('COM4', 'Uno');
disp 'Connection received!'
pause (3);
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [2.4 3.8];
title('Moisture Sensor Voltage vs Time (live)');
xlabel('Time [HH:MM:SS]');
ylabel('Voltage [volt]');
stop = false;
startTime = datetime('now');
while ~stop
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
while (voltage < 2.9)
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
while (voltage > 2.9)
writeDigitalPin(a,'D2',1);
disp 'Pump is on!'
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
if (voltage < 2.9)
writeDigitalPin(a,'D2',0)
disp 'Pump is off!'
while (voltage < 2.9)
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
end
end
end
Notice how I repeat the following lines over and over again?
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
How can I simplify this code so that I don't need to repeat all 6 lines every loop and can call a function to do it.

1 Kommentar

Peter Perkins
Peter Perkins am 9 Dez. 2022
Slightly off-topic: not clear to me that you need to use the old datenum format for your times. I think you should be able to plot against durations here, and will ultimately be happier.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Askic V
Askic V am 6 Dez. 2022

0 Stimmen

Why not trying straightforward:
function readVoltageDraw(a, startTime, ax)
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
and call it just like this:
while (voltage > 2.9)
writeDigitalPin(a,'D2',1);
disp 'Pump is on!'
readVoltageDraw(a, startTime, ax)
...

4 Kommentare

clear all; close all;
a = arduino('COM3', 'Uno');
disp 'Connection received!'
pause (3);
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [2.4 3.8];
title('Moisture Sensor Voltage vs Time (live)');
xlabel('Time [HH:MM:SS]');
ylabel('Voltage [volt]');
stop = false;
startTime = datetime('now');
while ~stop
readVoltageDraw(a, startTime, ax)
while (voltage < 2.9)
readVoltageDraw(a, startTime, ax)
end
while (voltage > 2.9)
writeDigitalPin(a,'D2',1);
disp 'Pump is on!'
readVoltageDraw(a, startTime, ax)
if (voltage < 2.9)
writeDigitalPin(a,'D2',0)
disp 'Pump is off!'
while (voltage < 2.9)
readVoltageDraw(a, startTime, ax)
end
end
end
end
function readVoltageDraw(a, startTime, ax)
voltage = readVoltage(a, 'A1');
t = datetime('now') - startTime;
addpoints(h,datenum(t),voltage)
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
I have rewritten the code as such. However I am receving this error message:
Unrecognized function or variable 'h'.
Error in MinorProjecthelp>readVoltageDraw (line 37)
addpoints(h,datenum(t),voltage)
Error in MinorProjecthelp (line 16)
readVoltageDraw(a, startTime, ax)
Torsten
Torsten am 6 Dez. 2022
If "h" is needed in "readVoltageDraw", you must pass it there as input argument...
Md Zarif Raiyan
Md Zarif Raiyan am 6 Dez. 2022
Just did that, thank you, it works now :)
Askic V
Askic V am 6 Dez. 2022
Yes, I forgot to specify animated handle h as an input argument. Thank you Torsten for point that out.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by