-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffee.ino
116 lines (95 loc) · 2.5 KB
/
coffee.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
// coffee o clock
// this coffee grinder grinds your coffee super slowly and silently over 24 hours
// limiting you to one cup a day
// SET YOUR OWN TIME ZONE-- the number is the UTC offset
// (Example: Eastern time is UTC-5, so the number is -5)
int timezone=-8;
// SET THE HOUR YOU WANT THE GRINDER TO GO OFF using 24 hr format
// This is a weekly calendar-- shows the hour scheduled for M,T,W,Th,F,S,S
int coffeeHour[7]={7,7,7,7,7,9,9}; // in 24 hour format
// SET HOW LONG YOU WANT THE GRINDER TO GRIND EACH DAY
// note: it's wayyyyy slower than a regular grinder.
// This is a weekly calendar-- shows the minutes of grinding scheduled for M,T,W,Th,F,S,S
// If you are using google calendar instead, you can set every number list to 0
int grindTimeList[7]={5,5,5,4,4,4,3}; // in minutes
// No need to change these:
int mosfet=D6;
int hour;
int grindState=-1;
//0: don't grind
//1: get ready to grind
//2: grinding
//3: finished grinding, returning to regular state
//-1: boot state
int timeGrindOn;
int timeAtGrindStart;
int timeBetweenGrinds;
int timeAtGrindOff;
void setup() {
pinMode(mosfet,OUTPUT);
Particle.function("grind", grindCoffee);
Particle.function("toggle", coffeeToggle);
// set time zone
Time.zone(timezone);
// get current hour
hour=Time.hour();
Serial.print(hour);
setGrindOff();
}
void loop(){
if (grindState==0) {
noGrind();
}
else if (grindState==2) {
grinding();
}
}
int getGrindTime() {
// get day of week
int g=grindTimeList[Time.weekday()-1];
g=g*1000*60;
return g;
}
int grindCoffee(String command) {
char inputStr[64];
command.toCharArray(inputStr,64);
int grindTime=atoi(inputStr);
coffeeToggle("");
delay(grindTime);
coffeeToggle("");
return 0;
}
int coffeeToggle(String command) {
digitalWrite(mosfet,LOW);
delay(500);
digitalWrite(mosfet,HIGH);
delay(500);
digitalWrite(mosfet,LOW);
return 0;
}
void setGrindOn() {
timeGrindOn=getGrindTime();
timeAtGrindStart=millis();
coffeeToggle("");
grindState=2;
}
void grinding() {
if (millis()>timeAtGrindStart+timeGrindOn) {
setGrindOff();
grindState=3;
}
}
void setGrindOff() {
int m=Time.minute();
timeBetweenGrinds=(60-m)*60*1000;
timeAtGrindOff=millis();
if (grindState==3) {coffeeToggle("");}
grindState=0;
}
void noGrind() {
if (millis()>timeAtGrindOff+timeBetweenGrinds) {
setGrindOn();
Particle.publish("coffee-on", NULL, 60, PRIVATE);
grindState=1;
}
}