this is program how build nRF51822 to get ADXL345 data
Dependencies: BLE_API mbed nRF51822
Fork of ADXL345_I2C by
Diff: main.cpp
- Revision:
- 6:bc835d0f686f
- Parent:
- 5:8242f251bcac
- Child:
- 7:31412349a322
--- a/main.cpp Mon Dec 04 15:11:37 2017 +0000 +++ b/main.cpp Wed Dec 06 03:10:08 2017 +0000 @@ -18,7 +18,9 @@ DigitalOut led1(LED1); Serial uart1(USBTX,USBRX); UARTService *uartServicePtr; - uint8_t sFlag = 0; + + + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { @@ -50,31 +52,62 @@ led1 = !led1; } -uint8_t c; -void uartRx(void) +int readings[3] = {0, 0, 0}; +int threshold =80; +int xval[100]={0}; +int yval[100]={0}; +int zval[100]={0}; +int xavg; +int yavg; +int zavg; +int steps, flag=0; +void calibrate(void) { - - c = uart1.getc(); - - if(sFlag < 39) - b[sFlag++] = c; - - if(c == '\r' || c == '\n' || sFlag == 15) - { - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, sFlag/*bytesRead*/); - sFlag = 0; - } + led1=1; + int sum1 = 0; + int sum2 = 0; + int sum3 = 0; + for (int i=0; i<100; i++) + { + xval[i]=(1); //nilai x adxl345 + sum1 = xval[i]+sum1; + } + wait(0.1); + xavg=sum1/100.0; + uart1.printf("%i\n", (int16_t)xavg); //nilai hasil kalibrasi x + + + for (int i=0; i<100; i++) + { + yval[i]=(1); //nilai y adxl345 + sum2 = yval[i]+sum2; + } + wait(0.1); + yavg=sum2/100.0; + uart1.printf("%i\n", (int16_t)yavg); //nilai hasil kalibrasi y + + for (int i=0; i<100; i++) + { + zval[i]=(1); //nilai z adxl345 + sum3 = zval[i]+sum3; + } + wait(0.1); + zavg=sum3/100.0; + uart1.printf("%i\n", (int16_t)zavg); //nilai hasil kalibrasi z + led1=0; } - - int main() { + int main() + { uart1.baud(9600); int readings[3] = {0, 0, 0}; + calibrate(); uart1.printf("Starting ADXL345 test...\n"); wait(.001); uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); wait(.001); + //Go into standby mode to configure the device. accelerometer.setPowerControl(0x00); @@ -88,11 +121,80 @@ //Measurement mode. accelerometer.setPowerControl(0x08); + int acc=0; + int totvect [100] = {0}; + int totave [100] = {0}; + int totvel [100] = {0}; + int totdist [100] = {0}; + int totheight [100] = {0}; + + + //float sum1, sum2, sum3 = 0 + double xaccl[100] = {0}; + double yaccl[100] = {0}; + double zaccl[100] = {0}; + + //float x,y,z + for (int i=0; i<100; i++) + { + xaccl[i]=(1); + wait(0.1); + yaccl[i]=(1); + wait(0.1); + zaccl[i]=(1); + wait(0.1); + + //formula + + totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zaccl[i] - zavg)*(zaccl[i] - zavg))); + totave[i] = (totvect[i] + totvect[i-1]) / 2 ; + totvel[i]=(totave[i]*0.2); + //totave1[i]=(totvel[i]+totvel[i-1])/2; + totdist[i]=(totvect[i]*(0.2*0.2)/2); + totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2); + //totave3[i]=((totheight[i]+totheight[i-1])/2); + acc=acc+totave[i]; + + uart1.printf("%i\n", (int16_t)totave[i]); + uart1.printf("%i\n", (int16_t)totvel[i]); + uart1.printf("%i\n", (int16_t)totdist[i]); + uart1.printf("%i\n", (int16_t)totheight[i]); + + wait(0.1); + + if (steps == 10) + { + uart1.printf("capek"); + } + + else + { + //nothing + } + + //cal steps + if (totave[i] > threshold && flag==0) + { + steps = steps+1; + flag=1; + } + else if (totave[i] > threshold && flag == 1) + { + // do nothing + } + if (totave[i] < threshold && flag == 1) + {flag=0;} + uart1.printf("%i\n", (int16_t)steps); + + + } + + led1 = 1; uart1.baud(9600); Ticker ticker; ticker.attach(periodicCallback, 1); - uart1.attach(uartRx,Serial::RxIrq); + DEBUG("Initialising the nRF51822\n\r"); @@ -121,9 +223,11 @@ wait(0.1); accelerometer.getOutput(readings); uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/); - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/); - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/); - } + ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/); + ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/); + ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/); + + } } +