//====================================================== // REACTION GAME MAIN ROUTINE //====================================================== /* REACTION GAME SETTINGS PARAMETERS NOTE: * ------------------------------------------ * * FORMAT : * -------- * {"tr": 10, "mn": 1, "mx": 2} * * MEANNING : * ---------- * tr : trials pre counter * mn : minimum delay, between trials * mx : maximum delay, between trials * * THINGS THAT ENDS THE GAME : * --------------------------- * 1. BY COUNT DOWN "tr" REACH TO ZERO * 2. BY A COMMAND "FROM APP VIA BLUETOOTH" * * CALCULATING RESULTS : * --------------------- * Rapid Kick Speed = Number Of Kickes Made / Elasped Time In Sec */ //====================================================== void game_reaction(String settings) { //------------------------------------------------------ // START OF READING THE SETTINGS //------------------------------------------------------ // Deserialize the JSON document DeserializationError error = deserializeJson(JSON, settings); // JSON ERROR if(error) { dualcomm(JSON_ERROR); return; } // PUBLIC VARIABLES uint8_t minDelay = JSON["mn"]; uint8_t maxDelay = JSON["mx"]; uint16_t trials = JSON["lm"]; uint16_t temp = trials; // Fix Random Error maxDelay += 1; // Clearing Buffer JSON.clear(); //------------------------------------------------------ // PREPARE FOR GAME START //------------------------------------------------------ // DisJSON Welcome Message terminal(""); terminal("trials : " + String(trials) + " Strikes"); terminal("minDelay : " + String(minDelay) + " Seconds"); terminal("maxDelay : " + String(maxDelay) + " Seconds"); terminal("2 Seconds To Start Get Ready & Good Luck !!"); terminal(""); // Ready Signal LED_SIGNAL_REACTION_START(); // Main Variables uint16_t sdelay = 0; uint16_t power = 0; uint16_t response = 0; unsigned long startTime = 0; unsigned long stopTime = 0; // Processing Results for(int i=0; i Get Ready !!"); // Set Random Delay Time sdelay = random(minDelay, maxDelay); sdelay *= 1000; delay(sdelay); // Light Up Green LED_SET_COLOR(0, 255, 0, 255); // Start Timer startTime = millis(); // Sensing Strike Power while(power < VTH) { // Reading The Strike power = readImpact(); // If Stop Command Came From Smart Phone if(isBluetooth) { String msg = Bluetooth.readStringUntil('\n'); msg.trim(); if(msg == CM_STOP) { dualcomm(GAME_OVER); // LightUp LED_SIGNAL_END(); // RESET return; } else if(msg == CM_RESET) { LED_SIGNAL_REACTION_RESET(); trials = temp; break; } } } // Stop Timer stopTime = millis(); // Stop Light LED_SET_COLOR(255, 0, 0, 255); // Calculating Response Time response = stopTime - startTime - 1; // Create Json JSON String msg = reaction_JSON_json(i+1, power, response, sdelay); // Print To Terminal dualcomm(msg); // Clear All sdelay = 0; power = 0; response = 0; } // Red Light Delay delay(500); // END GAME dualcomm(GAME_OVER); // Blink Red LED_SIGNAL_CELEBRATION(); // Return To Game Selector return; } //====================================================== // REACTION JSON JSON -> To Send Back To Server //====================================================== String reaction_JSON_json(unsigned int count, unsigned int power, unsigned int response, unsigned int startDelay) { // Convert To Seconds Unit float re = response; re /= 1000; // Asignning Values JSON["ct"] = count; // Strike Number JSON["pw"] = power; // Strike Power In Analog Read JSON["sd"] = startDelay/1000; // LED Strip Start Delay in Seconds JSON["re"] = String(re, 3).toFloat(); // Strike Reaction Time In Milli Sec String output; serializeJson(JSON, output); JSON.clear(); return output; } //====================================================== // REACTION GAME START LED SIGNAL //====================================================== void LED_SIGNAL_REACTION_START() { // Cross fade the white signal to get ready LED_CROSS_FADE(255, 255, 255, 3); // Set the LED strip color to red LED_SET_COLOR(255, 0, 0, 255); } //====================================================== // REACTION GAME RESET LED SIGNAL //====================================================== void LED_SIGNAL_REACTION_RESET() { // Light Bright Blue LED_BLINK(0, 0, 255, 2); // Wait For 1/4 Second delay(250); // Fade Out The Blue LED_SET_COLOR(0, 255, 0, 255); }