This program is a reaction time game for two players on the QW dev kit. The winner and his reaction time are shown in the console window and transmitted via Sigfox.

Dependencies:   mbed

Fork of QW-Reactiontime by Quicksand

QW Reaction Time Game

This program is a reaction time game for two players on the QW dev kit. The winner and his reaction time are shown in the console window and transmitted via Sigfox.

Code explanation

The program starts with the initialisation/declaration of the leds and pushbuttons. Also the necessary function prototypes and serial communications are declared. After that, the program is waiting to start the game. While the program is waiting, the leds are looping. Instructions to play the game are displayed in the console window. The game is started by pushing one of the buttons. Then the two players have to wait till all four leds turn on to push their button. The player that pushes the fastest after the leds turn on is the winner. The results of the game are shown in the console window and transmitted via Sigfox.

Sigfox message payload

First there is the "06", this is the Quicksand ID of the example program. This is used by Quicksand to keep track of our example programs. The second value that is transmitted is the ID of the winner. The third and last value that is transmitted is the reaction time of the winner.

More information and other example code can be found on the component page by clicking the link below: https://developer.mbed.org/components/QW-SIGFOX-Development-Kit/

Revision:
2:73b5ca784164
Parent:
1:00a17f5a247c
Child:
3:1bc9575369bd
--- a/main.cpp	Tue Dec 13 16:52:23 2016 +0000
+++ b/main.cpp	Thu Dec 15 09:57:11 2016 +0000
@@ -1,6 +1,6 @@
-/* This program demonstrates how to use the board to simulate a dual dice throw.
- * The result is displayed in binary on the leds and transmitted via Sigfox.
- * Open a serial console to the board to see extra info.
+/* This program demonstrates how to use the board to play a reaction time game.
+ * The winner and his reaction time are displayed in the console window and transmitted via Sigfox.
+ * Open a serial connection to the board to see extra info and instructions.
  */
  
 #include "mbed.h"
@@ -20,8 +20,6 @@
 InterruptIn SW1(PA_8);
 InterruptIn SW2(PB_10);
 
-
-
 /* Function prototypes */
 void sw1interrupt();
 void sw2interrupt();
@@ -31,19 +29,21 @@
 
 bool ser_timeout = false; 
 
-
 /* Serial port over USB */
 Serial pc(USBTX, USBRX);
 
 /* Serial connection to sigfox modem */
 Serial modem(PA_9, PA_10);
 
-bool dice1 = false;
-bool dice2 = false;
-uint8_t dice1val;
-uint8_t dice2val;
-uint8_t number;
+/* User variables */
+uint16_t randomTime;
+uint16_t reactionTime;
+uint8_t winner;
 
+bool play = false;
+bool player1 = false;
+bool player2 = false;
+bool busyPlaying =  false;
 
 int main() {   
     
@@ -64,103 +64,105 @@
     SW2.fall(&sw2interrupt); 
     
     /* Intializes random number generator */
-    srand((unsigned) time(&t));     
+    srand((unsigned) time(&t));
     
-    while(1) { 
+    /* Push a button to start the first game */    
+    pc.printf("\nPress a button to start the reaction time game...\n\r");
+    
+    while(1) {          
         
-        if(dice1 == true && dice2 == true){
-            number = dice1val + dice2val;
-            pc.printf("\nThe number is: %d\n\r", number);            
-            
-            switch(number) {
-                case 2  :
-                    LED_0 = 1;
-                    LED_1 = 1;
-                    LED_2 = 0;
-                    LED_3 = 1;
-                    break; 
-                case 3  :
-                    LED_0 = 1;
-                    LED_1 = 1;
-                    LED_2 = 0;
-                    LED_3 = 0;
-                    break;
-                case 4  :
-                    LED_0 = 1;
-                    LED_1 = 0;
-                    LED_2 = 1;
-                    LED_3 = 1;
-                    break; 
-                case 5  :
-                    LED_0 = 1;
-                    LED_1 = 0;
-                    LED_2 = 1;
-                    LED_3 = 0;
-                    break; 
-                case 6  :
-                    LED_0 = 1;
-                    LED_1 = 0;
-                    LED_2 = 0;
-                    LED_3 = 1;
-                    break; 
-                case 7 :
-                    LED_0 = 1;
-                    LED_1 = 0;
-                    LED_2 = 0;
-                    LED_3 = 0;
-                    break; 
-                case 8  :
-                    LED_0 = 0;
-                    LED_1 = 1;
-                    LED_2 = 1;
-                    LED_3 = 1;
-                    break;
-                case 9  :
-                    LED_0 = 0;
-                    LED_1 = 1;
-                    LED_2 = 1;
-                    LED_3 = 0;
-                    break; 
-                case 10 :
-                    LED_0 = 0;
-                    LED_1 = 1;
-                    LED_2 = 0;
-                    LED_3 = 1;
-                    break; 
-                case 11  :
-                    LED_0 = 0;
-                    LED_1 = 1;
-                    LED_2 = 0;
-                    LED_3 = 0;
-                    break;  
-                case 12  :
-                    LED_0 = 0;
-                    LED_1 = 0;
-                    LED_2 = 1;
-                    LED_3 = 1;
-                    break;                
-                default : 
-                    pc.printf("\nImpossible number!!\n\r");
-                    LED_0 = 1;
-                    LED_1 = 1;
-                    LED_2 = 1;
-                    LED_3 = 1;
+        /* When waiting to start a new game, show a loop on the leds */
+        while(play == false){            
+            if(play == false){  // Check every time, otherwise after interrupt, loop would be finished first
+                LED_0 = 1;LED_1 = 1;LED_2 = 1;LED_3 = 0;
+                wait_ms(100);
+            }
+            if(play == false){
+                LED_0 = 1;LED_1 = 1;LED_2 = 0;LED_3 = 1;
+                wait_ms(100); 
+            }
+            if(play == false){
+                LED_0 = 1;LED_1 = 0;LED_2 = 1;LED_3 = 1;
+                wait_ms(100); 
+            }
+            if(play == false){
+                LED_0 = 0;LED_1 = 1;LED_2 = 1;LED_3 = 1;
+                wait_ms(100);   
             }
-            char command[SER_BUFFER_SIZE];
-            sprintf(command, "AT$SF=04%04x,2,0\n", (int) number );
-            pc.printf("Sending thrown number =  %i over Sigfox using modem command: %s\n", number , command);
-            modem_command_check_ok(command);
-            //wait_ms(5000);
+            if(play == false){
+                LED_0 = 1;LED_1 = 0;LED_2 = 1;LED_3 = 1;
+                wait_ms(100);      
+            }
+            if(play == false){
+                LED_0 = 1;LED_1 = 1;LED_2 = 0;LED_3 = 1;
+                wait_ms(100); 
+            }                                 
+        }  
+        
+        /* After the 4 leds flash on, calculate the reaction time */
+        while(busyPlaying == true && player1==false && player2 == false){
+            reactionTime++;
+            wait_ms(1);
+        }
+        
+        /* When player 1 has won */       
+        if(player1 == true){
+            pc.printf("\nPlayer 1 was the fastest. His time was: %d ms\n\r", reactionTime);            
             LED_0 = 1;
             LED_1 = 1;
             LED_2 = 1;
-            LED_3 = 1;
-            dice1 = false;
-            dice2 = false;
+            LED_3 = 1;  
+            winner = 1;          
+            char command[SER_BUFFER_SIZE];
+            sprintf(command, "AT$SF=05%04x%04x,2,0\n", (int) winner, (int) reactionTime );
+            pc.printf("Sending winner = %i and reaction time =  %i ms over Sigfox\n", winner, reactionTime);
+            pc.printf("using modem command: %s\n", command);
+            modem_command_check_ok(command);
+            player1 = false;
+            player2= false;
+            busyPlaying = false;            
+            play = false;
+            reactionTime = 0;
+            pc.printf("\nPress a button to start a new game...\n\r");
+        }
+        
+        /* When player 2 has won */
+        if(player2 == true){
+            pc.printf("\nPlayer 2 was the fastest. His time was: %d ms\n\r", reactionTime);            
+            LED_0 = 1;
+            LED_1 = 1;
+            LED_2 = 1;
+            LED_3 = 1; 
+            winner = 2;           
+            char command[SER_BUFFER_SIZE];
+            sprintf(command, "AT$SF=05%04x%04x,2,0\n", (int) winner, (int) reactionTime );
+            pc.printf("Sending winner = %i and reaction time =  %i ms over Sigfox\n", winner, reactionTime);
+            pc.printf("using modem command: %s\n", command);
+            modem_command_check_ok(command);
+            player1 = false;
+            player2= false;
+            busyPlaying = false;            
+            play = false;
+            reactionTime = 0;
+            pc.printf("\nPress a button to start a new game...\n\r");
         }        
         
+        /* Check if a new game has been started, when a game has been started then generate a random time */
+        if(play == true){
+            pc.printf("\nGame has started, push your button as quick as possible when leds go on!\n\r");
+            randomTime =   (rand() % 5000)+1;   //random wait max 5 seconds
+            wait_ms( randomTime + 1000 ); // wait for random time + 1 second
+            busyPlaying =  true;
+            LED_0 = 0;
+            LED_1 = 0;
+            LED_2 = 0;
+            LED_3 = 0;
+        } 
+        
+        
     }
 }
+
 void modem_setup()
 {
     /* Reset to factory defaults */
@@ -187,7 +189,7 @@
 
 bool modem_command_check_ok(char * command)
 {
-    /* first clear serial data buffers */
+    /* First clear serial data buffers */
     while(modem.readable()) modem.getc();
     /* Timeout for response of the modem */
     Timeout tmout;
@@ -226,31 +228,37 @@
     tmout.detach();
     return ok;
 }
+
 /* Button 1 ISR */
 void sw1interrupt()
-{
-    if(dice1 == false){
-        pc.printf("\nDice 1 has been thrown\n\r");
-        dice1 =  true; 
-        dice1val = (rand() % 6)+1; 
-    }
-    else{
-        pc.printf("\nDice 1 has already been thrown\n\r");
-    }
+{    
+        if(play == false && busyPlaying == false){          // If not already playing... 
+         play = true;
+         LED_0 = 1;
+         LED_1 = 1;
+         LED_2 = 1;
+         LED_3 = 1;
+        }
+        if(busyPlaying == true && player2 != true){         // When playing and there is no winner yet...
+            player1 = true;
+        }   
          
 }
 
 /* Button 2 ISR */
 void sw2interrupt()
-{
-    if(dice2 == false){
-        pc.printf("\nDice 2 has been thrown\n\r");
-        dice2 =  true; 
-        dice2val = (rand() % 6)+1; 
-    }
-    else{
-        pc.printf("\nDice 2 has already been thrown\n\r");
-    }
+{    
+        if(play == false && busyPlaying == false){          // If not already playing... 
+         play = true;   
+         LED_0 = 1;
+         LED_1 = 1;
+         LED_2 = 1;
+         LED_3 = 1; 
+        }
+        if(busyPlaying == true && player1 != true){         // When playing and there is no winner yet...
+            player2 = true;
+        }       
+    
 }
 
 /* ISR for serial timeout */
@@ -258,6 +266,3 @@
 {
     ser_timeout = true;
 }
-
-
-