So, I'm working on RedBear Blend Micro V 1.0, and I must use the function:


To read some input from the serial monitor(Input being stuff I type in the Serial Monitor) and send it via bluetooth. It works perfectly this way!

Now what I need to do is create a random double data type number, and insert it in the place of Serial.read()

Read This:


Syntax: void ble_write(unsigned char data); ble_write sends a single byte data to the BLE Central.

So, can you please help me, to:

1)Generate a random double

2)Send it using the ble_write() function?

For a full list of function sin the ReadBear Library click on the link: https://github.com/RedBearLab/nRF8001

  • 1
    If it's random then why not just send random unsigned chars? Jul 16, 2016 at 16:06
  • @IgnacioVazquez-Abrams Thanks for the suggestion but the thing is, for now I want to send a definite NUMBER via the ReadBear BlendMicro, but later, instead of this number, i will be sending data from an ECG Sensor, the data from the ecg sensor is a Double..Dont worry Ive checked the ECG part Jul 17, 2016 at 2:15

1 Answer 1


Edit: Chris Stratton rightfully points out that the question is about a double, not a float:

Most, but not all Arduinos, are AVR-based. In the following I assume you are talking about AVR doubles, i.e. floats. If this is not the case, then just replace the array of 4 bytes by an array of 8 bytes.

Original answer: A float is four bytes, so it is somewhat equivalent to an array of four unsigned chars, i.e. to unsigned char[4]. You can use this equivalence with an union. For example, the following function will send a float byte by byte:

void ble_write_float(float x)
    union {
        float f;
        unsigned char c[4];
    } data;

    data.f = x;

Notice that this will send the bytes in whatever is the natural order of the sending processor. If it's an AVR (like most Arduinos), this will be LSB (least significant byte) first. You have to make sure that this is what the receiver expects.

Following Ignacio Vazquez-Abrams' suggestion, for sending a random float you could just send four random bytes. However, it may be wiser to have an idea of the random distribution you want for your float, and use things like random() and map() to achieve that distribution. Using four random bytes, you get a very wide distribution (all the possible floats are equally likely) which includes weird things like denormals, infinities and NaN (literally meaning “not a number”).

  • The question asks about a double while you answer with regard to a float so it would be helpful to mention that on the ATmega based Arduinos they are equivalent, however on some other varieties they are not. Oct 14, 2016 at 20:31
  • @ChrisStratton: Thanks for pointing this out. I amended the answer. Oct 16, 2016 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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