-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode
222 lines (176 loc) · 7.43 KB
/
code
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
properties (Access = private)
arduino;
isReading = false; threshold_v = 1; moistureLine; thresholdLine; startTime;
%
% dataBuffer; bufferSize = 10; dataTimer;
end
%
% methods (Access = private)
% function collectData(app)
% newData = readData();
% app.dataBuffer = circshift(app.dataBuffer, -1);
% app.dataBuffer(end) = newData;
% end
% end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.arduino = arduino();
%
% app.dataBuffer = zeros(app.bufferSize, 1);
% app.dataTimer = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @(~,~)app.collectData);
% start(app.dataTimer);
configurePin(app.arduino, 'D4', 'DigitalOutput');
app.moistureLine = animatedline(app.UIAxes, 'Color', 'm','LineStyle','-','LineWidth',0.5);
app.thresholdLine = animatedline(app.UIAxes, 'Color', 'k','LineStyle','-','LineWidth',0.5);
legend(app.UIAxes,'Moisture Percentage','Treshold Line');
app.SoilIsWateringLamp.Color= [0 0 0];
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
app.startTime = datetime('now');
if ~isa(app.arduino, 'arduino')
app.arduino = arduino();
end
if ~app.isReading
app.isReading = true;
while app.isReading
analogValue = readVoltage(app.arduino, 'A1');
moisturePercentage = interp1([0,5], [100,0], analogValue);
%
% if ~isnumeric(moisturePercentage) || isnan(moisturePercentage) || moisturePercentage < 0 || moisturePercentage > 100
% continue;
% end
currentTime = datetime('now');
elapsedSeconds = seconds(currentTime - app.startTime);
addpoints(app.moistureLine, elapsedSeconds, moisturePercentage);
addpoints(app.thresholdLine, elapsedSeconds, app.threshold_v);
if moisturePercentage > app.threshold_v
writeDigitalPin(app.arduino, 'D4', 0);
app.SoilIsWateringLamp.Color=[0 0 0];
else
writeDigitalPin(app.arduino, 'D4', 1);
app.SoilIsWateringLamp.Color=[[0.47,0.67,0.19]];
end
end
end
end
% Button pushed function: ExitButton
function ExitButtonPushed(app, event)
delete(app);
end
% Value changed function: timeValue
function timeValueValueChanged(app, event)
duration = app.timeValue.Value;
app.UIAxes.XLim = [0 duration];
app.UIAxes.XTick = 0:duration/10:duration;
end
% Value changed function: ThresholdDropDown
function ThresholdDropDownValueChanged(app, event)
app.threshold_v = str2double(app.ThresholdDropDown.Value);
end
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
filetype = app.FileTypeDropDown.Value;
fileName = fullfile(getenv('USERPROFILE'), 'Desktop', 'Figure');
switch filetype
case '.jpg'
exportgraphics(app.UIAxes, [fileName '.jpg']);
case '.png'
exportgraphics(app.UIAxes, [fileName '.png']);
case '.fig'
exportgraphics(app.UIAxes, [fileName '.fig']);
end
end
% Selection changed function: ColorButtonGroup
function ColorButtonGroupSelectionChanged(app, event)
moistcolor = app.ColorButtonGroup.SelectedObject;
switch moistcolor.Text
case 'Red'
m_color = 'r';
case 'Blue'
m_color = 'b';
case 'Green'
m_color = 'g';
end
app.moistureLine.Color=m_color;
end
% Selection changed function: ThicknessButtonGroup
function ThicknessButtonGroupSelectionChanged(app, event)
moistthickness = app.ThicknessButtonGroup.SelectedObject;
switch moistthickness.Text
case 'Thin'
m_width= 0.3;
case 'Medium'
m_width=2;
case 'Thick'
m_width=4;
end
app.moistureLine.LineWidth=m_width;
end
% Selection changed function: LineStyleButtonGroup
function LineStyleButtonGroupSelectionChanged(app, event)
moiststyle = app.LineStyleButtonGroup.SelectedObject;
switch moiststyle.Text
case 'Dashed Dotted Line'
m_style= '-.';
case 'Dashed Line'
m_style='--';
case 'Dotted Line'
m_style= ':';
end
app.moistureLine.LineStyle=m_style;
end
% Selection changed function: ColorButtonGroup_2
function ColorButtonGroup_2SelectionChanged(app, event)
tresholdcolor = app.ColorButtonGroup_2.SelectedObject;
switch tresholdcolor.Text
case 'Red'
t_color = 'r';
case 'Blue'
t_color = 'b';
case 'Green'
t_color = 'g';
end
app.thresholdLine.Color=t_color;
end
% Value changed function: FileTypeDropDown
function FileTypeDropDownValueChanged(app, event)
filetype = app.FileTypeDropDown.Value;
end
% Value changed function: Switch
function SwitchValueChanged(app, event)
gridState = app.Switch.Value;
if strcmp(gridState, 'Grid On')
grid(app.UIAxes, 'on');
else if strcmp(gridState, 'Grid Off')
grid(app.UIAxes, 'off');
end
end
end
% Selection changed function: ThicknessButtonGroup_2
function ThicknessButtonGroup_2SelectionChanged(app, event)
tresholdthickness = app.ThicknessButtonGroup_2.SelectedObject;
switch tresholdthickness.Text
case 'Thin'
t_width= 0.3;
case 'Medium'
t_width=2;
case 'Thick'
t_width=4;
end
app.thresholdLine.LineWidth=t_width;
end
% Selection changed function: LineStyleButtonGroup_2
function LineStyleButtonGroup_2SelectionChanged(app, event)
tresholdstyle = app.LineStyleButtonGroup_2.SelectedObject;
switch tresholdstyle.Text
case 'Dashed Dotted Line'
t_style= '-.';
case 'Dashed Line'
t_style='--';
case 'Dotted Line'
t_style= ':';
end
app.thresholdLine.LineStyle=t_style;