-
Notifications
You must be signed in to change notification settings - Fork 2
/
BLEwardrobeV2.ino
525 lines (453 loc) · 13.3 KB
/
BLEwardrobeV2.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <AccelStepper.h>
#include <ezButton.h>
#include <Wire.h>
#include <SparkFun_APDS9960.h>
#include <ArduinoBLE.h>
#include <String.h>
#define APDS9960_INT 2 // Needs to be an interrupt pin
BLEService BLE_Service_Wardrobe("1101");
BLEStringCharacteristic wardrobeChar("2101", BLERead | BLENotify | BLEWrite, 100);
BLEDevice central;
int firstConnect = 1;
int myHeight = 3000;
int warHeight = 0;
int liftOff = -1000;
int state_prev_st = 0;
int state_st = 0;
int state_prev_bot = 0;
int state_bot = 0;
int state_prev_top = 0;
int state_top = 0;
unsigned long bottomTime_0 = 0;
int bottomLimit = 7000;
unsigned long topTime_0 = 0;
int topLimit = 2000;
int topForwardMAX = 100;
int topBackwardMAX = 100;
float topCurrSpeed = 0;
float topLastSpeed = 0;
float accel = .05;
float deAccelForward = .07;
float deAccelBackward = .01;
const int bottomEN = 6;
const int bottomIn1 = 8;
const int bottomIn2 = 7;
const int topEN = 5;
const int topIn1 = 4;
const int topIn2 = 9;
const int pulseRight = 13;
const int dirRight = 12;
const int pulseLeft = 11;
const int dirLeft = 10;
const int limitSwitchPin = 2;
const int nxtButtonPin = 3;
const int IRpin = A0;
ezButton limitSwitch(limitSwitchPin);
ezButton nxtButton(nxtButtonPin);
bool limitReached = false;
AccelStepper stepperRight(AccelStepper::DRIVER, pulseRight, dirRight); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepperLeft(AccelStepper::DRIVER, pulseLeft, dirLeft); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
SparkFun_APDS9960 apds = SparkFun_APDS9960();
enum dir {
up,
down,
left,
right,
near,
far,
none
};
void setup() {
Serial.begin(9600);
//while(!Serial);
//////////////////////////////////////////////////////
/////////////Button Setup////////////////////////////
limitSwitch.setDebounceTime(50);
nxtButton.setDebounceTime(50);
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////Stepper Setup////////////////////////////
stepperRight.setMaxSpeed(2000.0);
//stepperRight.setSpeed(1000.0);
stepperRight.setAcceleration(2000.0);
stepperLeft.setMaxSpeed(2000.0);
stepperLeft.setAcceleration(2000.0);
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////Ln298 Setup////////////////////////////
pinMode(bottomEN, OUTPUT);
pinMode(bottomIn1, OUTPUT);
pinMode(bottomIn2, OUTPUT);
pinMode(topEN, OUTPUT);
pinMode(topIn1, OUTPUT);
pinMode(topIn2, OUTPUT);
// Set initial rotation direction
digitalWrite(bottomIn1, LOW);
digitalWrite(bottomIn2, HIGH);
digitalWrite(topIn1, HIGH);
digitalWrite(topIn2, LOW);
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
///////IR SENSOR//////
pinMode(IRpin, INPUT);
//////////////////////
//////////////////////////////////////////////////////
/////////////APDS Setup////////////////////////////
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
Serial.println(F("Gesture sensor is now running"));
} else {
Serial.println(F("Something went wrong during gesture sensor init!"));
}
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////BLE Setup///////////////////////////////
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin())
{
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("E-Triv-Wardrobe");
BLE.setAdvertisedService(BLE_Service_Wardrobe);
BLE_Service_Wardrobe.addCharacteristic(wardrobeChar);
BLE.addService(BLE_Service_Wardrobe);
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
//Initial Stepper Sequence
stepperRight.move(200);
stepperLeft.move(-200);
while(stepperRight.distanceToGo() > 0) {
stepperRight.run();
stepperLeft.run();
}
stepperRight.move(-200);
stepperLeft.move(200);
while(stepperRight.distanceToGo() > 0) {
stepperRight.run();
stepperLeft.run();
}
}
void loop() {
//Update EZ buttons
limitSwitch.loop();
nxtButton.loop();
//Run State Machines
SM_st();
SM_bot();
SM_top();
//Update Steppers
stepperRight.run();
stepperLeft.run();
//BLE
BLEheight();
/////Stepper SM debug///////////
if(state_st != state_prev_st) {
Serial.print("Current Stepper State: ");
Serial.println(state_st);
}
/////Top SM debug///////////
if(state_top != state_prev_top) {
Serial.print("Current Top State: ");
Serial.println(state_top);
}
/////Bot SM debug///////////
if(state_bot != state_prev_bot) {
Serial.print("Current Bot State: ");
Serial.println(state_bot);
}
// if(topCurrSpeed != topLastSpeed) {
// Serial.print("Current Speed: ");
// Serial.println(topCurrSpeed);
// Serial.print(" Last Speed: ");
// Serial.println(topLastSpeed);
// }
}
void SM_top() {
state_prev_top = state_top;
switch(state_top) {
case 0: //Reset
state_top = 1;
digitalWrite(topIn1, HIGH);
digitalWrite(topIn2, LOW);
analogWrite(topEN, 0);
topCurrSpeed = 0;
topLastSpeed = 0;
break;
case 1: //Start
if(state_st == 3) {state_top = 2;}
break;
case 2: //GO
topTime_0 = millis();
state_top = 3;
break;
case 3: //Move
if((millis() - topTime_0) > topLimit) {
if(!digitalRead(IRpin)) {
state_top = 4;
Serial.println(topCurrSpeed);
}
}
accelDC(20, topForwardMAX, accel);
analogWrite(topEN, topCurrSpeed);
break;
case 4: //Slow Down
if(topCurrSpeed > 0) {
accelDC(topForwardMAX, 0, deAccelForward);
analogWrite(topEN, topCurrSpeed);
}
else {
topLastSpeed = 0;
state_top = 5;
}
break;
case 5: //START GO!
if(state_st == 9) {
if(nxtButton.isReleased() || handleGesture() == far) {
//if(handleGesture() == left) {state_st = 10;}
topTime_0 = millis();
digitalWrite(topIn1, LOW);
digitalWrite(topIn2, HIGH);
//analogWrite(topEN, 133);
state_top = 6;
}
}
break;
case 6: //GOING
if((millis() - topTime_0) > topLimit) {
if(!digitalRead(IRpin)) {
state_top = 7;
}
}
accelDC(20, topBackwardMAX, accel);
analogWrite(topEN, topCurrSpeed);
break;
case 7: //SLOW DOWN & RESET
if(topCurrSpeed > 0) {
accelDC(topBackwardMAX, 0, deAccelBackward);
analogWrite(topEN, topCurrSpeed);
}
else {
state_top = 0;
}
break;
}
}
void SM_bot() {
state_prev_bot = state_bot;
switch(state_bot) {
case 0: //Reset
state_bot = 1;
analogWrite(bottomEN, 0);
break;
case 1: //Start
if(state_st == 2) {state_bot = 2; analogWrite(bottomEN, 255);}
break;
case 2: //GO
bottomTime_0 = millis();
state_bot = 3;
break;
case 3: //Wait & stop
if((millis() - bottomTime_0) > bottomLimit) {
state_bot = 0;
}
break;
}
}
void SM_st() {
state_prev_st = state_st;
switch(state_st) {
case 0: //Reset
state_st = 1;
break;
case 1: //StartIDLE
if(nxtButton.isReleased()) {state_st = 2;}
if(handleGesture() == right) {state_st = 2;}
break;
case 2: //MoveHome
stepperRight.move(-10000000);
stepperLeft.move(10000000);
state_st = 3;
break;
case 3: //Wait for Limit
if(limitSwitch.isPressed()) {
//stepperRight.setAcceleration(10000); stepperLeft.setAcceleration(10000);
//stepperRight.move(0); stepperLeft.move(0);
stepperRight.setAcceleration(0); stepperLeft.setAcceleration(0);
stepperRight.setSpeed(0); stepperLeft.setSpeed(0);
stepperRight.runSpeed(); stepperRight.runSpeed();
//stepperRight.move(0); stepperLeft.move(0);
state_st = 4;}
// else {
// stepperRight.setSpeed(5000); stepperLeft.setSpeed(5000);
// stepperRight.runSpeed(); stepperRight.runSpeed();
// }
break;
case 4: //Move to height
stepperRight.setAcceleration(2000); stepperLeft.setAcceleration(2000);
stepperRight.setMaxSpeed(2000); stepperLeft.setMaxSpeed(2000);
stepperRight.move(myHeight);
stepperLeft.move(-myHeight);
state_st = 5;
break;
case 5: //Wait for height
if(stepperRight.distanceToGo() == 0) {stepperRight.move(0); stepperLeft.move(0); state_st = 6;}
break;
case 6: //idle for lift off
if(nxtButton.isReleased()) {state_st = 7;}
if(handleGesture() == left) {state_st = 7;}
break;
case 7: //Lift off
stepperRight.move(liftOff);
stepperLeft.move(-liftOff);
state_st = 8;
break;
case 8: //Wait for Lift off
if(stepperRight.distanceToGo() == 0) {state_st = 9; stepperRight.move(0); stepperLeft.move(0);}
break;
case 9: //Idle for Top
if(state_top == 1) {state_st = 0;}
break;
}
}
void accelDC(float start, float end, float inc) {
topLastSpeed = topCurrSpeed;
if(topLastSpeed < 0) {topLastSpeed = 0;}
//if(topCurrSpeed == 0) {topLastSpeed = 0;}
if((end - start) > 0) {
if(topCurrSpeed < end) {
topCurrSpeed += inc;
}
else {topCurrSpeed = end; topLastSpeed = end;}
}
else {
if(topCurrSpeed > end) {
topCurrSpeed -= inc;
}
else {topCurrSpeed = end; topLastSpeed = end;}
}
}
dir handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
Serial.println("UP");
return up;
break;
case DIR_DOWN:
Serial.println("DOWN");
return down;
break;
case DIR_LEFT:
Serial.println("LEFT");
return left;
break;
case DIR_RIGHT:
Serial.println("RIGHT");
return right;
break;
case DIR_NEAR:
Serial.println("NEAR");
return near;
break;
case DIR_FAR:
Serial.println("FAR");
return far;
break;
default:
Serial.println("NONE");
return none;
}
}
return none;
}
void BLEheight() {
if(!central) {central = BLE.central(); firstConnect = 1;} //check if connected
else {
if(firstConnect) {Serial.print("Connected to central: "); Serial.println(central.address()); firstConnect = 0;}
BLE.poll();
if (wardrobeChar.written()) {
String value = wardrobeChar.value();
Serial.print("Received value: "); // for debugging
Serial.println(value); // for debugging
if (value == "adjustHeight" && state_st == 1) {
Serial.print("Adjusting Height");
wardrobeChar.writeValue("adjustStatustrue");
adjusting();
}
}
}
}
void adjusting() {
int height = 0;
homeSteppers();
goToMyHeight();
while (wardrobeChar.value() != "confirmHeight") {
//Serial.println("adjusting");
BLE.poll();
if (wardrobeChar.written()) {
String value = wardrobeChar.value();
Serial.print("Received value: "); // for debugging
Serial.println(value); // for debugging
// handle slide requests
if (value.startsWith("slide")) {
Serial.println("in Slide");
height = value.substring(5).toInt();
Serial.println(height);
if(height > 5.0) {
Serial.println("in adjust");
stepperRight.moveTo(height);
stepperLeft.moveTo(-height);
//Serial.println(stepperRight.distanceToGo());
while(stepperRight.currentPosition() != height) {
//Serial.println("in motor move");
stepperRight.run();
stepperLeft.run();
}
Serial.print("Current Pos: ");
Serial.println(stepperRight.currentPosition());
}
}
}
}
wardrobeChar.writeValue(String(height));
myHeight = height;
}
void homeSteppers() {
stepperRight.move(-10000000);
stepperLeft.move(10000000);
while(!limitSwitch.isPressed()) {
limitSwitch.loop();
stepperRight.run();
stepperLeft.run();
}
stepperRight.setAcceleration(0); stepperLeft.setAcceleration(0);
stepperRight.setSpeed(0); stepperLeft.setSpeed(0);
stepperRight.runSpeed(); stepperRight.runSpeed();
stepperRight.setAcceleration(2000); stepperLeft.setAcceleration(2000);
stepperRight.setMaxSpeed(2000); stepperLeft.setMaxSpeed(2000);
stepperRight.setCurrentPosition(0); stepperLeft.setCurrentPosition(0);
Serial.print("Current Pos: ");
Serial.println(stepperRight.currentPosition());
}
void goToMyHeight() {
stepperRight.moveTo(myHeight);
stepperLeft.moveTo(-myHeight);
while(stepperRight.distanceToGo() > 0) {
stepperRight.run();
stepperLeft.run();
}
Serial.print("Current Pos: ");
Serial.println(stepperRight.currentPosition());
}