-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoffeePot.ino
70 lines (55 loc) · 1.59 KB
/
CoffeePot.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
#define SENSOR 4
#define INDICATOR 3
#define VALVE 2
#define DEBOUNCE_START_DELAY 2000
#define MIN_FLOW_TIME 2000
#define MAX_FLOW_TIME 20000
void setup() {
pinMode(SENSOR, INPUT);
pinMode(VALVE, OUTPUT);
pinMode(INDICATOR, OUTPUT);
digitalWrite(VALVE, LOW);
digitalWrite(INDICATOR, LOW);
}
void flowOn() {
digitalWrite(VALVE, HIGH);
digitalWrite(INDICATOR, HIGH);
}
void flowOff() {
digitalWrite(VALVE, LOW);
digitalWrite(INDICATOR, LOW);
}
unsigned long water_low_detected = 0;
unsigned long water_flowing = 0;
void loop() {
bool sensor_low = digitalRead(SENSOR);
unsigned long now = millis();
// If water switches to low, record it switched to low.
if (sensor_low && !water_low_detected) {
water_low_detected = now;
}
// If water stays low for longer than DEBOUNCE_START_DELAY, flow water into tank.
if (water_low_detected && !water_flowing && (now > (water_low_detected + DEBOUNCE_START_DELAY))) {
water_flowing = now;
flowOn();
// Minimum activation time to reduce chatter.
delay(MIN_FLOW_TIME);
}
// If the water is not low, but we are on, turn off.
if (!sensor_low && water_low_detected) {
water_low_detected = 0;
water_flowing = 0;
flowOff();
}
// If the water is flowing, see if it's reached the emergency cut off limit.
if (water_flowing && (now > (water_flowing + MAX_FLOW_TIME))) {
flowOff();
// We never exit this state, you have to reset the device.
while (true) {
digitalWrite(INDICATOR, LOW);
delay(500);
digitalWrite(INDICATOR, HIGH);
delay(500);
}
}
}