How to get Timestamp data with heartrate sensor

4 views (last 30 days)
Hi,
I'm working with a Polar OH1 heartrate monitor and trying to capture HR data. Trying to figure out if I can capture it and also get a timestamp on when it was captured.
This is what my code looks like:
blelist
belt = ble("Polar OH1 7C7B3E2E")
hr = characteristic(belt, "Heart Rate", "Heart Rate Measurement")
data = read(hr)
for loop = 1:3000
flag = uint8(data(1));
% Get the first bit of the flag, which indicates the format of the heart rate value
heartRateValueFormat = bitget(flag, 1);
if heartRateValueFormat == 0
% Heart rate format is uint8
heartRate = data(2);
else
% Heart rate format is uint16
heartRate = double(typecast(uint8(data(2:3)), 'uint16'));
end
fprintf('Heart rate measurement: %d(bpm)\n', heartRate);
end
If anyone can tell me how to get this to write continously to a csv file and also get the data timestamped, that would be extremely helpful!
Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 12 Feb 2021
Edited: Walter Roberson on 12 Feb 2021
fopen() the output file at the top. Put in an onCleanup to close the file.
When you have data,
fprintf(fid, '%s,%d\n', datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS')), heartRate);
However you should consider fetching the datetime immediately after reading the data.
And reading the data should probably be done inside the loop.
Also, please sanity-check the uint16 case, as you might need to swap bytes.

Categories

Find more on Instrument Control Toolbox Supported Hardware in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!