Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q TSI mbed
Fork of snakecontroller by
Diff: main.cpp
- Revision:
- 8:93190b81ceca
- Parent:
- 7:292f2de3c013
- Child:
- 9:7628a5b8b6f2
--- a/main.cpp	Thu Jun 09 14:38:05 2016 +0000
+++ b/main.cpp	Thu Jun 09 18:25:29 2016 +0000
@@ -1,9 +1,21 @@
+/**
+    Snake Controller
+    snakeController.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
+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(PTC7);                         //Test LED for left
@@ -27,7 +39,8 @@
          
         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;
@@ -39,8 +52,10 @@
             right = true;
             //printf("right \r\n");
         }//endif
-        wait(0.1);    
+        
+        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;
@@ -53,15 +68,16 @@
             //printf("for \r\n");
         }//endif
         
-        wait(0.1);
+        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 = 0;                 //Direction variable is a two bit number 0-3 0 is forward
                 
-                lefthigh = 0;
+                lefthigh = 0;                   //Light up the forward LED, make sure all others are of
                 leftlow = 0;
                 righthigh = 0;
                 rightlow = 0;
@@ -118,7 +134,7 @@
             }//endelse
         }//endelse
         
-        printf("Direction = %d \r\n", Direction);
+        printf("Direction = %d \r\n", Direction);          //Print the value of Direction to the serial         
         
     }//endwhile
 }//endmain
    