-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeGrinder.ino
323 lines (264 loc) · 7.97 KB
/
CoffeeGrinder.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Coffee Grinder timer
- Rotary encoder rotation adjusts time duration.
- Pushing encoder allows rotary encoder to adjust the calibration between dose weight and the given duration, changing the time/weight ratio
- Pressing the Grind button powers the relay for set duration, and stores changed duration to EEPROM
- Releasing the Calibration button stores dose weight to EEPROM
*/
//load libraries
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
//Define variables
//LCD I2C
#define I2C_ADDR 0x27 //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
//RELAY
#define RELAYSIGNAL 6
#define LEDPIN 7
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define CAL 4
//CALIBRATION BUTTON
#define SW 5
//Initialise the LCD
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//EEPROM
int timeAddr = 0;
int timeEERead = 0;
int weightAddr = 1;
int weightEERead = 0;
int byteMin = 0;
int byteMax = 255;
//Dose variables
unsigned int grindTimeINT = EEPROM.read(timeAddr);
unsigned int grindWeightINT = EEPROM.read(weightAddr);
float grindTime = float(grindTimeINT)/10; //1 byte max allows max 25.5 second grind time
float grindTimeRemaining = grindTime;
float grindWeight = float(grindWeightINT)*0.2; //1 byte max allows max 51 gram dose
float grindWeightRemaining = grindWeight; //1 byte max allows max 51 gram dose
float grindCoeff = float(grindWeightINT)/float(grindTimeINT);
//Rotary Encoder variables
int counter = 0;
int currentStateCLK;
int lastStateCLK;
unsigned long lastButtonPress = 0;
//Calibration Variables
bool calMode = 0;
unsigned long lastCalButtonPress = 0;
int lastCalBtnState = HIGH;
//Screen Update
long previousMillis = 0;
unsigned int interval = 500;
void setup()
{
//Define the LCD as 16 column by 2 rows
Serial.begin(9600);
lcd.begin (16,2);
Serial.print("Grind Time INT: ");
Serial.println(grindTimeINT);
Serial.print("Grind Weight INT: ");
Serial.println(grindWeightINT);
Serial.print("Grind Coeff: ");
Serial.println(grindCoeff);
//Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
//Configure relay pin, and set to LOW
pinMode(RELAYSIGNAL, OUTPUT);
digitalWrite(RELAYSIGNAL,LOW);
//Configure LED pin, and set to LOW
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN,LOW);
lcd.setCursor(3, 0);
lcd.print("GRINDY BOI");
screenUpdate();
// Set encoder & pushbutton pins as inputs with internal pull-up
pinMode(CLK,INPUT_PULLUP);
pinMode(DT,INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
pinMode(CAL, INPUT_PULLUP);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop()
{
unsigned long currentMillis = millis();
//Checks if Calibration Mode is active
calCheck();
//Checks Encoder
encoderCheck();
//Periodic screen refresh, at defined interval
if (currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
}
}
void grindRun()
{
//Compare EEPROM value to variable, and write new value to EEPROM if changed
timeEERead = EEPROM.read(timeAddr);
if (timeEERead != grindTimeINT)
{
EEPROM.write(timeAddr, grindTimeINT);
Serial.print("Grind Time changed. New time stored to EEPROM: ");
Serial.println(grindTimeINT);
}
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("GRINDING");
previousMillis = millis();
unsigned long startMillis = millis();
//Turn on Relay
digitalWrite(RELAYSIGNAL,HIGH);
digitalWrite(LEDPIN,HIGH);
//Relay powered for defined duration
while (millis() - startMillis < (grindTimeINT*100)) {
// Loop til grind time passed.
grindTimeRemaining = grindTime - float(millis()- startMillis)/1000;
grindWeightRemaining = grindWeight * (float(millis()- startMillis)/1000)/grindTime;
lcd.setCursor(0, 1);
lcd.print(grindTimeRemaining,1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print("sec");
lcd.setCursor(10, 1);
lcd.print(grindWeightRemaining,1);
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.print("g");
Serial.println(grindTimeRemaining);
delay(100);
}
//Turn off Relay
digitalWrite(RELAYSIGNAL,LOW);
digitalWrite(LEDPIN,LOW);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("GRIND COMPLETE");
delay(1000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("GRINDY BOI");
screenUpdate();
}
void encoderCheck()
{
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK ){
//if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (calMode==0){
if (digitalRead(DT) != currentStateCLK) {
if (grindTimeINT != byteMin)
{
grindTimeINT--;
}
}
else {
if (grindTimeINT != byteMax)
{
grindTimeINT++;
}
}
grindTime=float(grindTimeINT)/10;
grindWeightINT = grindTimeINT * grindCoeff;
grindWeight = float(grindWeightINT)*0.2;
screenUpdate();
}
else if (calMode == 1){
if (digitalRead(DT) != currentStateCLK) {
if (grindWeightINT != byteMin)
{
grindWeightINT--;
}
}
else {
if (grindWeightINT != byteMax)
{
grindWeightINT++;
}
}
grindWeight = float(grindWeightINT)*0.2;
grindCoeff = float(grindWeightINT)/float(grindTimeINT);
screenUpdate();
}
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//debounce
if (millis() - lastButtonPress > 50) {
Serial.println("Grinding");
grindRun();
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}
void calCheck()
{
// Read the button state
int calBtnState = digitalRead(CAL);
//Save grind weight if calibration button has been released
if (calBtnState == HIGH && lastCalBtnState == LOW) {
Serial.println("Saving Calibration");
EEPROM.write(weightAddr, grindWeightINT);
Serial.println("Grind Weight changed. New weight stored to EEPROM");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("GRINDY BOI");
screenUpdate();
}
//Calibration mode when button pressed
if (calBtnState == LOW) {
//debounce
if (millis() - lastCalButtonPress > 50) {
Serial.println("Calibration Mode");
calMode = 1;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("CALIBRATE");
screenUpdate();
}
// Remember last button press event
lastCalButtonPress = millis();
}
else {
calMode = 0;
}
// Put in a slight delay to help debounce the reading
delay(1);
lastCalBtnState = calBtnState;
}
void screenUpdate(){
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(grindTime,1);
lcd.setCursor(4, 1);
lcd.print("sec");
lcd.setCursor(11, 1);
lcd.setCursor(10, 1);
lcd.print(grindWeight,1);
lcd.setCursor(14, 1);
lcd.print("g");
}