-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
72 lines (58 loc) · 1.75 KB
/
main.m
File metadata and controls
72 lines (58 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function Soil_Condition = TEST
% TEST.m
% Real-time soil moisture monitoring and irrigation control using Arduino
% Reads voltage from A0, applies threshold classification,
% controls pump on D2, and plots live moisture trends.
% Initialize Arduino connection
a = arduino("COM5",'uno');
% Threshold states (in volts)
enough = 1.6;
alittlebitmore = 1.7;
dry = 1.9;
% Start live graph
figure(1)
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [-0.1 5];
ylabel('Voltage [volts]');
xlabel('Time [seconds]');
title('Voltage Moisture Sensor vs Time (Live)');
startTime = datetime('now');
stop = 0;
while ~stop
% Read voltage from analog pin A0
voltage = readVoltage(a,'A0');
% Linear calibration transformation (y = mx + b)
newy = (-0.666 * voltage) + 1.332;
% Compute elapsed time
t = datetime('now') - startTime;
% Plot real-time data
addpoints(h, datenum(t), newy)
datetick('x','keeplimits')
drawnow
% Threshold-based control logic
% Soil very dry — water more
if (voltage >= dry)
Soil_Condition = 'A LOT MORE';
disp('A LOT MORE')
writeDigitalPin(a,'D2',1);
pause(2);
writeDigitalPin(a,'D2',0);
% Soil moderately dry — water slightly
elseif (voltage <= alittlebitmore) && (voltage > enough)
Soil_Condition = 'A LITTLE MORE';
disp('A LITTLE MORE')
writeDigitalPin(a,'D2',1);
pause(2);
writeDigitalPin(a,'D2',0);
% Soil sufficiently moist — no watering
elseif (voltage < enough)
Soil_Condition = 'ENOUGH';
disp('ENOUGH')
writeDigitalPin(a,'D2',0);
% Optional stop condition via digital pin D6
stop = readDigitalPin(a,'D6');
end
end
end