-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp.ino
311 lines (280 loc) · 5.97 KB
/
fp.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
/*
initialState: https://groker.init.st/api/events?accessKey=ist_3Y4exZqlwezqaeRaBAmeBbQkyDrKTvpS&bucketKey=LSLT9HDY8RLG
*/
//Link to bucket: https://go.init.st/2vdubnt
#include "pitches.h"
#include "SparkFunMicroOLED.h" // Include MicroOLED library
#include "math.h"
#include "bitmaps.h"
#include <blynk.h>
#include "SparkFunMMA8452Q.h"
//button
bool keepGoing = true;
int lastButtonState = LOW;
//piezzo buzzer
int melody[] = {NOTE_G5, NOTE_G5, NOTE_G5, NOTE_E5, NOTE_G5, NOTE_C6};
int duration[] = {2, 4, 4, 4, 4, 4};
const int PIN_SERVO = D4;
const int PIN_BUZZER = D2;
const int PIN_TRIGGER = D8;
const int PIN_ECHO = D3;
const int PIN_BUTTON = A5;
//for blynk
int menuItem;
int buttonPushed;
//for distance sensor
double SPEED = 0.03444;
int MAX_RANGE = 78;
int MIN_RANGE = 0;
double distance;
const double distanceToGND = 17;
//initial state
unsigned long prevStudyTime = 0;
int data = 0;
bool restart = false;
//accelerometer
int taps = 0;
//OLED bitmaps
enum Pics
{
GUMBALL,
STUDY,
BREAK,
MUSIC
};
//objects
Servo servo;
MicroOLED oled;
MMA8452Q accel;
//Blynk app
unsigned long prevMillis = 0;
unsigned long blynkDelay = 1000;
unsigned long timerLength = 0;
void setup()
{
//for accelerometer
accel.begin(SCALE_2G, ODR_1);
byte threshold = 1;
//time elapsed between pulses (in increments of 0.0625 ms)
byte pulseTimeLimit = 200; //255 * 0.0625
//after a pulse is detected, how long does it wait before another pulse is detected (like a delay)
byte pulseLatency = 20;
accel.setupTap(threshold, threshold, threshold, pulseTimeLimit, pulseLatency);
//for button
pinMode(PIN_BUTTON, INPUT);
//for distance sensor
pinMode(PIN_TRIGGER, OUTPUT);
pinMode(PIN_ECHO, INPUT);
//for piezzo buzzer
pinMode(PIN_BUZZER, OUTPUT);
//for servo
pinMode(PIN_SERVO, OUTPUT);
Serial.begin(9600);
servo.attach(PIN_SERVO);
//for OLED
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
delay(1000); // Delay 1000 ms
//for Blynk
Serial.begin(9600);
delay(5000); // Allow board to settle
Blynk.begin("qnh7VDp-oZr-rsl2GK9aA4KySEWSqYym");
Blynk.syncAll(); //optional if you want the argon to be sent all app values when argon resets
}
//OLED display action based on state
//switch LED based on state
void action()
{
//if blynk button is pushed
if (buttonPushed == 1)
{
//serve gumball
playMusic();
serveGum();
}
//otherwise, use distance sensor to trigger gumball
else
{
motionSensor();
}
}
//display welcome image before using machine
void displayWelcome()
{
oled.clear(PAGE);
switch (menuItem)
{
case 1:
oled.drawBitmap(gumMachine);
break;
case 2:
oled.drawBitmap(book);
break;
case 3:
oled.drawBitmap(music);
break;
}
oled.display();
}
//activates servo to dispense gum
void serveGum()
{
for (int i = 108; i >= 13; i -= 2)
{
servo.write(i);
delay(10);
}
delay(1000);
for (int i = 13; i <= 110; i += 2)
{
servo.write(i);
delay(10);
}
delay(1000);
}
//play the piezzo buzzer when detect motion
void playMusic()
{
for (int i = 0; i < arraySize(melody); i++)
{
int noteTime = 1000 / duration[i];
tone(PIN_BUZZER, melody[i], noteTime);
int pauseTime = noteTime * 1.3;
delay(pauseTime);
}
}
void motionSensor()
{
// start trigger
digitalWrite(PIN_TRIGGER, LOW); // prepare
delayMicroseconds(2);
digitalWrite(PIN_TRIGGER, HIGH); // prepare
delayMicroseconds(10);
digitalWrite(PIN_TRIGGER, LOW); // prepare
int timeRoundTrip = pulseIn(PIN_ECHO, HIGH); // wait for round trip time
distance = timeRoundTrip * SPEED / 2;
if (distance >= MAX_RANGE ||
distance <= MIN_RANGE)
{
Serial.println("out of range");
// digitalWrite(PIN_BLUE, LOW);
}
else if (distance <= distanceToGND)
{
Serial.println(String(distance) + " cm");
// digitalWrite(PIN_BLUE, HIGH);
playMusic();
serveGum();
}
else
{
// digitalWrite(PIN_BLUE, LOW);
}
delay(500);
}
//get menu item from blynk
BLYNK_WRITE(V0)
{
//assign incoming value from pin V0 to a variable
int pinValue = param.asInt(); //or param.asStr() or .asDouble()
menuItem = pinValue;
}
//check if button is pushed in blynk
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
buttonPushed = pinValue;
}
//check if blynk wants to restart timer
BLYNK_WRITE(V3)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
restart = true;
}
else
{
restart = false;
}
}
void readButton()
{
//button (using analogRead, ran out of Digital pins)
int button = analogRead(PIN_BUTTON);
Serial.println(button);
//if button pushed, switch off or on
if (button < 100 && lastButtonState > 2000 && keepGoing == true)
{
//turns off the machine
keepGoing = false;
}
else if (button < 100 && lastButtonState > 2000 && keepGoing == false)
{
keepGoing = true;
}
lastButtonState = button;
delay(250);
}
void loop()
{
Blynk.run();
readButton();
if (keepGoing)
{
displayWelcome();
//if user wants to do study mode
if (menuItem == 2)
{
//restart study timer
if (restart)
{
data = 0;
}
unsigned long studyTime = millis();
//publish every 1 minute
if (studyTime - prevStudyTime > 60000)
{
//minutes
//publish minutes studied to initial state
Particle.publish("studyTime", String(data) + " min", PRIVATE);
data += 1;
prevStudyTime = studyTime;
}
}
//regular gumball mode
else if (menuItem == 1)
{
action();
}
//knocking mode (3 knocks to dispense gum)
else
{
useAccel();
}
}
else
{
Serial.println("Turned off");
}
}
void useAccel()
{
//CHECK IF DATA available
if (accel.available())
{
accel.read(); //read the values of the acceleration
if (accel.readTap() > 0)
{
//count as a bump
Serial.println("Tap!");
taps++;
if (taps == 3)
{
playMusic();
serveGum();
taps = 0;
}
}
}
}