I'm trying to make a reaction time game on an stm32 board in C. I'm not sure how to return the exact reaction time, at the moment I made a loop which adds 50ms each time it loops, without getting interrupted.
if ((GPIOA->IDR & USER_GPIO_PIN) != 0x0)
{
//code
}
So, when you click the button (USER_GPIO_PIN) it should break the loop and return an exact reaction time number (not sure how to do this) or an estimate with
LCD_GLASS_DisplayString((uint8_t*)"text");
So is there any function to get the time difference between presses of the button?
Also, I tried adding a random delay to make the game more enjoyable (after a certain delay the led light flashes and you have to press the button), but the random function I tried, doesn't seem to work.
srand(time(NULL));
uint32_t randomDelay = rand();
srand
once, at the start of your program. – Weather Vaneclock()
might be appropriate. Mark the start time, subtract that from theclock()
value when the button is pressed. For the actual elapsed time see the man page andCLOCKS_PER_SEC
. – Weather Vane