-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMakerFiller.ino
More file actions
95 lines (85 loc) · 2.09 KB
/
CoffeeMakerFiller.ino
File metadata and controls
95 lines (85 loc) · 2.09 KB
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
#define meter 0
#define button 10
#define valve 6
#define led 9
unsigned long clickCount = 0;
unsigned long debounceDelay = 75;
unsigned long lastDebounceTime = 0;
int lastButtonState = 0;
int buttonState = 1;
unsigned long clickGoal = 3850; //2100clicks/4cups
bool buttonPressed = false;
int machineState = 0; // 0 = IDLE
//watchdog
int watchDogLength = 1000;
unsigned long watchDogTimer = 0;
unsigned long prevClickCount = 0;
void setup() {
//Serial.begin(9600);
pinMode(button,INPUT_PULLUP);
pinMode(meter,INPUT_PULLUP);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
pinMode(valve,OUTPUT);
digitalWrite(valve,LOW);
attachInterrupt(digitalPinToInterrupt(meter), clickDetected, FALLING);
}
void loop() {
int reading = digitalRead(button);
if (reading != lastButtonState){
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState){
buttonState = reading;
if (buttonState == LOW){
buttonPressed = true;
}
}
}
lastButtonState = reading;
switch (machineState){
case 0:
delay(10);
if (buttonPressed){
buttonPressed = false;
machineState = 1;
clickCount = 0;
watchDogTimer = millis();
digitalWrite(valve,HIGH);
digitalWrite(led,HIGH);
}
break;
case 1:
if (buttonPressed){
buttonPressed = false;
machineState = 0;
clickCount = 0;
digitalWrite(valve,LOW);
digitalWrite(led,LOW);
break;
}
else if (clickCount >= clickGoal) {
machineState = 0;
clickCount = 0;
digitalWrite(valve,LOW);
digitalWrite(led,LOW);
break;
}
if (clickCount != prevClickCount){
watchDogTimer = millis();
}
else if (millis() - watchDogTimer > watchDogLength){
//no clicks longer than watchDogLength
machineState = 0;
clickCount = 0;
digitalWrite(valve,LOW);
digitalWrite(led,LOW);
}
prevClickCount = clickCount;
break;
}
}
void clickDetected() {
clickCount++;
}