Accelerometer game

Dependencies:   4DGL-uLCD-SE ADXL345 mbed-rtos mbed

Revision:
1:54ed477da4d3
Parent:
0:c69d1ec4d395
--- a/main.cpp	Thu Mar 20 03:22:57 2014 +0000
+++ b/main.cpp	Thu Mar 20 03:33:00 2014 +0000
@@ -4,48 +4,45 @@
 #include "ADXL345.h"
 
 
-PwmOut speaker(p21);
+PwmOut speaker(p21); //create a PwmOut object for the speaker
 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
-ADXL345 accelerometer(p5, p6, p7, p8);
-Serial pc(USBTX, USBRX);
-Mutex lcd_mutex;
+ADXL345 accelerometer(p5, p6, p7, p8);//create a global accelerometer object
+Serial pc(USBTX, USBRX);// for debugging
+Mutex lcd_mutex;// Create the mutex lock object 
 
 int red_car_x, red_car_y,blue_car_x,blue_car_y,frog_x,frog_y,i,j;
 int x=0,y=0;
-void thread1(void const *args)
+void thread1(void const *args)//thread 1 moves the red car
 {
     
-    while(true) {       
-    
-        
+    while(true) {    
+         //check for boundary conditions
         if(red_car_x>127){
         red_car_x=-16;
         red_car_y=32;
         
         }
-        lcd_mutex.lock();
-        uLCD.filled_rectangle(red_car_x,red_car_y,red_car_x+15,red_car_y+15,0x000000);
+        lcd_mutex.lock();//acquire lock
+        uLCD.filled_rectangle(red_car_x,red_car_y,red_car_x+15,red_car_y+15,0x000000);//erase previous position
         red_car_x=red_car_x+16;
         
         for(i=0;i<10;i++){
         uLCD.filled_rectangle(red_car_x,red_car_y,red_car_x+15,red_car_y+15,0xFF3300);//change position
               
-        }
-        
+        }      
              
-        lcd_mutex.unlock();
+        lcd_mutex.unlock();//release the lock
         Thread::wait(500);
     
     }
 }
 
 
-void thread2(void const *args)
+void thread2(void const *args)//thread 1 moves the blue car, code similar to thread 1
 {
     
-    while(true) {       
-    
-        
+    while(true) {  
+           
         if(blue_car_x>127){
         blue_car_x=-16;
         blue_car_y=64;
@@ -58,42 +55,33 @@
         uLCD.filled_rectangle(blue_car_x,blue_car_y,blue_car_x+15,blue_car_y+15,BLUE);//change position
               
         }
-        
-               
+                       
         lcd_mutex.unlock();
         Thread::wait(200);
     
     }
 }
 
-void thread3(void const *args)
+void thread3(void const *args)//thread 3 plays the sound effects
 {
     
      
-    while(true) {       
-    
-        
+    while(true) {    
+            
         speaker.period(1.0/150.0); // 500hz period
         speaker =0.25; //25% duty cycle - mid range volume
         wait(.02);
-        speaker=0.0;
-        
-     
+        speaker=0.0;//stop the speaker
+             
         Thread::wait(500);
     
     }
 }
-
-
-           
-  
-
- 
+      
 int main()
 {
-   
-    
-   A:uLCD.cls();
+       
+   A:uLCD.cls();// clear the LCD
     
     uLCD.text_width(2); //2X size text
     uLCD.text_height(2);
@@ -101,10 +89,10 @@
     
     uLCD.locate(2,2);
     
-    uLCD.printf("\n Frogger\n");
+    uLCD.printf("\n Frogger-G\n");//display the Title
     wait(0.1);
     
-    
+    // Sound effects at the start of the game
    for (i=0; i<500; i=i+100) {
         speaker.period(1.0/float(i));
         speaker=0.25;
@@ -128,16 +116,14 @@
     blue_car_y=64;
     frog_x=32;
     frog_y=96;
+    //initialize the frog position
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x99FF00);
     uLCD.filled_rectangle(red_car_x,red_car_y,red_car_x+15,red_car_y+15,0xff3300);
     uLCD.filled_rectangle(blue_car_x,blue_car_y,blue_car_x+15,blue_car_y+15,BLUE);
     
     int readings[3] = {0, 0, 0};
     
-   // pc.printf("Starting ADXL345 test...\n");
-   // pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
- 
-    //Go into standby mode to configure the device.
+     //Go into standby mode to configure the device.
     accelerometer.setPowerControl(0x00);
  
     //Full resolution, +/-16g, 4mg/LSB.
@@ -148,17 +134,16 @@
  
     //Measurement mode.
     accelerometer.setPowerControl(0x08);
-    
-   Thread t1(thread1); //start thread1
-   Thread t2(thread2);
+    //Initialize all threads
+   Thread t1(thread1); 
+   Thread t2(thread2); 
    Thread t3(thread3);
-   
-   
- 
+    
     while (1) {
     
         
        for(i=0;i<5;i++)
+       //average accelerometer readings for accuracy 
        { 
         accelerometer.getOutput(readings);
         pc.printf("%i, %i, %i            ", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
@@ -168,11 +153,12 @@
         x=int(x/5);
         y=int(y/5);
         
-        //13-bit, sign extended values.
+        //debug
        pc.printf("%d, %d   ", int(x),int(y) );
-        //right
-        
-        
+             
+        //move the frog according to accelerometer readings
+        //thresholds obtained through trial and error
+        //move right
         if(y>0){
     lcd_mutex.lock();
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x000000);
@@ -181,7 +167,7 @@
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x99FF00);
     lcd_mutex.unlock();
     }
-    //up
+    //move up
     if(x<70){
         lcd_mutex.lock();
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x000000);
@@ -190,7 +176,7 @@
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x99FF00);
     lcd_mutex.unlock();
     }
-    //left
+    //move left
     if(y<-70){
         lcd_mutex.lock();
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x000000);
@@ -199,7 +185,7 @@
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x99FF00);
     lcd_mutex.unlock();
     }
-    //down
+    //move down
     if(x>180){
         lcd_mutex.lock();
     uLCD.filled_rectangle(frog_x,frog_y,frog_x+15,frog_y+15,0x000000);
@@ -209,18 +195,17 @@
     lcd_mutex.unlock();
     }
     
+    //check collisions
     if ((frog_x==red_car_x & frog_y==red_car_y)|(frog_x==blue_car_x & frog_y==blue_car_y))
     {
-     
     lcd_mutex.lock();    
     uLCD.cls();
-    
-    
-    uLCD.text_width(2); //4X size text
+       
+    uLCD.text_width(2); //2X size text
     uLCD.text_height(2);
     uLCD.color(RED);
     uLCD.locate(2,2);
-    uLCD.printf("\n   GAME \n   OVER!\n");
+    uLCD.printf("\n   GAME \n   OVER!\n");// display message
     speaker.period(1.0/500.0); // 500hz period
     speaker =0.5; //50% duty cycle - max volume
     wait(3);
@@ -228,21 +213,20 @@
     
     wait(2);
     lcd_mutex.unlock();
-    goto A;
+    goto A;//restart the game
     }
-    
+    //check if frog has crossed the road
     if (frog_y<32)
     {
      
     lcd_mutex.lock();    
     uLCD.cls();
-    
-    
-   uLCD.text_width(2); //4X size text
+        
+   uLCD.text_width(2); //2X size text
     uLCD.text_height(2);
     uLCD.color(0xffff00);
     uLCD.locate(2,2);
-    uLCD.printf("\n   You \n   WIN!\n");
+    uLCD.printf("\n   You \n   WIN!\n");//display message
     
     for (i=0; i<500; i=i+100) {
         speaker.period(1.0/float(i));
@@ -251,7 +235,7 @@
     }
     wait(5);
     lcd_mutex.unlock();
-    goto A;
+    goto A;//restart the game
     }