Arduino int size

28 Mar 2015

In our Arduino project, we defined some constant integers like these:

static const int MESSAGE_BULLET_START = 30000;
static const int MESSAGE_BULLET_END = 39999;

Our use case was receiving an integer message via bluetooth and using the constants to define the type of message. However, the integer size on Arduino boards is 16 bit, which will allow you to use 216 = 65536 values. When using signed ints, the value can range between -32768 and 32768. That's why our message didn't work. We changed the constants to unsigned and our problem was over:

static const unsigned int MESSAGE_BULLET_START = 30000;
static const unsigned int MESSAGE_BULLET_END = 39999;

Perhaps this will save you some trouble, it costed us an hour or so.

back to blog
comments powered by Disqus