A byte stores an 8-bit unsigned number, from 0 to 255.
I can understand the following line:
byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal)
But I also saw a use such as:
volatile byte state = LOW;
What does the last line mean here? Is that special for Arduino? I mean LOW is not a 8 bit unsigned number. LOW is not a number here.
3 Answers 3
There is probably somewhere else, like this:
#define LOW 0
Which means the pre-processor turns your line from this:
volatile byte state = LOW;
into this:
volatile byte state = 0;
There is nothing arduino-specific here, just the use of standard C/C++ features.
It is common practise in C/C++ to define certain much-used constants at the beginning of the code.
#define true 1
this means that every time you write true in your code, the compiler will see it as 1.
The arduino IDE comes with a few constants predefined:
- true = 1
- false = 0
- HIGH = 1
- LOW = 0
- ...
-
in C++
true
andfalse
are already keywords, why would one try to#define
them?jfpoilpret– jfpoilpret2017年01月25日 07:28:41 +00:00Commented Jan 25, 2017 at 7:28 -
@jfpoilpret Perhaps if one is not using C++ ...Marcel Besixdouze– Marcel Besixdouze2021年03月21日 19:34:56 +00:00Commented Mar 21, 2021 at 19:34
In the same way that you can do
int blarb = 12;
#define wibble 73
you can do
int FOO = 73;
#define BINKY 99
or even
int LOW = 0;
#define HIGH 1
So in the line
volatile byte state = LOW;
someone has created some sort of numerical constant with the name LOW
.
You can read all about various constants provided for you here.