-
Notifications
You must be signed in to change notification settings - Fork 0
/
Godrej Cooking Aid Probe.ino
436 lines (385 loc) · 14.7 KB
/
Godrej Cooking Aid Probe.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Author: Indranil Chandra and Vaibhav Yengul
// Linkit One code for Godrej Cooking Aid Probe
// The code has been released under MIT License
// Download the latest version of Arduino IDE from:
// https://www.arduino.cc/en/Main/Software
// Configure Arduino IDE to program Linkit One Development Board from MediaTek Labs, use below link to get started:
// http://labs.mediatek.com/site/global/developer_tools/mediatek_linkit/get-started/index.gsp
// Add all the below libraries and its dependancies to compile and burn the code on MediaTek Linkit One Development board:
// https://github.com/leouzz/Linkit-One-OneWire
// https://github.com/Seeed-Studio/Grove_Starter_Kit_For_LinkIt/tree/master/libraries/Suli_LinkIt_One
// https://github.com/Seeed-Studio/Grove_LED_Bar
///////////////////Libraries//////////////////////////////////////////////////////////////////////////////////////////
#include <OneWire.h>
#include <Wire.h>
#include <LAudio.h>
#include <Suli.h>
#include <Seeed_LED_Bar_Arduino.h>
#include <String>
/////////////////Global Vairables/////////////////////////////////////////////////////////////////////////////////////
int TouchButton = 3; //TouchButton on Pin 3
int DS18S20_Pin = 7; //DS18S20 Signal on Pin 7
SeeedLedBar bar(5, 4); // CLK, DTA -> Connect the Vcc pin of LED bar on 3.3V
OneWire ds(DS18S20_Pin);
float temperature;
float prev_temp = 27.0;
float HighTemp = 40;
float LowTemp = 30;
volatile int buttonPress = 1;
//////////////////////////////////Struct recipeData//////////////////////////////////////////////////////////////
struct recipeData {
int nSteps;
int rTime[100];
float rTemp[100];
};
///////////////////ISR///////////////////////////////////////////////////////////////////////////////////////////////
void buttonPressISR() {
buttonPress = 0;
}
/////////////////PLAY_Audio//////////////////////////////////////////////////////////////////////////////////////////
void PLAY_Audio(char* song,int vol) {
LAudio.playFile(storageFlash,song);
LAudio.setVolume(vol);
delay(2500);
}
////////////////Retrieve Temperature Function////////////////////////////////////////////////////////////////////////
float getTemp(float prevTemp){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
Serial.println("Device Address could not be found in search");
return prevTemp;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return prevTemp;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.println("Device is not recognized");
return prevTemp;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
if(TemperatureSum >= -25 && TemperatureSum <= 250){
return TemperatureSum;
}
else{
return prevTemp;
}
}
///////////////////Parse Data for Play Mode/////////////////////////////////////////////////////////////////////////
struct recipeData parseDataPlay(String receivedData){
struct recipeData rData;
int semicolonindex1,semicolonindex2,semicolonindex3,semicolonindex4;
semicolonindex1 = receivedData.indexOf(";");
semicolonindex2 = receivedData.indexOf(";",semicolonindex1+1);
semicolonindex3 = receivedData.indexOf(";",semicolonindex2+1);
semicolonindex4 = receivedData.indexOf(";",semicolonindex3+1);
String mode = receivedData.substring(0, semicolonindex1);
String stepCount = receivedData.substring(semicolonindex1+1, semicolonindex2);
String stepsList = receivedData.substring(semicolonindex2+1, semicolonindex3);
String temperatureList = receivedData.substring(semicolonindex3+1, semicolonindex4);
String timeList = receivedData.substring(semicolonindex4+1);
int count = stepCount.toInt();
Serial.println(" ");
Serial.print("No. of steps: ");
Serial.println(count);
Serial.println(" ");
rData.nSteps = count;
String stepsArray[count];
stepsList.replace("["," ") ;
stepsList.replace("]",",") ;
Serial.println("Recipee Steps are: ");
for(int i=0; i < count; i++){
int commaIndex = stepsList.indexOf(",");
stepsArray[i] = stepsList.substring(1, commaIndex);
Serial.print("Step");
Serial.print(i+1);
Serial.print(": ");
Serial.println(stepsArray[i]);
if(commaIndex+1 != stepsList.length())
stepsList = stepsList.substring(commaIndex+1, stepsList.length());
}
Serial.println(" ");
float tempArray[count];
temperatureList.replace("["," ") ;
temperatureList.replace("]",",") ;
Serial.println("Temperature Array: ");
for(int i=0; i < count; i++){
int commaIndex = temperatureList.indexOf(",");
String x = temperatureList.substring(1, commaIndex);
tempArray[i] = x.toFloat();
rData.rTemp[i] = tempArray[i];
Serial.print(tempArray[i]);
Serial.print(" ");
if(commaIndex+1 != temperatureList.length())
temperatureList = temperatureList.substring(commaIndex+1, temperatureList.length());
}
Serial.println(" ");
float timeArray[count];
timeList.replace("["," ") ;
timeList.replace("]",",") ;
Serial.println("Time Array: ");
for(int i=0; i < count; i++){
int commaIndex = timeList.indexOf(",");
String x = timeList.substring(1, commaIndex);
timeArray[i] = x.toFloat();
rData.rTime[i] = timeArray[i];
Serial.print(timeArray[i]);
Serial.print(" ");
if(commaIndex+1 != timeList.length())
timeList = timeList.substring(commaIndex+1, timeList.length());
}
Serial.println(" ");
return rData;
}
/////////////////////////Parse Data for Record Mode////////////////////////////////////////////////////////////////
int parseDataRecord(String receivedData){
int semicolonindex1,semicolonindex2,semicolonindex3,semicolonindex4;
semicolonindex1 = receivedData.indexOf(";");
semicolonindex2 = receivedData.indexOf(";",semicolonindex1+1);
String mode = receivedData.substring(0, semicolonindex1);
String stepCount = receivedData.substring(semicolonindex1 + 1, semicolonindex2);
String stepsList = receivedData.substring(semicolonindex2 + 1) ;
int count = stepCount.toInt();
Serial.println(" ");
Serial.print("No. of steps: ");
Serial.println(count);
Serial.println(" ");
String stepsArray[count];
stepsList.replace("["," ") ;
stepsList.replace("]",",") ;
Serial.println("Recipee Steps are: ");
for(int i=0; i < count; i++){
int commaIndex = stepsList.indexOf(",");
stepsArray[i] = stepsList.substring(1, commaIndex);
Serial.print("Step");
Serial.print(i);
Serial.print(": ");
Serial.println(stepsArray[i]);
if(commaIndex+1 != stepsList.length())
stepsList = stepsList.substring(commaIndex+1, stepsList.length());
}
Serial.println(" ");
return count;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////recordMode()////////////////////////////////////////////////////////////////
String recordMode(int numberOfSteps){
int recipeFinishFlag = 0;
int stepCounter = 1;
int timeCounter = 0;
while(recipeFinishFlag == 0){
temperature = getTemp(prev_temp);
String senddata = "#" + String(temperature) + "," + String(timeCounter) + "\n";
char *s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
prev_temp = temperature;
if(buttonPress == 0 && stepCounter < numberOfSteps){
Serial.println("Touch Button Pressed");
senddata = " Step " + String(stepCounter+1) + " started" + "\n";
s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
stepCounter++;
buttonPress = 1;
}
else if((buttonPress == 0) && (stepCounter == numberOfSteps)){
Serial.println("Touch Button Pressed");
senddata = " Recipe Finished\n";
s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
recipeFinishFlag = 1;
Serial.println("Recipe Finished");
buttonPress = 1;
}
delay(1000);
timeCounter += 1;
} ///////////////////////////////////////while step finish loop ends here
String recordModeACK = "Recording of the Recipee was finished succesfully!";;
PLAY_Audio("recordModeACK.mp3",6);
return recordModeACK;
}
////////////////////////playMode//////////////////////////////////////////////////////////////////////////
String playMode(struct recipeData values){
int numberOfSteps = values.nSteps;
int timeCounter = 0;
int led_limit = 7;
int currentTimeCount = 0;
for(int currentStepNo=0; currentStepNo < numberOfSteps; currentStepNo++){
String senddata = " Step " + String(currentStepNo+1) + " started" + "\n";
char *s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
int tempSetFlag = 0;
int perfectTempFlag = 0;
int randFlag = 0;
while((tempSetFlag == 0) && (perfectTempFlag == 0)){
//Read Temperature
temperature = getTemp(prev_temp);
String senddata = "#" + String(temperature) + "," + String(timeCounter) + "\n";
char *s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
prev_temp = temperature;
//Temperature Reading Finished
//Compare with Ideal Temperature and give audio-visual output
if((temperature >= (values.rTemp[currentStepNo] - 5)) && (temperature <= (values.rTemp[currentStepNo] + 5)) ){
bar.setLevel(0);
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
bar.singleLed(1,1);
PLAY_Audio("perfect.mp3",6);
perfectTempFlag = 1;
}
else if(temperature < values.rTemp[currentStepNo]){
bar.setLevel(0);
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
bar.singleLed(0,1);
PLAY_Audio("less.mp3",6);
}
else if(temperature > values.rTemp[currentStepNo]){
bar.setLevel(0);
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
for(int i=2;i<=led_limit;i++){
bar.singleLed(i,1);
}
PLAY_Audio("more.mp3",6);
}
timeCounter += 2;
////////////////////////
randFlag++;
if(randFlag == 15){
tempSetFlag = 1;
}
////////////////////////
}// Perfect Temeprature for the Step has now been set
if(tempSetFlag == 1){
bar.setLevel(0);
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
bar.singleLed(1,1);
PLAY_Audio("imperfect.mp3",6);
}
//then do wait for the counter to finish
while(currentTimeCount != values.rTime[currentStepNo]){
//Read Temperature
temperature = getTemp(prev_temp);
String senddata = "#" + String(temperature) + "," + String(timeCounter) + "\n";
char *s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
prev_temp = temperature;
//Temperature Reading Finished
delay(1000);
currentTimeCount += 1;
timeCounter += 1;
}
}
String senddata = " Recipe was cooked succesfully!\n";
char *s = &senddata[0] ;
Serial.print("Sending Data: ");
Serial.println(senddata);
for(int i=0 ; i<senddata.length() ; i++){
s = &senddata[i] ;
Serial1.write(*s);
}
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
String playModeACK = "Recipe was cooked succesfully!";
PLAY_Audio("playModeACK.mp3",6);
return playModeACK;
}
///////////////////////////////////void setup()////////////////////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
pinMode(TouchButton, INPUT_PULLUP);
attachInterrupt(1, buttonPressISR, FALLING);
LAudio.begin();
bar.begin(5, 4);
bar.setLevel(0);
for(int i=0;i<10;i++){
bar.singleLed(i,0);
}
while(!Serial);
Serial.println("Welcome to Godrej Cookind Aid Application!");
Serial.println(" ");
}
/////////////////////////////////void loop()//////////////////////////////////////////////////////////////
void loop() {
String Data = "";
while (Serial1.available()){
char character = Serial1.read(); // Receive a single character from the software serial port
Data.concat(character); // Add the received character to the receive buffer
if (character == '*'){
Serial.println(Data);
int colonMode = Data.indexOf(';');
String Mode = Data.substring(0, colonMode);
if (Mode == "r"){
int numberOfSteps = parseDataRecord(Data);
String returnedString = recordMode(numberOfSteps);
Serial.println(" ");
Serial.println(returnedString);
Serial.println(" ");
}
else if(Mode == "p"){
struct recipeData values = parseDataPlay(Data);
String returnedString = playMode(values);
Serial.println(" ");
Serial.println(returnedString);
Serial.println(" ");
}
}///////////////////if char == * loop ends here
}///////////////////////////////////while Serial1.available loop ends here
Data = "";
}//void loop ends here