Accelerometer Controlled Car

Dependencies:   FXOS8700Q mbed

Revision:
0:4f41abe16b42
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Apr 27 06:33:51 2018 +0000
@@ -0,0 +1,148 @@
+/* FXOS8700Q Example Program
+ * Copyright (c) 2014-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "FXOS8700Q.h"
+
+Serial pc(USBTX, USBRX);
+I2C i2c(PTE25, PTE24);
+FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);    // Configured for the FRDM-K64F with onboard sensors
+DigitalOut blue(LED3);
+DigitalOut red(LED1);
+DigitalOut green(LED2);
+DigitalOut m1f(PTC2);
+DigitalOut m1r(PTC3);
+DigitalOut m2f(PTC4);
+DigitalOut m2r(PTC12);
+
+//DigitalOut en1(PTA2);
+//DigitalOut en2(PTD0);
+
+int main(void)
+{
+    motion_data_units_t acc_data, mag_data;
+    motion_data_counts_t acc_raw, mag_raw;
+    float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
+    int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int;
+
+    acc.enable();
+    printf("FXOS8700QAccelerometer Who Am I= %X\r\n", acc.whoAmI());
+    while (true) { 
+        acc.getAxis(acc_raw);
+        acc.getX(raX);
+        acc.getY(raY);
+        acc.getZ(raZ);
+       printf("ACC: X=%06d Y=%06d Z=%06d \t MAG: X=%06d Y=%06d Z=%06d\r\n", acc.getX(tmp_int), acc.getY(tmp_int), acc.getZ(tmp_int));
+        
+        red =1;
+        green =1;
+        blue =1;     
+       
+       if(raZ>3500)
+           {
+           red=0;
+           //en1=0;
+           //en2=0;
+           m1f = 0;
+           m2f = 0;
+           m1r = 0;
+           m2r = 0;
+           
+           }
+           else
+           {
+           red =1;
+           //en1=0;
+           //en2=0;
+           m1f = 0;
+           m2f = 0;
+           m1r = 0;
+           m2r = 0;
+           } 
+       if(raY > 2500 || raY <-2500)
+           {
+           if(raY > 2500)
+           {
+           green = 0;
+           //en1=1;
+           //en2=1;
+           m1f = 1;
+           m2f = 1;
+           m1r = 0;
+           m2r = 0;
+           }
+           else if(raY < -2500)
+           {
+               green =0;
+               //en1=1;
+               //en2=1;
+               m1f = 0;
+               m2f = 0;
+               m1r = 1;
+               m2r = 1;
+            }
+           else
+           {
+           green =1;
+           //en1=0;
+           //en2=0;
+           m1f = 0;
+           m2f = 0;
+           m1r = 0;
+           m2r = 0;
+           }
+           }
+        if(raX > 2500 || raX <-2500)
+           {
+           if(raX > 2500)
+           {
+           blue = 0;
+           //en1=1;
+           //en2=0;
+           m1f = 1;
+           m2f = 0;
+           m1r = 0;
+           m2r = 0;
+           }
+           else if(raX < -2500)
+           {
+                
+                blue =0;
+               // en1=0;
+              //  en2=1;
+                m1f = 0;
+                m2f = 1;
+                m1r = 0;
+                m2r = 0;
+            }
+           else
+           {
+           blue =1;
+           //en1=0;
+           //en2=0;
+           m1f = 0;
+           m2f = 0;
+           m1r = 0;
+           m2r = 0;
+           }
+           }
+      
+        acc.getAxis(acc_data);
+        puts("");
+        wait(1.0f);
+    }
+    
+}
\ No newline at end of file