f

Dependencies:   mbed

Fork of ESE350-Whack-a-Mole1 by ese519

Revision:
1:7fae8b781c61
Parent:
0:ddc820578cb0
--- a/WhackAMole.cpp	Wed Mar 20 19:05:36 2013 +0000
+++ b/WhackAMole.cpp	Tue Oct 20 00:17:13 2015 +0000
@@ -23,6 +23,15 @@
 char rxBuffer[128];
 int rxLen;
 
+// funcs
+int get_mole();
+void led_update(int i);
+
+// global vars
+int total_time = 0;
+int send_new_mole = 1;
+int round = 0;
+
 //***************** Do not change these methods (please) *****************//
 
 /**
@@ -87,6 +96,9 @@
 
     //Start the timer
     timer.start();
+    
+    // seed rand num generator
+    srand(time(NULL));
 
     while(true) {
         //Try to receive some data
@@ -95,19 +107,57 @@
             //Toggle the Led
             led1 = led1^1;
             pc.printf("Received: %s\r\n", rxBuffer);
+            
+            //if (strcmp(rxBuffer, "HIT")) {
+                total_time += timer.read_ms();
+                send_new_mole = 1;
+                round++;
+                wait(0.5);
+                printf("Your current score = %d ms\n",total_time);
+            //}
+            
+        }
+        
+        if (round == 10) {
+            printf("You hit 10 moles in %d seconds!\n",total_time);
+            exit(1);
         }
         
         //Send some data every second
-        if(timer.read_ms() >= 1000) {
+        if(send_new_mole) {
+            
+            send_new_mole = 0;
+            
             //Reset the timer to 0
             timer.reset();
-            // Toggle LED 2.
-            led2 = led2^1;
+
+            // get mole
+            string msg_out;
+            int mole = get_mole();
+            if (mole == 1) msg_out = "MOLE1";
+            if (mole == 2) msg_out = "MOLE2";
+            if (mole == 3) msg_out = "MOLE3";
+            led_update(mole);
+
             //Add to the buffer. You may want to check out sprintf
-            strcpy(txBuffer, "Jeff is the best TA!");
+            strcpy(txBuffer, msg_out.c_str());
             //Send the buffer
             rf_send(txBuffer, strlen(txBuffer) + 1);
             pc.printf("Sent: %s\r\n", txBuffer);
+                       
         }
     }
 }
+
+int get_mole() {
+    return rand()%3+1;
+}
+
+void led_update(int i) {
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+    if (i==1) led2 = 1;
+    if (i==2) led3 = 1;
+    if (i==3) led4 = 1;
+}