this profile using UART with ADXL335 accelerometer, but this is can't reach accelerometer sensor.. please help me to fix it

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
asyrofi
Date:
Sat Dec 09 06:25:35 2017 +0000
Revision:
15:34c3e96ce282
Parent:
14:4dfb61282efb
welldone;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:e910d9bb040f 1
yihui 0:e910d9bb040f 2 #include "mbed.h"
rgrover1 12:33153cf38631 3 #include "ble/BLE.h"
yihui 0:e910d9bb040f 4
rgrover1 12:33153cf38631 5 #include "ble/services/UARTService.h"
yihui 0:e910d9bb040f 6
asyrofi 15:34c3e96ce282 7 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
Rohit Grover 2:e060367b9024 8 * it will have an impact on code-size and power consumption. */
yihui 0:e910d9bb040f 9
Rohit Grover 2:e060367b9024 10 #if NEED_CONSOLE_OUTPUT
rgrover1 6:e0fc9072e853 11 #define DEBUG(...) { printf(__VA_ARGS__); }
yihui 0:e910d9bb040f 12 #else
Rohit Grover 2:e060367b9024 13 #define DEBUG(...) /* nothing */
Rohit Grover 2:e060367b9024 14 #endif /* #if NEED_CONSOLE_OUTPUT */
yihui 0:e910d9bb040f 15
Rohit Grover 2:e060367b9024 16 BLEDevice ble;
Rohit Grover 2:e060367b9024 17 DigitalOut led1(LED1);
rgrover1 6:e0fc9072e853 18 UARTService *uartServicePtr;
asyrofi 15:34c3e96ce282 19 Serial uart1(USBTX, USBRX);
yihui 0:e910d9bb040f 20
asyrofi 15:34c3e96ce282 21 AnalogIn analog_value1(p4);
asyrofi 15:34c3e96ce282 22 AnalogIn analog_value2(p5);
asyrofi 15:34c3e96ce282 23 AnalogIn analog_value3(p6);
asyrofi 14:4dfb61282efb 24
asyrofi 14:4dfb61282efb 25 DigitalOut led(LED1);
asyrofi 14:4dfb61282efb 26
rgrover1 13:15764cc1f12c 27 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
yihui 0:e910d9bb040f 28 {
Rohit Grover 2:e060367b9024 29 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 30 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 31 ble.startAdvertising();
Rohit Grover 2:e060367b9024 32 }
yihui 0:e910d9bb040f 33
asyrofi 15:34c3e96ce282 34 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
asyrofi 15:34c3e96ce282 35
asyrofi 15:34c3e96ce282 36 DEBUG("Connected!\n\r");
asyrofi 15:34c3e96ce282 37
asyrofi 15:34c3e96ce282 38 }
asyrofi 15:34c3e96ce282 39
asyrofi 15:34c3e96ce282 40
asyrofi 15:34c3e96ce282 41 uint8_t b[40]={'a','d','q','w'};
asyrofi 15:34c3e96ce282 42 void onDataWritten1(const GattWriteCallbackParams *params)
yihui 0:e910d9bb040f 43 {
asyrofi 15:34c3e96ce282 44
asyrofi 15:34c3e96ce282 45
rgrover1 12:33153cf38631 46 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 47 uint16_t bytesRead = params->len;
asyrofi 15:34c3e96ce282 48 DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
asyrofi 15:34c3e96ce282 49
yihui 0:e910d9bb040f 50 }
Rohit Grover 2:e060367b9024 51 }
yihui 0:e910d9bb040f 52
asyrofi 15:34c3e96ce282 53
Rohit Grover 2:e060367b9024 54 void periodicCallback(void)
Rohit Grover 2:e060367b9024 55 {
rgrover1 6:e0fc9072e853 56 led1 = !led1;
asyrofi 15:34c3e96ce282 57
Rohit Grover 2:e060367b9024 58 }
yihui 0:e910d9bb040f 59
asyrofi 15:34c3e96ce282 60 int threshold =80;
asyrofi 15:34c3e96ce282 61 int xval[10]={0};
asyrofi 15:34c3e96ce282 62 int yval[10]={0};
asyrofi 15:34c3e96ce282 63 int zval[10]={0};
asyrofi 15:34c3e96ce282 64 int xavg;
asyrofi 15:34c3e96ce282 65 int yavg;
asyrofi 15:34c3e96ce282 66 int zavg;
asyrofi 15:34c3e96ce282 67
asyrofi 15:34c3e96ce282 68
asyrofi 15:34c3e96ce282 69 int calibratex()
asyrofi 15:34c3e96ce282 70 {
asyrofi 15:34c3e96ce282 71 led1=1;
asyrofi 15:34c3e96ce282 72 long int sum1 = 0;
asyrofi 15:34c3e96ce282 73 for (int i=0; i<10; i++)
asyrofi 15:34c3e96ce282 74 {
asyrofi 15:34c3e96ce282 75
asyrofi 15:34c3e96ce282 76 xval[i] = analog_value1.read_u16(); //nilai x adxl335
asyrofi 15:34c3e96ce282 77 sum1 = yval[i]+sum1;
asyrofi 15:34c3e96ce282 78 }
asyrofi 15:34c3e96ce282 79
asyrofi 15:34c3e96ce282 80 xavg=sum1/10.0;
asyrofi 15:34c3e96ce282 81
asyrofi 15:34c3e96ce282 82 led1=0;
asyrofi 15:34c3e96ce282 83 return xavg;
asyrofi 15:34c3e96ce282 84 }
asyrofi 15:34c3e96ce282 85 int calibratey()
asyrofi 15:34c3e96ce282 86 {
asyrofi 15:34c3e96ce282 87 led1=1;
asyrofi 15:34c3e96ce282 88 long int sum2 = 0;
asyrofi 15:34c3e96ce282 89 for (int i=0; i<10; i++)
asyrofi 15:34c3e96ce282 90 {
asyrofi 15:34c3e96ce282 91
asyrofi 15:34c3e96ce282 92 yval[i] = analog_value2.read_u16(); //nilai y adxl335
asyrofi 15:34c3e96ce282 93 sum2 = yval[i]+sum2;
asyrofi 15:34c3e96ce282 94 }
asyrofi 15:34c3e96ce282 95 yavg=sum2/10.0;
asyrofi 15:34c3e96ce282 96 led1=0;
asyrofi 15:34c3e96ce282 97 return yavg;
asyrofi 15:34c3e96ce282 98 }
asyrofi 15:34c3e96ce282 99 int calibratez()
asyrofi 15:34c3e96ce282 100 {
asyrofi 15:34c3e96ce282 101
asyrofi 15:34c3e96ce282 102 led1=1;
asyrofi 15:34c3e96ce282 103 long int sum3 = 0;
asyrofi 15:34c3e96ce282 104 for (int i=0; i<10; i++)
asyrofi 15:34c3e96ce282 105 {
asyrofi 15:34c3e96ce282 106
asyrofi 15:34c3e96ce282 107 zval[i]=analog_value3.read_u16(); //nilai z adxl335
asyrofi 15:34c3e96ce282 108 sum3 = zval[i]+sum3;
asyrofi 15:34c3e96ce282 109 }
asyrofi 15:34c3e96ce282 110
asyrofi 15:34c3e96ce282 111 zavg=sum3/10.0;
asyrofi 15:34c3e96ce282 112 led1=0;
asyrofi 15:34c3e96ce282 113 return zavg;
asyrofi 15:34c3e96ce282 114 }
asyrofi 15:34c3e96ce282 115
asyrofi 15:34c3e96ce282 116
yihui 0:e910d9bb040f 117 int main(void)
yihui 0:e910d9bb040f 118 {
asyrofi 15:34c3e96ce282 119 uart1.baud(9600);
asyrofi 15:34c3e96ce282 120 char buffer [20];
asyrofi 15:34c3e96ce282 121
asyrofi 14:4dfb61282efb 122 printf("\nAnalogIn Example\n");
asyrofi 14:4dfb61282efb 123
Rohit Grover 2:e060367b9024 124 led1 = 1;
Rohit Grover 2:e060367b9024 125 Ticker ticker;
Rohit Grover 2:e060367b9024 126 ticker.attach(periodicCallback, 1);
yihui 0:e910d9bb040f 127
Rohit Grover 2:e060367b9024 128 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 129 ble.init();
Rohit Grover 2:e060367b9024 130 ble.onDisconnection(disconnectionCallback);
asyrofi 15:34c3e96ce282 131 ble.onDataWritten(onDataWritten1);
yihui 0:e910d9bb040f 132
Rohit Grover 2:e060367b9024 133 /* setup advertising */
Rohit Grover 2:e060367b9024 134 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 2:e060367b9024 135 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 136 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 137 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 138 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 139 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 140
rgrover1 12:33153cf38631 141 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
Rohit Grover 2:e060367b9024 142 ble.startAdvertising();
yihui 0:e910d9bb040f 143
rgrover1 6:e0fc9072e853 144 UARTService uartService(ble);
rgrover1 6:e0fc9072e853 145 uartServicePtr = &uartService;
asyrofi 15:34c3e96ce282 146
asyrofi 15:34c3e96ce282 147 int avg1 = calibratex();
asyrofi 15:34c3e96ce282 148 int avg2 = calibratey();
asyrofi 15:34c3e96ce282 149 int avg3 = calibratez();
asyrofi 15:34c3e96ce282 150
asyrofi 15:34c3e96ce282 151 //inisiliasi
asyrofi 15:34c3e96ce282 152 int steps=0;
asyrofi 15:34c3e96ce282 153 int flag=0;
asyrofi 15:34c3e96ce282 154 int acc=0;
asyrofi 15:34c3e96ce282 155 int totvect [20] = {0};
asyrofi 15:34c3e96ce282 156 int totave [20] = {0};
asyrofi 15:34c3e96ce282 157 int totvel [20] = {0};
asyrofi 15:34c3e96ce282 158 int totdist [20] = {0};
asyrofi 15:34c3e96ce282 159 int totheight [20] = {0};
asyrofi 15:34c3e96ce282 160
asyrofi 15:34c3e96ce282 161
asyrofi 15:34c3e96ce282 162 //float sum1, sum2, sum3 = 0
asyrofi 15:34c3e96ce282 163 int xaccl[20];
asyrofi 15:34c3e96ce282 164 int yaccl[20];
asyrofi 15:34c3e96ce282 165 int zaccl[20];
asyrofi 15:34c3e96ce282 166
asyrofi 15:34c3e96ce282 167 // Test Daata
asyrofi 15:34c3e96ce282 168 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 169 /* int16_t reading_1= 0;
asyrofi 15:34c3e96ce282 170 int16_t reading_2= 0;
asyrofi 15:34c3e96ce282 171 int16_t reading_3= 0;
asyrofi 15:34c3e96ce282 172 int16_t avg_1= 0;
asyrofi 15:34c3e96ce282 173 int16_t avg_2= 0;
asyrofi 15:34c3e96ce282 174 int16_t avg_3= 0;*/
asyrofi 15:34c3e96ce282 175
asyrofi 15:34c3e96ce282 176 int16_t lang = 0;
asyrofi 15:34c3e96ce282 177 int16_t perc = 0;
asyrofi 15:34c3e96ce282 178 int16_t kec = 0;
asyrofi 15:34c3e96ce282 179 int16_t jar = 0;
asyrofi 15:34c3e96ce282 180 int16_t ting = 0;
asyrofi 15:34c3e96ce282 181 snprintf(buffer, 20, "data: %d,%d,%d,%d,%d\n",(int16_t)lang,(int16_t)perc,(int16_t)kec,(int16_t)jar,(int16_t)ting);
asyrofi 15:34c3e96ce282 182 /* snprintf(buffer, 20, "data: %d,%d,%d,%d,%d,%d\n",(int16_t)reading_1,(int16_t)reading_2,(int16_t)reading_3,(int16_t)avg_1,(int16_t)avg_2,(int16_t)avg_3);*/
asyrofi 15:34c3e96ce282 183
asyrofi 15:34c3e96ce282 184
yihui 0:e910d9bb040f 185
asyrofi 14:4dfb61282efb 186 while (true)
asyrofi 14:4dfb61282efb 187 {
Rohit Grover 2:e060367b9024 188 ble.waitForEvent();
asyrofi 15:34c3e96ce282 189 int16_t x = analog_value1.read_u16();
asyrofi 15:34c3e96ce282 190 int16_t y = analog_value2.read_u16();
asyrofi 15:34c3e96ce282 191 int16_t z = analog_value3.read_u16();
asyrofi 15:34c3e96ce282 192 uart1.printf("\r x = %d y = %d z = %d \n",(int16_t)x,(int16_t)y,(int16_t)z);
asyrofi 15:34c3e96ce282 193 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 194 snprintf(buffer, 20, "data: %d,%d,%d\n", (int16_t)x,(int16_t)y,(int16_t)z);
asyrofi 15:34c3e96ce282 195 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
asyrofi 15:34c3e96ce282 196
asyrofi 15:34c3e96ce282 197 uart1.printf("\n%i, %i, %i\n", (int16_t)avg1, (int16_t)avg2, (int16_t)avg3);
asyrofi 15:34c3e96ce282 198 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 199 snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)avg1,(int16_t)avg2,(int16_t)avg3);
asyrofi 15:34c3e96ce282 200 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
asyrofi 15:34c3e96ce282 201
asyrofi 14:4dfb61282efb 202 wait(0.5);
asyrofi 15:34c3e96ce282 203
asyrofi 15:34c3e96ce282 204 //float x,y,z
asyrofi 15:34c3e96ce282 205 for (int i=0; i<10; i++)
asyrofi 15:34c3e96ce282 206 {
asyrofi 15:34c3e96ce282 207 xaccl[i]=(analog_value1.read_u16());
asyrofi 15:34c3e96ce282 208 wait(0.1);
asyrofi 15:34c3e96ce282 209 yaccl[i]=(analog_value2.read_u16());
asyrofi 15:34c3e96ce282 210 wait(0.1);
asyrofi 15:34c3e96ce282 211 zaccl[i]=(analog_value3.read_u16());
asyrofi 15:34c3e96ce282 212 wait(0.1);
asyrofi 15:34c3e96ce282 213
asyrofi 15:34c3e96ce282 214 //formula
asyrofi 15:34c3e96ce282 215 totvect[i] = sqrt((double)((xaccl[i]-xavg)* (xaccl[i]-xavg))+(double) ((yaccl[i] - yavg)*(yaccl[i] - yavg)) +(double) ((zaccl[i] - zavg)*(zaccl[i] - zavg)));
asyrofi 15:34c3e96ce282 216 totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
asyrofi 15:34c3e96ce282 217 uart1.printf("\t acc: %i\n", (int16_t)totave[i]);
asyrofi 15:34c3e96ce282 218 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 219 snprintf(buffer, 20, "\t acc: %d\n", (int16_t)totave[i]);
asyrofi 15:34c3e96ce282 220 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 15:34c3e96ce282 221
asyrofi 15:34c3e96ce282 222 totvel[i]=(totave[i]*0.2);
asyrofi 15:34c3e96ce282 223 uart1.printf("\t vel: %i\n", (int16_t)totvel[i]);
asyrofi 15:34c3e96ce282 224 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 225 snprintf(buffer, 20, "\t vel: %d\n", (int16_t)totvel[i]);
asyrofi 15:34c3e96ce282 226 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 15:34c3e96ce282 227
asyrofi 15:34c3e96ce282 228 totdist[i]=(totvect[i]*(0.2*0.2)/2);
asyrofi 15:34c3e96ce282 229 uart1.printf("\t dist: %i\n", (int16_t)totdist[i]);
asyrofi 15:34c3e96ce282 230 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 231 snprintf(buffer, 20, "\tdist: %d\n", (int16_t)totdist[i]);
asyrofi 15:34c3e96ce282 232 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 15:34c3e96ce282 233
asyrofi 15:34c3e96ce282 234 totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2);
asyrofi 15:34c3e96ce282 235 uart1.printf("\thght: %i\n", (int16_t)totheight[i]);
asyrofi 15:34c3e96ce282 236 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 237 snprintf(buffer, 20, "\thght: %d\n", (int16_t)totheight[i]);
asyrofi 15:34c3e96ce282 238 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 15:34c3e96ce282 239
asyrofi 15:34c3e96ce282 240 acc=acc+totave[i];
asyrofi 15:34c3e96ce282 241
asyrofi 15:34c3e96ce282 242
asyrofi 15:34c3e96ce282 243
asyrofi 15:34c3e96ce282 244 //cal steps
asyrofi 15:34c3e96ce282 245 if (totave[i] > threshold && flag==0)
asyrofi 15:34c3e96ce282 246 {
asyrofi 15:34c3e96ce282 247 steps = steps+1;
asyrofi 15:34c3e96ce282 248 flag=1;
asyrofi 15:34c3e96ce282 249 }
asyrofi 15:34c3e96ce282 250 else if (totave[i] > threshold && flag == 1)
asyrofi 15:34c3e96ce282 251 {
asyrofi 15:34c3e96ce282 252 // do nothing
asyrofi 15:34c3e96ce282 253 }
asyrofi 15:34c3e96ce282 254 if (totave[i] < threshold && flag == 1)
asyrofi 15:34c3e96ce282 255 {flag=0;}
asyrofi 15:34c3e96ce282 256 uart1.printf("steps: %i\n", (int16_t)steps);
asyrofi 15:34c3e96ce282 257 memset(&buffer, 0, sizeof(buffer));
asyrofi 15:34c3e96ce282 258 snprintf(buffer, 20, "\nsteps: %d\t", (int16_t)steps);
asyrofi 15:34c3e96ce282 259 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 15:34c3e96ce282 260
asyrofi 15:34c3e96ce282 261 }
yihui 0:e910d9bb040f 262 }
asyrofi 14:4dfb61282efb 263
yihui 0:e910d9bb040f 264 }