-
Notifications
You must be signed in to change notification settings - Fork 0
/
EspressoWeight.ino
292 lines (231 loc) · 6.46 KB
/
EspressoWeight.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
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <HX711.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define NAVBAR_X_COORD 57 // X position for navbar locataion
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HX711 scale;
const int LeftButtonPin = 6;
const int CenterButtonPin = 7;
const int RightButtonPin = 5;
int LeftButtonState = 0;
int CenterButtonState = 0;
int RightButtonState = 0;
double currentbrewTime = 0;
double brewTime = 27;
float getpercent;
float barwidth;
float currentGrams;
float targetGrams = 308.0f;
float gramsGetPercent;
float gramsBarWidth;
float readingfromscale;
int currentScreen = 1;
// 1 = Menu 2 = Brew 3 = Time settings 4 = Tare Done 5 = End
int menuItem = 1;
int countdown = 1;
int tempcountdown;
void setup()
{
//initiate buttons
pinMode(LeftButtonPin, INPUT);
pinMode(CenterButtonPin, INPUT);
pinMode(RightButtonPin, INPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for OLED
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
drawScreens();
display.display();
// listen on serial for scale HX711
Serial.begin(57600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
// Calibrate the scale
scale.set_scale(1860);
scale.tare();
}
void loop()
{
//read button states
LeftButtonState = digitalRead(LeftButtonPin);
CenterButtonState = digitalRead(CenterButtonPin);
RightButtonState = digitalRead(RightButtonPin);
buttonLogic();
if (currentScreen == 2) {
display.clearDisplay();
drawScreens();
display.display();
}
if (scale.wait_ready_timeout(1000)) {
countdown = countdown-1;
if (countdown == 0) {
if (currentScreen == 1){
countdown = 60;
}
if (currentScreen == 2){
countdown = 10;
}
readingfromscale = scale.get_units(10);
display.clearDisplay();
drawScreens();
display.display();
}
}
delay(100);
}
void buttonLogic(){
//if on menu and left is pressed
if (LeftButtonState == HIGH && currentScreen == 1) {
if (menuItem == 1){
menuItem = 3;
} else {
menuItem = menuItem-1;
}
display.clearDisplay();
drawScreens();
display.display();
}
//if on menu and center is pressed
if (CenterButtonState == HIGH && currentScreen == 1 && currentScreen == 1) {
if (menuItem == 3){
menuItem = 1;
} else {
menuItem = menuItem+1;
}
display.clearDisplay();
drawScreens();
display.display();
}
// if on brew menu and pressed
if (RightButtonState == HIGH && currentScreen == 1 && menuItem == 1) {
currentScreen = 2;
display.clearDisplay();
drawScreens();
display.display();
}
// if on tare menu and pressed
if (RightButtonState == HIGH && currentScreen == 1 && menuItem == 2) {
scale.tare();
readingfromscale = scale.get_units(10);
display.clearDisplay();
drawScreens();
display.display();
}
}
void drawScreens(){
// SCREEN MENU
if (currentScreen == 1){
if (menuItem == 1){
drawMainMenu(1);
drawNavMenu();
}
if (menuItem == 2){
drawMainMenu(2);
drawNavMenu();
}
if (menuItem == 3){
drawMainMenu(3);
drawNavMenu();
}
display.setTextSize(1);
if (menuItem == 2){ display.setTextColor(BLACK);} else {display.setTextColor(WHITE);}
display.setCursor(90,24);
display.print(readingfromscale);
display.display();
}
// SCREEN BREWING
if (currentScreen == 2){
display.clearDisplay();
display.setCursor(1,1);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("Brewing");
display.setTextSize(1);
display.setCursor(105,6);
display.print(targetGrams);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(45,NAVBAR_X_COORD);
display.print("STOP");
//draw bar
currentGrams = readingfromscale;
getpercent = currentGrams / targetGrams * 100;
barwidth = 128.0f / 100.0f * getpercent;
currentbrewTime = currentbrewTime +1;
display.drawRect(0,19, SCREEN_WIDTH, 16, WHITE);
display.fillRect(0,19, barwidth, 16, WHITE);
display.setCursor(20,40);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print(currentbrewTime);
display.display();
//if grams is the same as targetgrams
if (currentGrams >= targetGrams) {
currentScreen = 5;
display.clearDisplay();
drawScreens();
display.display();
}
}
// SCREED DONE
if (currentScreen == 5){
display.setCursor(1,1);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("Complete");
display.setCursor(1,20);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print(currentbrewTime);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(45,NAVBAR_X_COORD);
display.print("MENU");
}
}
void drawMainMenu(int no){
if (no == 1){
display.fillRect(0,0, SCREEN_WIDTH, 16, WHITE);
}
if (no == 2){
display.fillRect(0,19, SCREEN_WIDTH, 16, WHITE);
}
if (no == 3){
display.fillRect(0,38, SCREEN_WIDTH, 16, WHITE);
}
display.setTextSize(2);
if (no == 1){ display.setTextColor(BLACK);} else {display.setTextColor(WHITE);}
display.setCursor(1,1);
display.print("Brew");
display.setTextSize(2);
if (no == 2){ display.setTextColor(BLACK);} else {display.setTextColor(WHITE);}
display.setCursor(1,20);
display.print("Tare");
display.setTextSize(2);
if (no == 3){ display.setTextColor(BLACK);} else {display.setTextColor(WHITE);}
display.setCursor(1,39);
display.print("Time");
}
void drawNavMenu(){
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,NAVBAR_X_COORD);
display.print("UP");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(45,NAVBAR_X_COORD);
display.print("DOWN");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(90,NAVBAR_X_COORD);
display.print("SELECT");
}