In this lab, you will: Construct a prototype

Dependencies:   ADXL362 mbed

Revision:
2:b56010953f54
Parent:
1:fedb70ea0eaa
Child:
3:33cca9e98fbb
--- a/main.cpp	Tue Feb 20 20:36:42 2018 +0000
+++ b/main.cpp	Wed Feb 21 21:06:45 2018 +0000
@@ -5,8 +5,10 @@
 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
 Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
 
 int adxl362_reg_print(int start, int length);
+void adxl362_get_average(int8_t avg[]);
 
 int main() {
     pc.printf("Starting program\n\r");
@@ -14,18 +16,73 @@
     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 avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2]
     
-    adxl362_reg_print(0,1);
-    wait_ms(1000);
-    /* Commented out while working on adxl362_reg_print
+    adxl362_reg_print(0,0); // Test of axdl_reg_print
+    wait_ms(1000); // wait so that values can be seen
+    int threshHoldRange = 10; // threshhold difference between avg and read values
+    adxl362_get_average(avg);// Generate an average of what x,y,z are.
+    //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
     while(1) {
         x=adxl362.scanx_u8();
         y=adxl362.scany_u8();
         z=adxl362.scanz_u8();
+        if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange)
+            || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange) 
+            //|| z > (zAvg + threshHoldRange) || z < (zAvg - threshHoldRange)) {
+            // commented out z as it gives weird values
+            ) {
+            /* print cases used to test threshhold range
+            pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
+            pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
+            pc.printf("Threshold range is = %d\r\n", threshHoldRange);
+            */
+            pc.printf("outside of threshold range\n\r");
+            myled = 1; // LED is ON
+            wait_ms(1900); // Wait 1.9s.  Last .1 sec is done while getting average
+            adxl362_get_average(avg); // Generate new average
+            myled = 0; // LED is OFF
+        }
         pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
         wait_ms(100);
     }
-    */
+}
+
+void adxl362_get_average(int8_t avg[]) {
+     // Generate an average of what x,y,z are.
+    int numberOfPolls = 10;
+    // reset average back to zero for each
+    int tmp[3]; // required because avg was having integer overflow
+    tmp[0] = 0;
+    tmp[1] = 0;
+    tmp[2] = 0;
+    avg[0] = 0;
+    avg[1] = 0;
+    avg[2] = 0;
+    int8_t x,y,z; 
+    // Poll each value 10 times
+    for (int i = 0; i < numberOfPolls; i++) {
+        //pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
+        x = adxl362.scanx_u8();
+        tmp[0]+= x;
+        y = adxl362.scany_u8();
+        tmp[1]+= y;
+        z=adxl362.scanz_u8();
+        tmp[2]+= z; 
+        wait_ms(10);
+        //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",tmp[0],tmp[1],tmp[2]);
+
+    } 
+    // Divide each value by the number of polls to get the average
+    tmp[0] = tmp[0] / numberOfPolls;
+    tmp[1] = tmp[1] / numberOfPolls;
+    tmp[2] = tmp[2] / numberOfPolls;
+    // store values from the tmp to the passed in array
+    avg[0] = tmp[0];
+    avg[1] = tmp[1];
+    avg[2] = tmp[2];
+    pc.printf("New Average: xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
+    wait_ms(5000);
 }
 
 int adxl362_reg_print(int start, int length) {
@@ -42,15 +99,11 @@
     
     // check if valid communication with device going
     // For some reason getting 0xAC instead of 0xAD?
-    // Printing out what value we are getting 
-    int ableToRead = adxl362.read_reg(adxl362.DEVID_AD);
-    pc.printf("ableToRead = %d\n\r", ableToRead);
-    /* TEMPORARILY COMMENTED OUT WHILE IT IS READING INCORRECTLY
-    if (ableToRead != 0xAD) {
+    // Printing out what value we are getting    
+    if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) {
         pc.printf("Error: Unable to read from DEVID_AD register\n\r");
         return -1;
-    }
-    */
+    }    
     // String array with all of the names of the different registers in order
     char regNames [37][20]  = {
          "DEVID_AD", "DEVID_MST", "PARTID",
@@ -69,8 +122,8 @@
     // Example for first two registers 
     // I think their is a better way to do this, but unsure of how.
     for (int i = 0; i < 2; i++) { 
-        uint8_t tmp; // hex value location of the register
-        uint8_t hexValue; // Hex value of the register
+        uint8_t tmp = 0; // hex value location of the register
+        uint8_t hexValue = 0; // Hex value of the register
         // May need to increment the registers inside of the if, since the values 
         // are not always sequential
         // You can find registers inside of ADXL362.h file in ADXL362 folder
@@ -84,7 +137,7 @@
         }
         pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[i], tmp); // Print register
     }
-    return 1;
+    return 0;
     
 // below is github with data for ADXL362 methods
  //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp