I coded this (BIN) into my Arduino sketch, and it's working (binary as expected), but I don't see it documented anywhere, so I don't know how to use it better, find others like it, and use it elsewhere. I guess I'd like to know the correct "programmer-speak", so I can do more research into other available options in similar scenarios:
randBinary = random(BIN);
I hacked the above into my code after seeing the following on official documentation (use of BIN):
Serial.print(78, BIN) gives "1001110"
from: https://www.arduino.cc/reference/en/language/functions/communication/serial/print/
BIN is a preprocessor macro defined as #define BIN 2
. It is defined in Print.h to use as second parameter of print
functions where the first parameter is of integer numeric type. The second parameter specifies the base in which the number should be printed. Default is decimal (DEC) of course. Other common number base formats used in computers are binary, hexadecimal and octal, so Print.h has preprocessor macros for 10, 2, 16 and 8 called DEC, BIN, HEX and OCT. The print function can be used with other base formats using a number as the second parameter. (Note. The second parameter in print function with first parameter as float or double is count of decimal places to print.)
The function random in Arduino has 2 overloaded versions. A version with one parameter max
and a version with two parameters min' and
max'.
The parameters of random specify the required range. For example if you want to choose a random day of week you will use random(7)
or random (1, 8)
. Other example is if you want to simulate a dice roll you will use random(1, 7)
.
You should not use BIN as parameter forrandom
. It will work because BIN is 2, but it is not the intended use of the macro BIN.