single player mbedKart

Dependencies:   Motor

(notes)

Revision:
37:44d2cbbd8507
Parent:
36:a844c1388408
Child:
38:db41ff8c0e00
--- a/main.cpp	Thu Dec 13 02:13:28 2018 +0000
+++ b/main.cpp	Thu Dec 13 02:29:12 2018 +0000
@@ -15,7 +15,7 @@
 void check_unpause() {
     char bnum=0;
     char bhit=0;
-    while (true) {
+    while (paused) {
         if (blue.getc()=='B') { //button data packet
             bnum = blue.getc(); //button number
             bhit = blue.getc(); //1=hit, 0=release
@@ -30,6 +30,8 @@
 
 void game_paused() {
     // Cycle through LEDs
+    left.speed(0.0);
+    right.speed(0.0);
     Thread pause_thread;
     pause_thread.start(check_unpause);
     while (paused) {
@@ -47,6 +49,35 @@
         }
         ThisThread::sleep_for(500);
     }
+    pause_thread.terminate();
+}
+
+void win() {
+    Thread pause_thread2;
+    pause_thread2.start(game_paused);
+    while(paused) {
+        unsigned int rgbColor[3];
+        
+        // Start off with red.
+        rgbColor[0] = 255;
+        rgbColor[1] = 0;
+        rgbColor[2] = 0;  
+        
+        // Choose the colors to increment and decrement.
+        for (int decColor = 0; decColor < 3; decColor += 1) {
+            int incColor = decColor == 2 ? 0 : decColor + 1;
+            
+            // cross-fade the two colors.
+            for(int i = 0; i < 255; i += 1) {
+                rgbColor[decColor] -= 1;
+                rgbColor[incColor] += 1;
+                
+                myRGBled.write(rgbColor[0]/255.0, rgbColor[1]/255.0, rgbColor[2]/255.0);
+                ThisThread::sleep_for(1);
+            }
+        }
+    }
+    pause_thread2.terminate();
 }
 
 // Thread to control speed
@@ -105,38 +136,14 @@
         }
         else if(R_value > 3000 && G_value < 2000 && B_value < 2000) {     //Check if cart reaches next checkpoint. cstate is set to 1 upon seeing first color of checkpoint
             pc.printf("Found red");
-            running = false;
+            paused = true;
+            win();
         }
         pc.printf("[C: %d, R: %d, G: %d, B: %d]\r\n", C_value, R_value, G_value, B_value); 
         ThisThread::sleep_for(500);
     }
 }
 
-void flash_RGB() {
-    while(1) {
-        unsigned int rgbColor[3];
-        
-        // Start off with red.
-        rgbColor[0] = 255;
-        rgbColor[1] = 0;
-        rgbColor[2] = 0;  
-        
-        // Choose the colors to increment and decrement.
-        for (int decColor = 0; decColor < 3; decColor += 1) {
-            int incColor = decColor == 2 ? 0 : decColor + 1;
-            
-            // cross-fade the two colors.
-            for(int i = 0; i < 255; i += 1) {
-                rgbColor[decColor] -= 1;
-                rgbColor[incColor] += 1;
-                
-                myRGBled.write(rgbColor[0]/255.0, rgbColor[1]/255.0, rgbColor[2]/255.0);
-                ThisThread::sleep_for(1);
-            }
-        }
-    }
-}
-
 int main() {
     pc.printf("Initializing ");
     
@@ -226,13 +233,9 @@
         }
     }
     // Clean up
-    pc.printf("Out of loop");
     duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
     pc.printf("%d",duration);
     thread1.terminate();
     thread2.terminate();
     thread3.terminate();
-    left.speed(0.0);
-    right.speed(0.0);
-    flash_RGB();
 }
\ No newline at end of file