The objective of this lab is to write some embedded code to develop a program that responds almost instantaneously to an event using MicroPython. We achieved this objective by designing a program that tests the reaction time of a user in response to a light by pressing a button on the Nucleo. We implemented interrupts to achieve this functionality. This assignment was conducted in two parts to navigate the differences in the approaches and to see which approach is more efficient.
In the first approach, we implemented the program using the "utime" library. The program waited for a random time between 2 and 3 seconds and the turned on the green LED on the Nucleo. We recorded the time as soon as the LED turned on. We set up an external interrupt on the blue "User" button so that when the button is pressed we can again record the time and compute the difference between the two times to get the reaction time and output it to the terminal. The LED would remain On for only one second. When the program is stopped, the average reaction time is calculated and displayed on the screen. If no attempts were made, an error message is printed.
In the second approach, instead of using the "utime" library, we used built- in features like the timer modules of the STM32 MCU. We configured Timer 2 as a free-running counter and designed two callback functions to record the times on two different channels:
The overall flow of this approach was similar to the first one. It has the same functionality when the keyboard interrupt is pressed as in the first approach and the differnce in precision is neglible to the naked eye.
Both approaches achieve the objective of determining the reaction time of the user by incorporating features of the Nucleo. It is observed, however, that utilizing the built-in hardware achieves greater precision while handling the timing. Since this assignment's code is not too heavy on the CPU, the difference in precision is neglible to our objective.
Documentation of this lab can be found here: Lab0x02
The source code for this lab:
https://bitbucket.org/npatel68/me405_labs/src/master/lab2/lab2a.py
https://bitbucket.org/npatel68/me405_labs/src/master/lab2/lab2b.py