0
votes

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();
2
Only call srand once, at the start of your program.Weather Vane
As you are looking for human reaction times, clock() might be appropriate. Mark the start time, subtract that from the clock() value when the button is pressed. For the actual elapsed time see the man page and CLOCKS_PER_SEC.Weather Vane
Thanks Weather Vane :)Marty4570
There are a couple general purpose hardware timers, a Real Time Clock, and the system clock. You can use any of them.Bence Kaulics
What libraries do you use in development?Bence Kaulics

2 Answers

0
votes

The random functionality in software is just a table with a lot of random numbers. The srand function dictates where in the table you will begin to look.

In other words you will need a "random" number to begin with (aka seed), in order to get the process started. One way to derive this seed is to look at the current time as you did. If this did not work I would look at how srand is used (remember it should only be used once in the initialization) and also what the function time(NULL) returns.

The stm32 (and most other MCU) does have its own random generator built into the HW itself. This functionality becomes available by configuring a defined set of HW registers. ST Microelectronics is kind enough to supply us with both example code and a HAL library bundled together called "STM32Cube" that you can use as a guideline when configuring this. For the stm32f7 series, please see the file stm32f7xx_hal_rng.c.

0
votes

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)

The 'clean' way to do this is to attach an EXTI interrupt to your button GPIO pin to trigger on the appropriate edge that matches your button 'down' transition.

When you light the LED, record the current value of a timer. When you get the EXTI interrupt, subtract the new timer value from the old and set a variable to tell your main loop to wake up and do something. Systick is the easiest timer to use for this.

srand(time(NULL));

Not going to work unless you have a backed up RTC that's keeping real-time between resets and a library that's integrating with that.

If you want to seed a PRNG on the STM32 then sample some external randomness like the bottom bits of the temperature/voltage sensor ADC inputs or a floating pin. Better still, use the hardware RNG.