Serpentine / Mbed 2 deprecated serpentine_snakecontroller_final

Dependencies:   MMA8451Q TSI mbed

Fork of snakecontroller by Serpentine

Revision:
11:551cad783135
Parent:
10:1c88d9d4ab81
Child:
12:be3ce60be6a0
diff -r 1c88d9d4ab81 -r 551cad783135 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 10 09:15:32 2016 +0000
@@ -0,0 +1,140 @@
+/**
+    Snake Controller
+    main.cpp
+    Purpose:    This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board.
+                It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS
+                oriented in the following configuration: o8o to provide feedback to the user.
+    @author Daniel Lock
+    @author Jamie Gnodde
+    @version
+*/
+
+
+#include "mbed.h"
+#include "TSISensor.h"
+#include "MMA8451Q.h"
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+Serial pc(USBTX, USBRX); // tx, rx - Set up USB serial to read back values for testing
+
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);    //Setup accelerometer
+DigitalOut lefthigh(PTC5);                          //LED for left
+DigitalOut leftlow(PTA4);
+DigitalOut righthigh(PTC11);                         //LED for right
+DigitalOut rightlow(PTC9);
+DigitalOut forwardhigh(PTC6);                       //LED for forward
+DigitalOut forwardlow(PTC10);
+DigitalOut backwardhigh(PTA5);                      //LED for backward
+DigitalOut backwardlow(PTC8);
+
+int Direction;                                      //2 bit number to describe direction 0 forward assigned CW
+
+bool right;                                         //Right or left? right = 1
+bool forward;                                       //Forward or Backward? forward = 1
+
+int main()
+{
+    while (1) 
+    {
+         
+        float accX=acc.getAccX();                           //Measure acceleration in X direction
+        float accY=acc.getAccY();                           //Measure acceleration in Y direction
+                
+        //Establish whether the board is tilted left or right               
+        if (accX > 0.1) 
+        {
+            right = false;
+            //printf("left \r\n");
+        }//endif
+
+        if (accX < -0.1) 
+        {
+            right = true;
+            //printf("right \r\n");
+        }//endif
+        
+        wait(0.1);                              //Not sure if this is left over from debugging
+
+        //Establish whether the board is tilted front or back
+        if (accY > 0.1) 
+        {
+            forward = false;
+            //printf("back \r\n");
+        }//endif
+
+        if (accY < -0.1) 
+        {
+            forward = true;            
+            //printf("for \r\n");
+        }//endif
+        
+        wait(0.1);                              //Not sure if this is left over from debugging
+        
+        //Establish the main axis of tilting so that the control outputs one direction
+        if(abs(accY) > abs(accX))
+        {
+            if(forward == true)
+            {
+                Direction = 0;                  //Direction variable is a two bit number 0-3 0 is forward
+                
+                lefthigh = 0;                   //Light up the forward LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 1;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;                
+            
+            }//endif
+            else
+            {
+                Direction = 2;                  //Direction variable is a two bit number 0-3 2 is backward
+                
+                lefthigh = 0;                   //Light up the backward LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 1;
+                backwardlow = 0;
+                   
+            }//endelse
+        }//endif
+        else
+        {
+            if(right == true)
+            {
+                Direction = 1;                  //Direction variable is a two bit number 0-3 1 is right
+                
+                lefthigh = 0;                   //Light up the right LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 1;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;
+             
+            }//endif
+            else
+            {
+                Direction = 3;                  //Direction variable is a two bit number 0-3 3 is left
+                
+                lefthigh = 1;                   //Light up the left LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;                
+                    
+            }//endelse
+        }//endelse
+        
+        printf("Direction = %d \r\n", Direction);          //Print the value of Direction to the serial for debugging       
+        
+    }//endwhile
+}//endmain