-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware.ino
140 lines (112 loc) · 2.76 KB
/
firmware.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
/* ------------- START CONFIG ------------- */
constexpr int BUTTON_PIN = 4;
constexpr int LED_PIN = 5;
constexpr int RELAY_PIN = 6;
constexpr int MOIST_PIN = A0;
int moisture;
boolean watering;
int rawMoisture = 0;
int waterTime;
/* ------------- END CONFIG ------------- */
//
//#include "thingProperties.h"
#include <Bounce2.h>
Bounce b;
unsigned long startedWatering;
void setup() {
Serial.begin(9600);
delay(1500);
b.attach(BUTTON_PIN,INPUT_PULLUP);
b.interval(25);
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
// Make sure the pump is not running
// stopWatering();
// Connect to Arduino IoT Cloud
// initProperties();
// ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// setDebugMessageLevel(4);
// ArduinoCloud.printDebugInfo();
// Blink LED to confirm we're up and running
for (int i = 0; i<=4; i++) {
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
void loop() {
// ArduinoCloud.update();
// Read the sensor and convert its value to a percentage
// (0% = dry; 100% = wet)
rawMoisture = analogRead(MOIST_PIN);
moisture = map(rawMoisture, 853, 575, 0, 100);
// Serial.println(moisture);
buttonCheck();
serialCheck();
waterCheck();
}
void serialCheck(){
if (Serial.available() > 0){
int recieved = Serial.read();
if (recieved == 97){
digitalWrite(6, HIGH);
delay(1900);
digitalWrite(6, LOW);
}
// Serial.println(recieved);
}
}
void buttonCheck(){
b.update();
if (b.changed() && b.read() == false) { // button pressed
watering = true;
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
}else{
watering = false;
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
}
void waterCheck() {
// Set the behavior according to the moisture percentage or watering status
if (watering) {
digitalWrite(RELAY_PIN, HIGH);
delay(10000);
} else if (moisture > 40) {
Serial.println("no water needed.");
delay(100);
} else if (moisture > 10) {
Serial.println("Water needed immediately!");
delay(100);
} else {
Serial.println("No water detected!");
delay(100);
}
}
// This function is triggered whenever the server sends a change event,
// which means that someone changed a value remotely and we need to do
// something.
//void onWateringChange() {
// if (watering) {
// startWatering();
// } else {
// stopWatering();
// }
//}
//void startWatering () {
// watering = true;
// startedWatering = millis();
// digitalWrite(RELAY_PIN, HIGH);
//}
//
//void stopWatering () {
// watering = false;
// digitalWrite(RELAY_PIN, LOW);
//}
//
//void onWaterTimeChange() {
// Add your code here to act upon WaterTime change
//}