So for my project, I am measuring heartbeat of a person in a span of 10s. However, as the number of heartbeats may differ for each person, is it wise to use a vector?

  • What are you measuring? The time between each and every beat, or the number of beats in that 10 second period?
    – Majenko
    Apr 9 '18 at 11:57
  • @Majenko I am measuring the time when each pulse occurs within a span of 10s. I will then use this information and find out the BPM in the end. Apr 9 '18 at 11:59
  • Why store information about every heartbeat, if you only need an average. Just store the number of heartbeats, and the timestamp of the last one. Combined with the timestamp of the first heartbeat, gives you enough information to calculate the average. Or do you want to measure irregularities?
    – Gerben
    Apr 9 '18 at 14:54

No, you don't want a vector, for a number of reasons:

  1. There is no "vector" unless you add extra (unsupported) code.
  2. Vectors use dynamic memory. Arduinos don't work well with dynamic memory.
  3. It's pointless anyway.

Just allocate an array big enough to contain enough timestamps. In a 10 second period, if you get more than 100 beats, the person you are measuring the heart rate of is most likely in a noticeably "unfortunate" state. That's 600bpm. That's entering panicked mouse territory.

Even with that extreme number of beats an array of 100 32-bit values is only 400 bytes.

  • alright thanks a lot. Just wondering too, is there going to be a difference if I allocate a vector with a size of 100 vs an array with a size of 100? Also, can you explain when you mention arduinos dont work well with dynamic memory? Is it due to not having enough memory? Apr 9 '18 at 12:08
  • Yes, it's due to not having much memory. It's easy for heap fragmentation to occur, as evidenced when people over-use String variables and their program crashes randomly. Vectors are pointless. Don't even consider them. Erase them from your mind. They are a concept not compatible with little microcontrollers.
    – Majenko
    Apr 9 '18 at 12:10
  • Alright, thats a pity since in c++ we almost always use vectors instead of arrays. So the only container for arduino are arrays? Apr 9 '18 at 12:13
  • 1
    Not the "only", but the only "sensible" one.
    – Majenko
    Apr 9 '18 at 12:13
  • 1
    On a small MCU they should be avoided at all cost. As should new. alloca() is OK since it does stack allocation.
    – Majenko
    Apr 9 '18 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.