0

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

ble_write(Serial.read());

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:

ble_write

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? – Ignacio Vazquez-Abrams Jul 16 '16 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 – Aditya Indoori Jul 17 '16 at 2:15
2

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;
    ble_write(data.c[0]);
    ble_write(data.c[1]);
    ble_write(data.c[2]);
    ble_write(data.c[3]);
}

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. – Chris Stratton Oct 14 '16 at 20:31
  • @ChrisStratton: Thanks for pointing this out. I amended the answer. – Edgar Bonet Oct 16 '16 at 20:07

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.