MICHAEL CADIZPaul Rodolf P. Castor
Published

Assignment 2 Human Response Timer

Measure human reaction time using RT-SPARK board's built-in LEDs and hardware interrupts

IntermediateFull instructions provided5 hours7
Assignment 2 Human Response Timer

Story

Read more

Code

Assignment 2 Human Response Timer

C/C++
// leds.h
#ifndef LEDS_H
#define LEDS_H
#include "main.h"

void LED_On(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void LED_Off(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

#endif /* LEDS_H */

/////////////////////////////////////////////////////////////

// leds.c
#include "leds.h"

void LED_On(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
    HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
}

void LED_Off(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
    HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
}

//////////////////////////////////////////////////////

// delay.h
#ifndef DELAY_H
#define DELAY_H
#include "main.h"

void delay_ms(uint32_t ms);

#endif /* DELAY_H */

//////////////////////////////////////////////////////

// delay.c
#include "delay.h"

void delay_ms(uint32_t ms) {
    HAL_Delay(ms);
}

///////////////////////////////////////////////////////

/* USER CODE BEGIN Includes */
#include "leds.h"
#include "delay.h"
#include <stdlib.h>
/* USER CODE END Includes */

/////////////////////////////////////////////////////////////////////

/* USER CODE BEGIN PV */
// Global variables to track the interrupt and score
volatile int isr_triggered = 0;
volatile uint32_t final_reaction_time = 0;
/* USER CODE END PV */

////////////////////////////////////////////////////////////

/* USER CODE BEGIN 4 */
// The Interrupt Service Routine (ISR)
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
    if(GPIO_Pin == USER_SWITCH_Pin) {
        isr_triggered = 1;
    }
}
/* USER CODE END 4 */

////////////////////////////////////////////////////////////////////////

/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    
    // 1. Ensure LED is off to start
    LED_Off(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
    
    // 2. Clear the counter
    uint32_t counter = 0;
    
    // 3. Wait a random amount of time (1000ms to 3000ms)
    delay_ms((rand() % 2001) + 1000); 
    
    // 4. Visual cue: Turn on the Green LED
    LED_On(GREEN_LED_GPIO_Port, GREEN_LED_Pin); 
    
    // 5. Count rapidly until the LEFT button triggers the interrupt
    while (isr_triggered == 0) {
        counter++; 
    }
    
    // 6. Save the score to the global variable to view in the debugger
    final_reaction_time = counter; 
    
    // 7. Reset the flag and turn off the LED to show the press was registered
    isr_triggered = 0;
    LED_Off(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
    
    // 8. Wait 5 seconds before starting the next round automatically
    delay_ms(5000); 

    /* USER CODE END 3 */
  }

Credits

MICHAEL CADIZ
3 projects • 0 followers
Paul Rodolf P. Castor
20 projects • 9 followers
Thanks to Paul Rodolf.

Comments