-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlantGuardian.ino
56 lines (47 loc) · 1.88 KB
/
PlantGuardian.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
/**
* Plant Guardian - Moisture Tracker
* Created by Jonathan Lundström for Conrad.se
* Website: http://jonathanlundstrom.me
* Last edited: 2017-07-04
*/
bool debug = false; // Turn this on to enable debug output in serial console.
int timeout = 1000; // Time to sleep before each serial output is sent.
int plantOneInput = A0; // Change this to represent port that sensor #1 is connected to.
int plantTwoInput = A1; // Change this to represent port that sensor #2 is connected to.
int maximumPlantOne = 880; // Change this number to the maximum value that the sensor gives while in a glass of water (enable debug)
int maximumPlantTwo = 870; // Change this number to the maximum value that the sensor gives while in a glass of water (enable debug)
float moisturePlantOne = 0;
float moisturePlantTwo = 0;
/**
* Setup Arduino application.
*/
void setup() {
Serial.begin(9600);
pinMode(plantOneInput, INPUT);
pinMode(plantTwoInput, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
/**
* Map moisture value against max value and return percentage.
*/
String moistureAsPercentage(float value, int maximum) {
int mappedValue = map(value, 0, maximum, 0, 100);
if (mappedValue > 100) { mappedValue = 100; }
return String(mappedValue);
}
/**
* The almighty loop...
*/
void loop() {
moisturePlantOne = analogRead(plantOneInput);
moisturePlantTwo = analogRead(plantTwoInput);
if (debug) {
Serial.println("Sensor #1: " + String(moisturePlantOne));
Serial.println("Sensor #2: " + String(moisturePlantTwo));
} else {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("{\"sensorOne\":\"" + moistureAsPercentage(moisturePlantOne, maximumPlantOne) + "\", \"sensorTwo\":\"" + moistureAsPercentage(moisturePlantTwo, maximumPlantTwo) + "\"}");
digitalWrite(LED_BUILTIN, LOW);
}
delay(timeout);
}