-
Notifications
You must be signed in to change notification settings - Fork 1
/
espresstouch-ae.ino
326 lines (254 loc) · 8.75 KB
/
espresstouch-ae.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
324
325
326
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_STMPE610_RK.h>
// This #include statement was automatically added by the Particle IDE.
#if defined(PARTICLE)
#include <Adafruit_mfGFX.h>
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC D5
#define TFT_CS D4
#else
#include "SPI.h"
#include <Adafruit_mfGFX.h>
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
#endif
#define TS_MINX 3800
#define TS_MAXX 100
#define TS_MINY 100
#define TS_MAXY 3750
const int STMPE_CS = D3;
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, 0);
Adafruit_STMPE610 touch = Adafruit_STMPE610(STMPE_CS);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
const uint32_t baud = 19200;
int makeCoffee(String command);
int len;
char szTX[64];
char szRX[64];
String inData;
char space = 32;
String weight;
int brewWeight;
int priorTime;
boolean makingCoffee = false;
//adam variables
int ms = 0;
int s = 0;
int coffee = 36;
int brewtime = 25;
int oldy,oldx = 0;
long brewTimeStarted=0;
int weightDisplay;
void setup() {
Serial1.begin(baud); // baud rates below 1200 can't be produced by USART
Serial.begin(9600);
strcpy(szTX, "TARE$0d$0a"); // add some exotic chars too
len = strlen(szTX) + 1;
Serial1.write((uint8_t*)szTX, len);
//Serial1.write((uint8_t*)szTX, len);
// TOUCHSCREEN SETUP
tft.begin();
touch.begin();
setupSettingsScreen();
// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
#define BACKCOLOR 0x0000
#define PRINTCOL 0xFFFF
#define CHARBACK 0x001F
//COFFEE RELAY
pinMode(A0, OUTPUT); // set pin as an output
digitalWrite(A0,LOW); // Turns OFF Relays 1
Particle.function("makeCoffee", makeCoffee);
Particle.publish("signal", "start");
randomSeed(analogRead(A0) + analogRead(A1));
}
void loop() {
// WAIT FOR SOMEONE TO CLICK "BREW"
if (!makingCoffee){
TS_Point p = touch.getPoint();
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
if (touch.touched() ) {
TS_Point p = touch.getPoint();
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
while ( !touch.bufferEmpty() || touch.touched())
{
p = touch.getPoint();
}
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
if (oldx == p.x || oldy == p.y) ;
else
{
pointTouchedNotBrewing(p.x,p.y);
}
oldx = p.x;
oldy = p.y;
}
}
if(Serial1.available())
{
String scaleInputLine = Serial1.readStringUntil('\r\n');
char inputStr[64];
scaleInputLine.toCharArray(inputStr,64);
char *op = strtok(inputStr," ");
String first = String(op);
op = strtok(NULL," ");
String priorWeight = weight;
weight = String(op);
op = strtok(NULL," ");
String third = String(op);
weight.remove(0,3); //remove first 4 zeros
weight.remove(7,4); // last 2 post decimal point
String theTime;
if (makingCoffee){
if(weight.toInt() != weightDisplay) // if the weight has changed by a full gram
{
tft.setCursor(140,100);
tft.setCursor(95, 50);
tft.fillRect(95,50, 70, 50, ILI9341_BLACK);
weightDisplay = weight.toInt();
String weightDisplayer = String(weightDisplay);
weightDisplayer.remove(3,5);
tft.print(weightDisplayer);
}
theTime = String((millis() /1000) - brewTimeStarted).remove(4,10); //get the time run in seconds
if(priorTime != theTime.toInt()) //if the time has increased by a full digit
{
tft.setCursor(90, 170);
tft.fillRect(90, 170, 80, 50, ILI9341_BLACK);
priorTime = theTime.toInt(); // set time to check for updates
tft.print(theTime);
}
}
//Particle.publish("weight", weight);
int delta = priorWeight.toInt() - weight.toInt();
//Particle.publish("delta", String(delta));
if (delta>5) { Particle.publish("ERROR IN FILTER (OVER 5 gram jump)", String(delta)); }
if((weight.toInt() > (brewWeight - 2)) && makingCoffee && delta < 5 && weight.toInt() < 50) // stop making coffee if within 2 of target and filter out noise of grams jumping over 5 in a second OR is over 50
{
Particle.publish("Stopped here weight reading", weight);
Particle.publish("Stopped here scale reading", scaleInputLine);
toggleRelay();
Particle.publish("time", theTime);
makingCoffee = false;
}
}
}
//Buttons logic from settings page
void pointTouchedNotBrewing(int x, int y){
if (x > (5) && x < (75) && y < 80 && y >10) {
coffee = coffee-1;
tft.setCursor(95, 50);
tft.fillRect(95,50, 50, 50, ILI9341_BLACK);
tft.print(String(coffee));
}
else if (x > (165) && x < (235) && y < 80 && y >10) {
coffee = coffee+1;
tft.setCursor(95, 50);
tft.fillRect(95,50, 50, 50, ILI9341_BLACK);
tft.print(String(coffee));
}
else if (x > (5) && x < (75) && y < 200 && y >130) {
brewtime = brewtime-1;
tft.setCursor(95, 170);
tft.fillRect(95, 170, 50, 50, ILI9341_BLACK);
tft.print(String(brewtime));
}
else if (x > (165) && x < (235) && y < 200 && y >130) {
brewtime = brewtime+1;
tft.setCursor(95, 170);
tft.fillRect(95, 170, 50, 50, ILI9341_BLACK);
tft.print(String(brewtime));
}
else if (x > 5 && x<235 && y > 230 && y <310){ brew(); }
}
//called when the brew button is press
void brew(){
makeCoffee(String(coffee));
setupBrewingScreen();
tft.setCursor(70,250);
while (makingCoffee){
loop();
}
delay(10000);
setupReadyScreen();
}
void setupBrewingScreen(){
tft.fillRect(5, 230, 230, 80, ILI9341_BLUE);
tft.setTextSize(4);
tft.setCursor(30,250);
tft.print("BREWING");
tft.drawRect(5, 230, 230, 80, ILI9341_WHITE);
}
void setupReadyScreen(){
tft.fillRect(5, 230, 230, 80, ILI9341_CYAN);
tft.setTextSize(4);
tft.setCursor(75,250);
tft.print("BREW");
tft.drawRect(5, 230, 230, 80, ILI9341_WHITE);
tft.fillRect(85, 170, 80, 50, ILI9341_BLACK);
tft.fillRect(95,50, 70, 50, ILI9341_BLACK);
tft.setCursor(95, 50);
tft.print(String(coffee));
tft.setCursor(95, 170);
tft.print(String(brewtime));
}
//makes the coffee
int makeCoffee(String command)
{
makingCoffee = true;
brewWeight = command.toInt();
brewWeight = brewWeight - 3; //adjust for "last second espresso dripping"
ms = 0; // reset timers
s = 0; // reset timers
//tare the scale
strcpy(szTX, "TARE$0d$0a"); // add some exotic chars too
len = strlen(szTX) + 1;
Serial1.write((uint8_t*)szTX, len);
toggleRelay(); // start making espresso
return 1;
}
void toggleRelay()
{
digitalWrite(A0,HIGH); // Turns ON Relays 1
delay(500); // Wait 2 seconds
digitalWrite(A0,LOW); // Turns Relay Off
brewTimeStarted = millis() / 1000.0;
}
void setupSettingsScreen(){
tft.fillScreen(ILI9341_BLACK);
tft.fillCircle(40, 45, 35, ILI9341_RED);
tft.fillCircle(200, 45, 35, ILI9341_GREEN);
tft.fillCircle(40, 165, 35, ILI9341_RED);
tft.fillCircle(200, 165, 35, ILI9341_GREEN);
tft.fillRect(5, 230, 230, 80, ILI9341_CYAN);
tft.drawRect(5, 230, 230, 80, ILI9341_WHITE);
tft.fillRect(15, 40, 50, 10, ILI9341_WHITE);
tft.fillRect(195, 20, 10, 50, ILI9341_WHITE);
tft.fillRect(175, 40, 50, 10, ILI9341_WHITE);
tft.fillRect(15, 160, 50, 10, ILI9341_WHITE);
tft.fillRect(195, 140, 10, 50, ILI9341_WHITE);
tft.fillRect(175, 160, 50, 10, ILI9341_WHITE);
tft.setCursor(85, 20);
tft.setTextSize(2);
tft.println("WEIGHT");
tft.setCursor(95, 140);
tft.println("TIME");
tft.setTextSize(4);
tft.setCursor(75,250);
tft.print("BREW");
tft.setCursor(95, 50);
tft.fillRect(95,50, 50, 50, ILI9341_BLACK);
tft.print(String(coffee));
tft.setCursor(95, 170);
tft.fillRect(95, 170, 50, 50, ILI9341_BLACK);
tft.print(String(brewtime));
loop();
}