Using boolean variables instead of HIGH/LOW in digitalWrite();

Someone please tell my why this doesn't compile. I'm just expecting the same behaviour as classic blink sketch for the moment.

const bool on = HIGH;
const bool off = LOW;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, on);   
  delay(1000);                       
  digitalWrite(LED_BUILTIN, off);    
  delay(1000);                    
}

It compiles for me. You have to reveal more. You can copy error messages from the compiler and post them here.

Here are the error messages.

Blink:9:31: error: cannot convert 'const bool' to 'PinStatus' for argument '2' to 'void digitalWrite(pin_size_t, PinStatus)'
   digitalWrite(LED_BUILTIN, on);
                                                  ^
Blink:11:32: error: cannot convert 'const bool' to 'PinStatus' for argument '2' to 'void digitalWrite(pin_size_t, PinStatus)'
   digitalWrite(LED_BUILTIN, off);
                                                  ^
exit status 1

Okay, then we need to know more about your Arduino IDE environment. I have 1.8.12 on Windows 10.

Er OK, this is more involved that I thought it would be. But that's OK.
I have Visual Studio Code 1.43.2, running on Ubuntu 18.04.4LTS.
I guess I could try running the same sketch on my Arduino IDE...

Which type arduino are you using? Some of the newer boards, such as the Nano Every, have changed to using an enum for digitalWrite, still uses HIGH and LOW but they are an enum of type PinStatus.

Interesting. It's an UNO WIFI Rev 2.

siddyboy:
Interesting. It's an UNO WIFI Rev 2.

Makes sense, both the Uno WiFi Rev2 and the Nano Every use the Arduino MegaAVR core, and digitalWrite() uses an enum, which is known to break compatibility with code written for other cores.

 typedef enum {
  LOW     = 0,
  HIGH    = 1,
  CHANGE  = 2,
  FALLING = 3,
  RISING  = 4,
} PinStatus;

You could still use on & off is you use #define

#define on HIGH
#define off LOW

Another way to toggle the output pin is to write CHANGE to the pin, making the blink sketch very simple:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, CHANGE);
  delay(1000);
}

hence why it's important to always respect formal type coherence... a pin status is not a boolean and relying on the compiler or to equivalence between values of constants to drive ones sketch is calling for trouble.

Arduino's team doing

 typedef enum {
  LOW     = 0,
  HIGH    = 1,
  CHANGE  = 2,
  FALLING = 3,
  RISING  = 4,
} PinStatus;

is also pretty poor, they should have pinned the type to uint8_t instead of the default which is an integer. That will drive more memory usage than necessary. They should have done

 typedef enum : uint8_t {
  LOW     = 0,
  HIGH    = 1,
  CHANGE  = 2,
  FALLING = 3,
  RISING  = 4,
} PinStatus;

Great stuff. Thanks for the help. Problem solved. I can achieve everything I need in one line. I guess I'm using an out of date book (10 years old) for guidance...

digitalWrite(LED_BUILTIN, CHANGE);

there is no consensus that those PinStatus or PinMode thingy is worth the trouble... Actually many people asking to get rid of the idea.

see Breakage caused by PinStatus and PinMode types #25