1
2
3
4
5
6
7close all
8instrreset
9clear
10clc
11
12
13waitTime = 10;
14
15
16
17
18s = serial('/dev/cu.usbmodem411','BAUD',9600);
19
20figure
21color = ['b', 'r', 'g', 'm', 'c', 'b', 'r', 'g', 'm', 'c'];
22for i = 1:5
23 h(i) = animatedline('Color',color(i),'LineWidth',2);
24end
25axh = gca;
26axh.YGrid = 'on';
27axh.YLim = [30 80];
28xlabel('Time')
29ylabel('Humidity (%)')
30legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',...
31 'Location','NorthWest')
32
33figure
34for i = 6:10
35 h(i) = animatedline('Color',color(i),'LineWidth',2);
36end
37axt = gca;
38axt.YGrid = 'on';
39axt.YLim = [10 40];
40xlabel('Time')
41ylabel('Temperature (\\circC)')
42legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',...
43 'Location','NorthWest')
44
45stop = false;
46waitTime = duration(0,waitTime,0);
47startTime = datetime('now');
48t = datetime('now') - startTime;
49
50while ~stop && t < waitTime
51
52
53 fopen(s);
54 idn = fscanf(s);
55 fclose(s);
56
57
58 C = strsplit(idn,':');
59
60
61 serialData = str2double(C);
62
63
64 corrData = serialData;
65
66
67
68
69
70 disp(corrData)
71
72
73 t = datetime('now') - startTime;
74
75
76 for i = 1:5
77 addpoints(h(i),datenum(t),corrData(i))
78 end
79
80
81 axh.XLim = datenum([t-seconds(600) t]);
82 datetick('x','keeplimits')
83 drawnow
84
85
86 for i = 6:10
87 addpoints(h(i),datenum(t),corrData(i))
88 end
89
90
91 axt.XLim = datenum([t-seconds(600) t]);
92 datetick('x','keeplimits')
93 drawnow
94
95
96 if str2double(C{end}) == 999
97 stop = true;
98 end
99end
100
101
102if stop
103 disp('Data acquisition ended because the STOP button has been pressed')
104else
105 disp('Data acquisition ended because the TIME limit has been reached')
106end
107
108
109
110for i = 1:5
111 [~,humLogs(i,:)] = getpoints(h(i));
112 [timeLogs,tempLogs(i,:)] = getpoints(h(i+5));
113end
114timeSecs = (timeLogs-timeLogs(1))*24*3600;
115
116figure
117subplot(1,2,1)
118plot(timeSecs,humLogs,'LineWidth',2)
119grid on
120ax = gca;
121ylim([round(ax.YLim(1)-2), round(ax.YLim(2)+2)])
122xlabel('Elapsed time (s)')
123ylabel('Humidity (%)')
124
125subplot(1,2,2)
126timeSecs = (timeLogs-timeLogs(1))*24*3600;
127plot(timeSecs,tempLogs,'LineWidth',2)
128hold off, grid on
129ax = gca;
130ylim([round(ax.YLim(1)-2), round(ax.YLim(2)+2)])
131xlabel('Elapsed time (s)')
132ylabel('Temperature (\\circC)')
133legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',...
134 'Location','Best')
135
136
137
138T = table(timeSecs',humLogs',tempLogs','VariableNames',...
139 {'Time_s','Relative_Humidity','Temperature_C'});
140filename = 'Humidity_and_Temperature_Data.xls';
141
142
143if exist(filename,'file')
144 delete(filename)
145end
146
147
148writetable(T,filename)
149
150fprintf('Results table with
151',...
152 length(timeSecs),filename)
153
154
155
156for sensor = 1:5
157
158
159 smoothHum = smooth(humLogs(sensor,:),25);
160 smoothTemp = smooth(tempLogs(sensor,:),25);
161
162
163 humMax = 1.02 * smoothHum;
164 humMin = 0.98 * smoothHum;
165
166
167 humMaxW = 1.05 * smoothHum;
168 humMinW = 0.95 * smoothHum;
169
170
171 tempMax = smoothTemp + 0.5;
172 tempMin = smoothTemp - 0.5;
173
174 figure
175 subplot(1,2,1), hold on
176 plot(timeSecs,humLogs(sensor,:),'b','LineWidth',2)
177 plot(timeSecs,smoothHum,'r','LineWidth',1)
178 plot(timeSecs,humMin,'r--','LineWidth',2)
179 plot(timeSecs,humMax,'r--','LineWidth',2)
180 plot(timeSecs,humMinW,'m--','LineWidth',1)
181 plot(timeSecs,humMaxW,'m--','LineWidth',1)
182 hold off, grid on, ylim([round(min(humMinW))-2, round(max(humMaxW))+2])
183 xlabel('Elapsed time (s)')
184 ylabel('Humidity (%)')
185 title(['Humidity data, average, and uncertainty for sensor ',num2str(sensor)])
186
187 subplot(1,2,2), hold on
188 plot(timeSecs,tempLogs(sensor,:),'b','LineWidth',2)
189 plot(timeSecs,smoothTemp,'r','LineWidth',1)
190 plot(timeSecs,tempMin,'r--','LineWidth',2)
191 plot(timeSecs,tempMax,'r--','LineWidth',2)
192 hold off, grid on, ylim([round(min(tempMin))-2, round(max(tempMax))+2])
193 xlabel('Elapsed time (s)')
194 ylabel('Temperature (\\circC)')
195 title(['Temperature data, average, and uncertainty for sensor ',num2str(sensor)])
196
197end