-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_dec14a.ino
327 lines (285 loc) · 9.31 KB
/
sketch_dec14a.ino
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
int fanPin=4;
int fanPin1=5;
String inString = "";
int moistureSensorPin = A0;
int methaneSensorPin = A1;
int servoPin = 8;
Servo servo;
int action = 0;
int pumpPin = 7;
int pumpTimeToRun = 5000;
int waterLevelPin = 6;
int ventAngle=0;
int startPump=0;
int trigPin=11;
int echoPin=12;
double duration, cm, inches;
double binDepth=30;
int AmbientTemperature;
int Temperature;
int Moisture;
int MethanePPM;
int WaterLevel;
int ScrapeLevel;
void setup() {
Serial.begin(9600);
pinMode(pumpPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
dht.begin();
delay(1000);
sensor.begin();
servo.attach(servoPin);
servo.write(action);
Serial.println("================================");
}
void loop() {
readSensors();
Serial.println("");
Serial.println("==============================================");
analyze(AmbientTemperature,Temperature, Moisture, MethanePPM, WaterLevel, ScrapeLevel);
Serial.println("==============================================");
delay(1000);
Serial.println("");
Serial.println("");
}
void readSensors() {
AmbientTemperature=getAmbientTemperature();
Temperature=getTemperature();
Moisture=getMoisture();
MethanePPM=getMethanePPM();
WaterLevel=getWaterLevel();
ScrapeLevel=getScrapeLevel();
}
int getAmbientTemperature() {
Serial.print("Humidity: ");
Serial.print(dht.readHumidity());
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(dht.readTemperature(true));
Serial.println(" *F");
return round(dht.readTemperature(true));
}
int getTemperature() {
sensor.requestTemperatures();
int sensorValue = sensor.getTempFByIndex(0);
Serial.print("Compost Temperature is: ");
if (sensorValue < 0) {
sensorValue = 0;
}
Serial.print(sensorValue);
Serial.println(" *F");
return sensorValue;
}
int getMoisture() {
float sensorValue = analogRead(moistureSensorPin);
int calcSensorValue;
if (sensorValue < 750) {
calcSensorValue = 30;
}
else if (sensorValue < 900) {
calcSensorValue = 50;
}
else {
calcSensorValue = 65;
}
Serial.print("Compost Moisture is: ");
Serial.print(100-calcSensorValue);
Serial.println(" %");
return 100-calcSensorValue;
}
int getMethanePPM() {
float sensorValue = analogRead(methaneSensorPin);
double ppm = 10.938 * exp(1.7742 * (sensorValue * 5.0 / 4095));
if (ppm < 0) {
ppm = 0;
}
Serial.print("Methane PPM is: ");
Serial.println(ppm);
return ppm;
}
int getWaterLevel() {
pinMode(waterLevelPin, INPUT_PULLUP);
int waterLevelValue = digitalRead(waterLevelPin);
if (waterLevelValue < 0) {
waterLevelValue = 0;
}
Serial.print("Water Level is: ");
Serial.println(waterLevelValue);
return waterLevelValue;
}
int getScrapeLevel() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration=pulseIn(echoPin, HIGH);
cm=(duration/2)/29.1;
// inches=(duration/2)/74;
Serial.print("Scrape level = ");
// Serial.print(round(inches));
// Serial.print(" in, ");
Serial.print(round(cm));
Serial.println(" cm");
double binFill = binDepth;
if (cm <3) {
cm =0;
}
if (cm>binDepth) {
Serial.println("There was an error");
}
else {
binFill = binDepth - cm;
}
Serial.println(binFill);
delay(250);
return binFill;
}
void analyze(int AmbientTemperature,int Temperature, int Moisture, int MethanePPM, int WaterLevel, int ScrapeLevel) {
if (Moisture>60) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost moisture is too wet. Turn your compost and add 'green' (Nitrogen-rich) materials.");
if (Temperature<90) {
servo.write(0);
digitalWrite(fanPin, HIGH);
digitalWrite(fanPin1, LOW);
if (Temperature<=AmbientTemperature) {
Serial.println("Your compost temperature is lower than optimal. Turn compost and add 'green' (Nitrogen-rich) materials.");
}
else {
Serial.println("Your compost temperature is lower than optimal. The ambient temperature is low, so you should cover your compost to continue aerobic composting.");
}
}
else if (Temperature>=90 && Temperature<=140) {
Serial.println("Your compost is at optimal temperature.");
}
else if (Temperature>=140 && Temperature<=160) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost temperature is slightly higher than optimal. You may want to turn the compost and add 'brown' materials.");
}
else if (Temperature>=160 && Temperature<=175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached unhealthily temperature. Turn compost and add 'brown' (Carbon-rich) materials.");
}
else if (Temperature>175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached an unsafe temperature. Immediately turn the compost and add water.");
digitalWrite(pumpPin, HIGH);
delay(pumpTimeToRun);
digitalWrite(pumpPin, LOW);
}
}
else if (Moisture>=40 && Moisture<=60) {
servo.write(0);
digitalWrite(fanPin, HIGH);
digitalWrite(fanPin1, LOW);
Serial.println("Your compost is at optimal moisture levels.");
if (Temperature<90) {
servo.write(0);
digitalWrite(fanPin, HIGH);
digitalWrite(fanPin1, LOW);
if (Temperature<=AmbientTemperature) {
Serial.println("Your compost temperature is lower than optimal. Turn compost and add 'green' (Nitrogen-rich) materials.");
}
else {
Serial.println("Your compost temperature is lower than optimal. The ambient temperature is low, so you should cover your compost to continue aerobic composting.");
}
}
else if (Temperature>=90 && Temperature<=140) {
Serial.println("Your compost is at optimal temperature.");
}
else if (Temperature>=140 && Temperature<=160) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost temperature is slightly higher than optimal. You may want to turn the compost and add 'brown' materials.");
}
else if (Temperature>=160 && Temperature<=175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached unhealthily temperature. Turn compost and add 'brown' (Carbon-rich) materials.");
}
else if (Temperature>175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached an unsafe temperature. Immediately turn the compost and add water.");
digitalWrite(pumpPin, HIGH);
delay(pumpTimeToRun);
digitalWrite(pumpPin, LOW);
}
}
else if (Moisture<40) {
servo.write(0);
digitalWrite(fanPin, HIGH);
digitalWrite(fanPin1, LOW);
Serial.println("Your compost is too dry and requires your attention. You need to turn and water your compost.");
digitalWrite(pumpPin, HIGH);
delay(pumpTimeToRun);
digitalWrite(pumpPin, LOW);
if (Temperature<90) {
servo.write(0);
digitalWrite(fanPin, HIGH);
digitalWrite(fanPin1, LOW);
if (Temperature<=AmbientTemperature) {
Serial.println("Your compost temperature is lower than optimal. Turn compost and add 'green' (Nitrogen-rich) materials.");
}
else {
Serial.println("Your compost temperature is lower than optimal. The ambient temperature is low, so you should cover your compost to continue aerobic composting.");
}
}
else if (Temperature>=90 && Temperature<=140) {
Serial.println("Your compost is at optimal temperature.");
}
else if (Temperature>=140 && Temperature<=160) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost temperature is slightly higher than optimal. You may want to turn the compost and add 'brown' materials.");
}
else if (Temperature>=160 && Temperature<=175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached unhealthily temperature. Turn compost and add 'brown' (Carbon-rich) materials.");
}
else if (Temperature>175) {
servo.write(180);
digitalWrite(fanPin, LOW);
digitalWrite(fanPin1, HIGH);
Serial.println("Your compost has reached an unsafe temperature. Immediately turn the compost and add water.");
digitalWrite(pumpPin, HIGH);
delay(pumpTimeToRun);
digitalWrite(pumpPin, LOW);
}
}
if (WaterLevel==0) {
Serial.println("Your need to add water to your water reservoir");
}
if (ScrapeLevel<10) {
Serial.println("Your dustbin is full. Move scrape to compostbin");
}
}