This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
358 lines (303 loc) · 8.09 KB
/
main.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define DELAY_STATE_CHANGE 500
#define PIN_MOISTURE A1
#define PIN_STATE_TRIGGER 3
#define MOISTURE_THRESHOLD 500
#define PIN_NEOPIXEL 2
#define NEOPIXEL_LED_COUNT 5
int valStateTrigger = 0;
int valMoisture = 0;
boolean stateMode = false;
boolean gotWater = false;
boolean saidHello = false;
boolean saidGoodbye = false;
boolean needsWater = false;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(NEOPIXEL_LED_COUNT, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
void setup()
{
// Pin setup
pinMode(PIN_STATE_TRIGGER, INPUT_PULLUP);
pinMode(PIN_MOISTURE, INPUT);
pinMode(PIN_NEOPIXEL, OUTPUT);
// Neopixel setup
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.clear(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
// Serial setup
Serial.begin(9600);
}
// ----------------------------------------------------------------------------
// NEOPIXEL SEQUENCES
//
void showGoodbye()
{
strip.clear();
// show static green
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 100, 0);
}
strip.show();
// fade from green to off
for (uint32_t i = strip.numPixels(); i > 0; i--)
{
for (int j = 100; j > 0; j--)
{
strip.setPixelColor(i - 1, 0, j, 0);
strip.show();
delay(10);
}
}
strip.clear();
}
void showIsAwake()
{
// show static green
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 10, 50, 0);
}
strip.show();
}
void showNeedsWatering()
{
// // fade from greenish to reddish color
// // decrease green color
// for (uint32_t i = strip.numPixels() - 1; i > -1; i--)
// {
// for (int j = 100; j > 19; j--)
// {
// strip.setPixelColor(i, 25, j, 0);
// strip.show();
// delay(2 * i + i);
// }
// }
// // increase red color
// for (uint32_t i = 0; i < strip.numPixels(); i++)
// {
// for (int j = 25; j < 81; j++)
// {
// strip.setPixelColor(i, j, 20, 0);
// strip.show();
// delay(10);
// }
// }
// show static red
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 100, 0, 0);
}
strip.show();
}
void showWink()
{
// "wink" by turning off the light for a short time twice
// remember current color for each pixel
uint32_t colors[strip.numPixels()];
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
colors[i] = strip.getPixelColor(i);
}
// wink twice
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
delay(60);
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, colors[i]);
}
strip.show();
delay(120);
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
delay(60);
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, colors[i]);
}
strip.show();
}
void showGreeting()
{
strip.clear();
// fade from off to green
for (uint32_t i = 0; i < strip.numPixels(); i++)
{ // For each pixel in strip...
for (int j = 0; j < 101; j++)
{
strip.setPixelColor(i, 25, j, 0);
strip.show();
delay(10);
}
for (int j = 0; j < 26; j++)
{
strip.setPixelColor(i, j, 100, 0);
strip.show();
}
}
// wink twice
showWink();
}
// Some functions of our own for creating animated effects -----------------
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait)
{
strip.clear();
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256)
{
for (uint32_t i = 0; i < strip.numPixels(); i++)
{ // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
void showThanksForWatering()
{
strip.clear();
// perform awesome animations...
rainbow(10); // rainbow demo already looks smooth and elegant
showWink();
delay(1000);
showWink();
delay(1000);
rainbow(5);
// turn off
for (uint32_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
// ----------------------------------------------------------------------------
// CONTROLLER
//
void signalHello()
{
Serial.println("Signal hello");
// 1. play hello sequence
showGreeting();
delay(2000);
Serial.println("Finished Signal hello");
}
void signalGoodbye()
{
Serial.println("Signal goodbye");
// 1. play goodbye sequence
showGoodbye();
delay(2000);
Serial.println("Finished Signal goodbye");
}
void signalForWatering()
{
Serial.println("Signal for watering");
// play LED sequence for waiting for water
// should have no delay!
showNeedsWatering();
}
void thanksForWatering()
{
Serial.println("Signal got some water");
// play LED sequence for gotWater
// reset state for watering
showThanksForWatering();
delay(2000);
Serial.println("Finished Signal got some water");
}
void checkForPause()
{
}
void checkForDrinking()
{
}
void checkForWatering()
{
// moisture is low (= value above 500):
// -> signal for watering
if (valMoisture > MOISTURE_THRESHOLD)
{
signalForWatering();
gotWater = false;
needsWater = true;
}
// moisture is high (= value is below or even to 500):
// -> plant got some water once
if (valMoisture <= MOISTURE_THRESHOLD && gotWater == false)
{
thanksForWatering();
gotWater = true;
needsWater = false;
}
}
void runWhenOn()
{
checkForPause();
checkForDrinking();
if (needsWater == false) {
showIsAwake();
}
}
void runAlways()
{
checkForWatering();
}
void loop()
{
valStateTrigger = digitalRead(PIN_STATE_TRIGGER);
valMoisture = analogRead(PIN_MOISTURE);
// Serial.print("Moisture: ");
// Serial.print(valMoisture);
// Serial.print(", State: ");
// Serial.println(valStateTrigger);
if (stateMode == false && valStateTrigger == LOW)
{
Serial.println("Set mode to 'on'");
stateMode = true;
delay(DELAY_STATE_CHANGE);
}
else if (stateMode == true && valStateTrigger == LOW)
{
Serial.println("Set mode to 'off'");
stateMode = false;
delay(DELAY_STATE_CHANGE);
}
if (stateMode == true && saidHello == false)
{
saidGoodbye = false;
saidHello = true;
signalHello();
}
if (stateMode == false && saidGoodbye == false)
{
saidHello = false;
saidGoodbye = true;
signalGoodbye();
}
if (stateMode == true)
{
runWhenOn();
}
runAlways();
}