For master node of whack a mole program

Dependencies:   mbed

Fork of ESE350-Whack-a-Mole by Eric Berdinis

Revision:
3:4f09529d391b
Parent:
2:73c0d4f0ff5c
Child:
4:76adb6c7e67d
diff -r 73c0d4f0ff5c -r 4f09529d391b whack_master.cpp
--- a/whack_master.cpp	Sun Oct 18 21:08:07 2015 +0000
+++ b/whack_master.cpp	Thu Oct 22 00:18:56 2015 +0000
@@ -77,86 +77,103 @@
 //***************** You can start coding here *****************//
 int main (void)
 {
-    
-    //Set the Number of slaves
-    int slaves = 2;
+
     
-    //Channels of the nodes
-    //channel1 = 15; //channel of the first node
-    //channel2 = 14; //channel of the first node
-    uint8_t channel;
+while(1){
     
-    //Set the Count to 10
-    int count = 0;
-    float begin = 0;
-    float end   = 0;
-    float diff  = 0;
-    float tdiff = 0;
     
-    //Start the timer
-    timer.start();
-    
-    //Set the yes and no arrays
-    char yes[] = "y";
-    char no[]  = "n";
-
-    while(count < 10) {
+        printf("Do you want to play? [y/n] \n");
+        char start = pc.getc();
+        
+        if(start == 'n'){
+            printf("Game Over\n");
+            exit(0); //exit is OK
+        }
+        
+        else if(start == 'y'){
+        
+        printf("Start Whacking ... \n");
+        //Set the Number of slaves
+        int slaves = 2;
         
-        //Send message to random node
+        //Channels of the nodes
+        //channel1 = 15; //channel of the first node
+        //channel2 = 14; //channel of the first node
+        uint8_t channel;
         
-        //Calculate random number
-        //srand(time(0));
-        channel = (rand()%(slaves)) + 14; //set to minimum channel 
-        //channel = 15;
+        //Set the Count to 10
+        int count = 0;
+        float begin = 0;
+        float end   = 0;
+        float diff  = 0;
+        float tdiff = 0;
         
-        //Set the Channel to the random node selected
-        mrf.SetChannel(channel);
+        //Start the timer
+        timer.start();
         
-        //Send message to the random slave and start timer
-        strcpy(txBuffer, "set");
-        rf_send(txBuffer, strlen(txBuffer) + 1);
-        //Switch on the Led when sending data
-        led2 = 1;
-        printf("Sending signal to channel %u \n", channel);
-        begin = timer.read_ms();
-        
-        //Wait for reply from the slave 
-        
-        while(1){
+        //Set the yes and no arrays
+        char yes[] = "y";
+        char no[]  = "n";
+    
+        while(count < 10) {
+            
+            //Send message to random node
+            
+            //Calculate random number
+            //srand(time(0));
+            channel = (rand()%(slaves)) + 14; //set to minimum channel 
+            //channel = 15;
             
-            rxLen = rf_receive(rxBuffer, 128);    
+            //Set the Channel to the random node selected
+            mrf.SetChannel(channel);
             
-            if(rxLen > 0) {
+            //Send message to the random slave and start timer
+            strcpy(txBuffer, "set");
+            rf_send(txBuffer, strlen(txBuffer) + 1);
+            //Switch on the Led when sending data
+            led2 = 1;
+            printf("Sending signal to mole %u \n", channel - 14);
+            begin = timer.read_ms();
+            
+            //Wait for reply from the slave 
+            
+            while(1){
                 
-                //Calculate time difference
-                end = timer.read_ms();
-                diff = end - begin;
-                tdiff += (diff / 1000);
-                
-                //Switch off Led to indicate received data
-                led2 = 0; 
+                rxLen = rf_receive(rxBuffer, 128);    
                 
-                //Check if mole was pressed or not
-                pc.printf("Received: %s\r\n", rxBuffer);
+                if(rxLen > 0) {
+                    
+                    //Calculate time difference
+                    end = timer.read_ms();
+                    diff = end - begin;
+                    tdiff += (diff / 1000);
+                    
+                    //Switch off Led to indicate received data
+                    led2 = 0; 
+                    
+                    //Check if mole was pressed or not
+                    pc.printf("Received: %s\r\n", rxBuffer);
+                    
+                    //If it was pressed - increment the count
+                    if(strcmp (yes,rxBuffer) == 0){
+                        pc.printf("pressed\n");
+                        count++;
+                    }
+                    //If it was not pressed - dont alter the count
+                    else if(strcmp (no,rxBuffer) == 0){
+                        pc.printf("Not pressed\n");
+                    }
+                        
+                    break;
+                    
+                }//end of "if" receive 
                 
-                //If it was pressed - increment the count
-                if(strcmp (yes,rxBuffer) == 0){
-                    pc.printf("pressed\n");
-                    count++;
-                }
-                //If it was not pressed - dont alter the count
-                else if(strcmp (no,rxBuffer) == 0){
-                    pc.printf("Not pressed\n");
-                }
-                    
-                break;
-                
-            }//end of "if" receive 
+            }//end of inner while 
             
-        }//end of inner while 
+        }//end of while count
         
-    }//end of while count
+        printf("Your did it in %f seconds\n",tdiff);
     
-    printf("Your did it in %f seconds\n",tdiff);
-    
+    }//end of else if start 
+  }//end of while 1
 }// end of main