forked from changer65535/hangermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangermill.ino
767 lines (570 loc) · 22.7 KB
/
hangermill.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//Possible bug log
//added address of speed and incline characteristics
//11-14-2019-->Working on Windows
//#include <NeoPixelBrightnessBus.h>
//#include <NeoPixelBus.h>
/*
//ESP32 Arduino Settings
Board ESP32 Dev Module
Upload Speed: 921600
CPU Freq 80 mHz *BT/Wifi"
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include <String.h>
///////////SOUND FUNCTIONS///////////
TaskHandle_t Task1;
//NeoPixelBus<NeoGrbFeature, NeoEsp32I2s1800KbpsMethod> strip(LED_COUNT, LED_PIN);
//byte flags = 0b00111110;
#define WALKING_METS 1
#define RUNNING_METS 2
float fGrade = 5; //percent
float fSpeed = 8; //mph
int nHeartRate = 60; //bpm
int targetMetsMode = RUNNING_METS;
////////////////INTEGRATION VALUES//////////////////////
float fTotalCalories = 0;
float fWeight = 70.0;
float fElevationGain = 0;
float fIntegratedDistance = 0;
float fElevation = 0.0;
float fCadence = 180;
float fCadenceFast = 185;
bool _BLEClientConnected = false;
#define HEART_RATE_UUID BLEUUID((uint16_t)0x180d)
#define FITNESS_MACHINE_UUID BLEUUID((uint16_t)0x1826)
#define DEVICE_INFORMATION_UUID BLEUUID((uint16_t)0x180a)
BLECharacteristic *pFitnessMachineControlPoint;
class ControlPointCallback: public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
byte *pData = pCharacteristic->getData();
{
if (pData[0] == 0x00) //initiate procedure to request control of treadmill
{
byte controlPointResponse[8];
controlPointResponse[0] = 0x80;
controlPointResponse[1] = 0x00;
pFitnessMachineControlPoint->setValue((byte *)&controlPointResponse,2);
Serial.println("Request Control");
}
if (pData[0] == 0x01) //initiate procedure to reset treadmill
{
byte controlPointResponse[8];
controlPointResponse[0] = 0x80;
controlPointResponse[1] = 0x00;
pFitnessMachineControlPoint->setValue((byte *)&controlPointResponse,2);
Serial.println("Reset Treadmill");
}
if (pData[0] == 0x02) //initiate procedure to set Speed
{
Serial.println("Set Speed");
uint16_t nSpeed = (pData[1] & 0xff) + ((pData[2] & 0xff) << 8);
float fDesiredSpeed = nSpeed / 160.934;
byte controlPointResponse[8];
controlPointResponse[0] = 0x80;
controlPointResponse[1] = 0x02;
pFitnessMachineControlPoint->setValue((byte *)&controlPointResponse,2);
char szCommand[16];
char szSpeed[8];
dtostrf(fDesiredSpeed,0,2,szSpeed);
sprintf(szCommand,"speed=%s",szSpeed);
Serial2.println(szCommand);
Serial.println(szCommand);
}
if (pData[0] == 0x03) //initiate procedure to set Incline
{
Serial.println("Set Incline");
uint16_t nIncline = (pData[1] & 0xff) + ((pData[2] & 0xff) << 8);
float fIncline = nIncline / 10.0;
Serial.print("Desired Incline: "); Serial.println(fIncline);
byte controlPointResponse[8];
controlPointResponse[0] = 0x80;
controlPointResponse[1] = 0x03;
pFitnessMachineControlPoint->setValue((byte *)&controlPointResponse,2);
char szCommand[16];
char szIncline[8];
dtostrf(fIncline,0,2,szIncline);
sprintf(szCommand,"grade=%s",szIncline);
Serial2.println(szCommand);
Serial.println(szCommand);
}
}
/*std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
Serial.println("*********");
}*/
}
};
//BLECharacteristic heartRateMeasurementCharacteristics(BLEUUID((uint16_t)0x2A37), BLECharacteristic::PROPERTY_NOTIFY);
//BLECharacteristic sensorPositionCharacteristic(BLEUUID((uint16_t)0x2A38), BLECharacteristic::PROPERTY_READ);
//BLEDescriptor heartRateDescriptor(BLEUUID((uint16_t)0x2901));
//BLEDescriptor sensorPositionDescriptor(BLEUUID((uint16_t)0x2901));
///MYSTUFF HANGERMILL
BLECharacteristic treadmillDataCharacteristic(BLEUUID((uint16_t)0x2acd),BLECharacteristic::PROPERTY_NOTIFY);//this needs to stay global for data
byte fitnessMachineFeaturesArray[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};//FITNESS MACHINE FEATURES FIELD
//0 - Average Speed Supported
//1 - Cadence Supported
//2 - Total Distance Supported
//3 - Inclination Supported
//4 - Elevation Gain Supported
//5 - Pace Supported
//6 - Step Count Supported
//7 - Resistance Level Supported
//8 - Stride Count Supported
//9 - Expended Energy Supported
//10 - Heart Rate Measurement Supported
//11 - Metabolic Equivalent Supported
//12 - Elapsed Time Supported
//13 - Remaining Time Supported
//14 - Power Measurement Supported
//15 - Force on Belt and Power Output Supported
//16 - User Data Retention Supported
//includes both the supported features and the target features. Here, supporting target speed and target heart rate
/*
*17-31 Reserved
*TARGET SETTING FEATURES FIELD
*0 - Speed Target Setting Supported
*1 - Inclination Target Setting Supported
*2 - Resistance Target Setting Supported
*3 - Power Target Setting Supported
*4 - Heart Rate Target Setting Supported
*5 - Targeted Expended Energy Configuration Supported
*6 - Targeted Step Number Configuration Supported
*7 - Targeted Stride Number Configuration Supported
*8 - Targeted Distance Configuration Supported
*9 - Targeted Training Time Configuration Supported
*10 - Targeted Time in Two Heart Rate Zones Configuration Supported
*11 - Targeted Time in Three Heart Rate Zones Configuration Supported
*12 - Targeted Time in Five Heart Rate Zones Configuration Supported
*13 - Indoor Bike Simulation Parameters Supported
*14 - Wheel Circumference Configuration Supported
*15 - Spin Down Control Supported
*16 - Targeted Cadence Configuration Supported
*17-31 - Reserved
*
*
** FLAGS
*
*/
uint16_t supportedSpeedRangeArray[3] = {80,2896,16};//0.5 mph - 18 mph - 0.1kph all decimal minus 2
int16_t supportedInclinationRangeArray[3] = {-20,150,2};
byte trainingStatusArray[8] = {0,0,0,0,0,0,0,0};
class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer* pServer)
{
_BLEClientConnected = true;
Serial.println("BLE connected");
};
void onDisconnect(BLEServer* pServer)
{
_BLEClientConnected = false;
Serial.println("BLE disconnected");
}
};
void InitBLE()
{
Serial.println("Initializing BLE!!");
BLEDevice::init("HANGMILL3");
// Create the BLE Server
Serial.println("creating Server");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
Serial.println("Creaiging Service");
////////////TREADMILL STUFF///////////////
// Create the BLE Service
BLEService *pTreadmillService = pServer->createService(FITNESS_MACHINE_UUID);
Serial.print("Treadmill UUID: ");
Serial.println(pTreadmillService->getUUID().toString().c_str());
Serial.println("Setting fitness machine");
////////FITNESS MACHINE FEATURE CHACTERISTIC////////////////////
BLECharacteristic *pFitnessMachineFeatureCharacteristic = pTreadmillService->createCharacteristic (BLEUUID((uint16_t)0x2acc),BLECharacteristic::PROPERTY_READ);
pFitnessMachineFeatureCharacteristic->setValue((byte *)&fitnessMachineFeaturesArray,sizeof(fitnessMachineFeaturesArray));
Serial.println("Setting Speed Range");
////////SPEED RANGE CHARACTERISTIC////////////////////
BLECharacteristic *pSupportedSpeedRangeCharacteristic = pTreadmillService->createCharacteristic(BLEUUID((uint16_t)0x2ad4),BLECharacteristic::PROPERTY_READ);
pSupportedSpeedRangeCharacteristic->setValue((byte *)&supportedSpeedRangeArray,sizeof(supportedSpeedRangeArray));
Serial.println("Setting Incline Range");
//////////INCLINATION RANGE CHARACTERISTIC//////////////////////////////
BLECharacteristic *pSupportedInclinationRange = pTreadmillService->createCharacteristic(BLEUUID((uint16_t)0x2ad5),BLECharacteristic::PROPERTY_READ);
pSupportedInclinationRange->setValue ((byte *)&supportedInclinationRangeArray,sizeof(supportedInclinationRangeArray));
Serial.println("Setting Treadmill Data");
////////////////TREADMILL DATA CHARACTERISTIC/////////////////////////
pTreadmillService->addCharacteristic(&treadmillDataCharacteristic);
treadmillDataCharacteristic.addDescriptor(new BLE2902());
Serial.println("Treadmill Control Point");
////////////////FITNESS MACHINE CONTROL POINT/////////////////////////
pFitnessMachineControlPoint = pTreadmillService->createCharacteristic(BLEUUID((uint16_t)0x2ad9),BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
pFitnessMachineControlPoint->setCallbacks(new ControlPointCallback());
Serial.println("Device Info");
/////////////////////////////////////////////////////////////////////////////////
////DEVICE INFORMATION STUFF/////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
BLEService *pDeviceInformationService = pServer->createService(DEVICE_INFORMATION_UUID);
BLECharacteristic *pManufacturerNameCharacteristic = pDeviceInformationService->createCharacteristic(BLEUUID((uint16_t)0x2a29),
BLECharacteristic::PROPERTY_READ);
pManufacturerNameCharacteristic->setValue("HANGERMILL");
pTreadmillService->start();
pDeviceInformationService->start();
///ADVERTISING STUFF
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(FITNESS_MACHINE_UUID);
pAdvertising->addServiceUUID(DEVICE_INFORMATION_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
pAdvertising->start();
Serial.print("Treadmill Service UUID: ");
Serial.println(pTreadmillService->getUUID().toString().c_str());
Serial.print("Fitness Machine Characteristic UUID: ");
Serial.println(pFitnessMachineFeatureCharacteristic->getUUID().toString().c_str());
Serial.print("Supported Speed Range Characteristic UUID: ");
Serial.println(pSupportedSpeedRangeCharacteristic->getUUID().toString().c_str());
Serial.print("Supported Incline Range UUID: ");
Serial.println(pSupportedInclinationRange->getUUID().toString().c_str());
Serial.print("Treadmill Data UUID: ");
Serial.println(treadmillDataCharacteristic.getUUID().toString().c_str());
}
void tone (int freq,int delay)
{
unsigned long t = millis();
long d = 500000 / freq;
while (millis() - t < delay)
{
dacWrite(DAC1,64);
delayMicroseconds(d);
dacWrite(DAC1,0);
delayMicroseconds(d);
}
}
void noise(int delay,int hiFreq)
{
unsigned long t = millis();
while (millis() - t < delay)
{
int r = random(hiFreq/4,hiFreq);
for (int i = 0;i<1;i++)
{
dacWrite(DAC1,64);
delayMicroseconds(r);
dacWrite(DAC1,0);
delayMicroseconds(r);
}
}
}
void play(int hz, int duration)
{
tone(hz,duration);
}
void upChirp ()
{
play(610,75);
play(1220,75);
}
void doubleChirp ()
{
play(610,75);
play(1220,75);
play(610,75);
play(1220,75);
}
void doubleTone ()
{
play(610,30);
delay(30);
play(610,30);
}
void quickTone ()
{
play(1220,25);
}
void quickToneHigh ()
{
play(2440,25);
}
void pollSerial()
{
static char szStr[16] = "";
static uint16_t nIndex = 0;
//Serial.println(szStr);
while (Serial2.available() > 0)
{
char ch = Serial2.read();
//Serial.println(ch);
if ((ch != 10) && (ch != 13)) szStr[nIndex] = ch;
nIndex++;
if (nIndex >= 16) nIndex = 0;
if (ch == '\n')
{
szStr[nIndex-1] = 0;
if (szStr[0] == 'g') fGrade = atof(&szStr[2]);
if (szStr[0] == 's') fSpeed = atof(&szStr[2]);
if (szStr[0] == 'p') fCadence = atof(&szStr[2]);
if (szStr[0] == 'h') nHeartRate = atoi(&szStr[2]);
if (szStr[0] == 'c')
{
int nChirp = atoi(&szStr[2]);
if (nChirp == 1) upChirp();
if (nChirp == 2) doubleChirp();
if (nChirp == 3) doubleTone();
if (nChirp == 4) quickTone();
if (nChirp == 5) quickToneHigh();
}
if (szStr[0] == 'r')
{
fTotalCalories = 0;
fWeight = 70.0;
fElevationGain = 0;
fIntegratedDistance = 0;
}
nIndex = 0;
memset(szStr,0,sizeof(szStr));
}
}
//Now for regular serial entry
static char szCommandStr[16] = "";
static uint16_t nCommandIndex = 0;
while (Serial.available() > 0)
{
char ch = Serial.read();
if ((ch != 10) && (ch != 13)) szCommandStr[nCommandIndex] = ch;
nCommandIndex++;
if (nCommandIndex >= 16) nCommandIndex = 0;
if (ch == '\n')
{
Serial.println("Command read");
szCommandStr[nCommandIndex-1] = 0;
if (szCommandStr[0] == 'g') fGrade = atof(&szCommandStr[2]);
if (szCommandStr[0] == 's') fSpeed = atof(&szCommandStr[2]);
if (szCommandStr[0] == 'p') fCadence = atof(&szCommandStr[2]);
if (szCommandStr[0] == 'h') nHeartRate = atoi(&szCommandStr[2]);
if (szCommandStr[0] == 'c')
{
int nChirp = atoi(&szCommandStr[2]);
if (nChirp == 1) upChirp();
if (nChirp == 2) doubleChirp();
if (nChirp == 3) doubleTone();
if (nChirp == 4) quickTone();
if (nChirp == 5) quickToneHigh();
}
if (szStr[0] == 'r')
{
fTotalCalories = 0;
fWeight = 70.0;
fElevationGain = 0;
fIntegratedDistance = 0;
}
nCommandIndex = 0;
memset(szCommandStr,0,sizeof(szStr));
}
}
}
#pragma pack (push,1)
struct
{
byte flags1; //0 - More Data
//1 - Average Speed present
//2 - Total Distance Present
//3 - Inclination and Ramp Angle Setting present
//4 - Elevation Gain present
//5 - Instantaneous Pace present
//6 - Average Pace present
//7 - Expended Energy present
byte flags2; //8 - Heart Rate present
//9 - Metabolic Equivalent present
//10 - Elapsed Time present
//11 - Remaining Time present
//12 - Force on Belt and Power Output present
//13-15 Reserved
uint16_t instantaneousSpeed; //Kilometers per hour resolution of 0.01
uint16_t averageSpeed; //Kilometers per hour resolution of 0.01
byte totalDistance[3]; //meters with a resolution of 1 [OFFSET: 6]
int16_t inclination; //Percent with a resolution of 0.1 [OFFSET: 9]
int16_t rampAngleSetting; //Degree with a resolution of 0.1 [OFFSET:11]
uint16_t positiveElevationGain; //meters with a resolution of 0.1 [OFFSET 13]
uint16_t negativeElevationGain; //meters with a resolution of 0.1 [OFFSET:15]
uint8_t instantaneousPace; //Kilometers per minute with a resolution of 0.1 [OFFSET: 17]
uint8_t averagePace; //Kilometers per minute with a resolution of 0.1 [OFFSET: 18]
uint16_t totalEnergy; //KCAL with a resolution of 1 [OFFSET: 19]
uint16_t energyPerHour; //KCAL with a resolution of 1 [OFFSET: 21]
uint8_t energyPerMinute; //KCAL with a resolution of 1 [OFFSET: 23]
uint8_t heartRate; //Heart Rate beats per minute resolution of 1 [OFFSET: 24]
uint8_t metabolicEquivalent; //METS with resolution of 0.1 [OFFSET: 25]
uint16_t elapsedTime; //Elapsed time in seconds with a resolution of 1
uint16_t remainingTime; //Time Remaining in seconds with a resolution of 1
int16_t forceOnBelt; //Newton with a resolution of 1
int16_t powerOutput; //Watts with a resolution of 1
}treadmillData;
#pragma pack(pop)
#pragma pack (push,1)
struct
{
byte flags;
uint16_t speed; //meters per second. resolution is 1/256
byte cadence;//1/Miniute... RPM. resolution is 1
uint16_t strideLength;// in meters resolution 1/100 m (cm)
uint32_t totalDistance;// in meters resolution is 1/10 m (decimeter)
}RSCmeasurementData;
#pragma pack(pop)
float fWalkingMets()
{
float mpm = fSpeed * 26.8224;
return (0.1 * mpm + 1.8 * mpm * fGrade/100 + 3.5)/3.5;
}
float fRunningMets()
{
float mpm = fSpeed * 26.8224;
return (0.2 * mpm + 0.9 * mpm * fGrade/100 + 3.5)/3.5;
}
float fMets()
{
if (targetMetsMode == WALKING_METS) return fWalkingMets();
if (targetMetsMode == RUNNING_METS) return fRunningMets();
return (-1);
}
void integrateStuff()
{
static unsigned long lastCheck = millis();
//if (millis() - lastCheck < 1000) return;
float fDeltaSecs = (millis() - lastCheck) / 1000.0;//seconds
lastCheck = millis();
float deltaCalories = fDeltaSecs * fMets() * fWeight / 3600.0;
//Serial.println(deltaCalories);
if (fSpeed >= 0.5) fTotalCalories += deltaCalories;
fElevationGain = fElevationGain + (fGrade / 100 * 5280.0 * fDeltaSecs * fSpeed / 3600.0);
fIntegratedDistance += (fDeltaSecs * fSpeed / 3600.0);
//Serial.println(fDeltaSecs);
//Serial.print(fIntegratedDistance);
//Serial.print(" ");
//Serial.println(fDist());
}
void setup()
{
delay(3000);//sanity check
//DAC1 is 25
//DAC2 is 26
Serial.begin(115200);
Serial.println("Start");
Serial2.begin(57600);
InitBLE();
Serial.print ("DAC1: ");
Serial.println(DAC1);
Serial.print ("DAC2: ");
Serial.println(DAC2);
//for (int i = 0;i<100;i++)
//{
// noise (30);
// delay(337-30);
//
//}
Serial.println(sizeof(treadmillData));
}
void printTreadmillData()
{
Serial.print("instantaneousSpeed: ");Serial.print(treadmillData.instantaneousSpeed);Serial.print(" ");
Serial.print("averageSpeed: ");Serial.print(treadmillData.averageSpeed);Serial.print(" ");
uint32_t t = treadmillData.totalDistance[0] + (treadmillData.totalDistance[1] << 8) + (treadmillData.totalDistance[2] << 16);
Serial.print("totalDistance: ");Serial.print(t);Serial.println(" ");
Serial.print("inclination: ");Serial.print(treadmillData.inclination);Serial.print(" ");
Serial.print("Ramp Angle: ");Serial.print(treadmillData.rampAngleSetting);Serial.println();
}
void updateBT ()
{
/**********THIS IS HUGE!!*****************
* FLAG INDEX 0 is 1
* FLAG INDEX 1 is 2
* FLAG INDEX 2 is 4
* etc....
* to set FLAGS indexed 0,1,2, set the byte to 1 + 2 + 4 = 7!
*/
memset(&treadmillData,0,sizeof(treadmillData));
static unsigned int lastUpdate = millis();
if (millis() - lastUpdate < 1000) return;
lastUpdate = millis();
//////////////FLAGS////////////////////
treadmillData.flags1 = 0b11111110;
treadmillData.flags2 = 0b00011111;
/////////////////////////////fSpeed//////////////////////
float fSpeedConst = constrain (fSpeed,0,16);
treadmillData.instantaneousSpeed = (uint16_t)(fSpeedConst * 160.934);
////////////////////////fGrade//////////////////////
treadmillData.inclination = (int16_t)(fGrade * 10.0);
float angle = atan(fGrade / 100.0)*180.0/3.14159;
treadmillData.rampAngleSetting = (int16_t)(angle * 10.0);
////////////////////averageSpeed///////////////////////
float fAverageSpeed = 6.0;
uint16_t nAverageSpeed = (uint16_t)(fAverageSpeed * 160.934);
///////////TOTAL DISTANCE///////////////////////
uint32_t td = fIntegratedDistance * 1609.34; //in meters;
treadmillData.totalDistance[0] = (byte)td & 0xff;
treadmillData.totalDistance[1] = (byte)(td >> 8) & 0xff;
treadmillData.totalDistance[2] = (byte)(td >> 16) & 0xff;
//treadmillData.totalDistance[0] = 0;
//treadmillData.totalDistance[1] = 0;
//treadmillData.totalDistance[2] = 0;
/////////////ELEVATION GAIN////////////
treadmillData.positiveElevationGain = fElevationGain * 0.3048 * 10.0; //feet to meters * 10
treadmillData.negativeElevationGain = 0;
/////////HEART RATE///////////////////////
treadmillData.heartRate = nHeartRate;
//////////////////////METS////////////////////
treadmillData.metabolicEquivalent = (byte)(fRunningMets() * 10);
//////////////////////KCAL////////////////////
treadmillData.totalEnergy = (uint16_t)(fTotalCalories * 10);
//printTreadmillData();
treadmillDataCharacteristic.setValue((byte *)&treadmillData,sizeof(treadmillData));
treadmillDataCharacteristic.notify();
}
void loop()
{
//Serial.println("Looping");
static unsigned long lastPaceCheck = millis();
static bool onOff = false;
//static RgbColor black(0,0,0);
static unsigned long lastBTUpdate = millis();
static float lastSpeed = 0;
static float lastGrade = 0;
pollSerial();
if (lastSpeed != fSpeed)
{
lastSpeed = fSpeed;
//Serial.print("fSpeed: ");
//Serial.println(fSpeed);
}
if (lastGrade != fGrade)
{
lastGrade = fGrade;
//Serial.print("fGrade: ");
//Serial.println(fGrade);
}
integrateStuff();
//int nSecs = millis() / 1000;
//fSpeed = (nSecs % 100) / 10.0;
//fGrade = (nSecs % 150) / 10.0;
updateBT();
if (fCadence > 0)
{
long fmsecPace = 60000 / fCadence;
if (fSpeed > 8.4) fmsecPace = 60000 / fCadenceFast;
if (fSpeed > 1)
{
if (millis() - lastPaceCheck > fmsecPace)
{
//Serial.println(millis() - lastPaceCheck);
lastPaceCheck = millis();
onOff = !onOff;
if (onOff) noise(30,2000);
else noise(30,1000);
}
}
}
}