lab5
Dependencies: ADXL362
Diff: main.cpp
- Revision:
- 0:cf970e75e1ae
- Child:
- 1:839c14f3143c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Feb 26 01:39:55 2018 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +#include "ADXL362.h" +DigitalOut led1(LED1); + +// Interface pulled from ADXL362.cpp +// ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) : +ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1); + +int adxl362_reg_print(int start, int length){ + for(int x = 0x00; x<=0x2E; x++){ + if(start==x){ + if(length>0){ + if(ADXL362::DEVID_AD==0xAD){ + uint16_t return_val = adxl362.read_reg(ADXL362::DEVID_AD); + } + } + } + } + + //printf("%x\n","0x" , return_val); + return return_val; +} + +int main() { + adxl362.reset(); + wait_ms(600); // we need to wait at least 500ms after ADXL362 reset + adxl362.set_mode(ADXL362::MEASUREMENT); + int8_t x,y,z; + + int8_t current [3] = {0,0,0}; + int8_t accel [3] = {0,0,0}; + int8_t currAdd; + int8_t diffAdd; + float threshold = 2; + float val; + int knock_counter = 0; + + while(1) { + x=adxl362.scanx_u8(); + y=adxl362.scany_u8(); + z=adxl362.scanz_u8(); + current [0] = x; + current [1] = y; + current [2] = z; + currAdd = abs(current[0] + current [1] + current [2]); + wait_ms(10); + x=adxl362.scanx_u8(); + y=adxl362.scany_u8(); + z=adxl362.scanz_u8(); + accel[0] = x; + accel[1] = y; + accel[2] = z; + diffAdd = abs(accel[0] + accel [1] + accel [2]); + wait_ms(10); + val = abs(currAdd - diffAdd); + + if(val > threshold){ + printf("A knock has been heard ma dude\r\n"); + knock_counter ++; + printf("Knocks = %d\r\n" , knock_counter); + led1 = 1; + wait_ms(2000); + led1 = 0; + } + // printf("x = %d y = %d z = %d\r\n",x,y,z); + wait_ms(100); + + + + } +} \ No newline at end of file