top of page

SOFTware

The software for the Heart Rate Hat is done in C++ and utilizes the mbed RTOS for multithreading. The code itself is comprised of three parts

  • Bluetooth Functionality

  • Heart Rate Calculation

  • LED Selection

Bluetooth Functionality

Bluetooth is required for wireless user input. This is accomplished using a RawSerial connection with the Bluefruit UART. This interface has the user input a target heart rate for their workout. Using this target, the Bluetooth thread reverse engineers a target heart range and target interval to be used in the LED selection code. All Bluetooth code is run in a separate thread (bluetooth_thread)  which allows the user to update their target heart rate at any point in time. Minor input cleaning prevents the user from inputting non-numerical characters and a separate function (printToBluetooth) is used to print out uniform messages to the users phone. 

Heart Rate Calculation

The code performs heart rate calculations by first detecting a heart beat. This is done by establishing a threshold for the pulse sensor's PWM output. When the sensor reads in a measurement greater than the threshold, it is counted as a pulse. This threshold is calculated by first determining an upper and lower bound to the threshold. The code keeps weighted averages of the highest and lowest sensor outputs and the threshold is set as a weighted average of both the upper and lower bound. This favors the upper bound in order to avoid false positives. As shown in the graph below, the use of weighted averages maintains a reasonable and dynamic threshold.

​

Now that heartbeats can be faithfully detected, next is the measurement of the inter-beat interval (IBI). This measures the time between heartbeats and is used to calculate instantaneous heart rates. At each heartbeat, the time is read from a timer which is used to determine the IBI. In order to filter out anomalous readings from jittering and head motion, if the IBI is read as being 50% greater than or 33% less than the average IBI then the reading is thrown out. The average IBI is kept as a weighted average again to prevent anomalous readings from dominating the measure. As well, if the IBI reading is below .24 (40 BPM) or above 1.5 (270 BPM) then the interval is just reset to the average (as this is indicative of a runaway variable triggered by a missed heart beat). The heart rate is then calculated from the IBI

LED Selection

The final part of the code is used to illuminate the LED Array. To do this, the code uses the target interval and target heart range derived in the Bluetooth thread. In order to improve readability of the LED Array, a counter is used to track which LED (0-14) is illuminated and the number only moves up or down one each heartbeat. This creates a smoother "animation" of the LED Array. The counter is incremented depending on the current heart rate. If the heart rate is in a range belonging to a higher LED, then it is incremented by one, if it belongs to a lower LED, it is decremented. This number is then output in binary to the decoder and the selected LED is illuminated.

bottom of page