5

I am a newbie to micro controllers and struggling to figure out how to fix this issue. When using an older Arduino micro controller it works fine, but when I use a new board (Arduino Uno Rev 2 Wifi) it gives me this error:

cannot convert 'bool' to 'PinStatus' for argument '2' to 'void digitalWrite(pin_size_t, PinStatus)'

At this line :

digitalWrite(pin_enable_line, !digitalRead(pin_enable_line));

Library I am using is: ReceiveOnlySoftwareSerial.

  • Can you show us the sketch you are using? – VE7JRO Apr 19 at 3:19
  • 2
    nice. the MegaAVR core use the new common Arduino API and it has enum for pin status. use HIGH and LOW, not bool. so digitalWrite(pin_enable_line, (digitalRead(pin_enable_line) == HIGH) ? LOW : HIGH); (value is still LOW 0 and HIGH 1) – Juraj Apr 19 at 4:54
4

The digitalRead method doesn't return a Boolean value. It returns an Integer.

From what I remember inside Arduino.h values of LOW and HIGH are defined as being simply Integer 0 and 1.

#define HIGH 0x1
#define LOW 0x0

I think that was changed to an Enum called PinStatus but Enum can be treated as an Integer.

This means that you have to store results of digitalRead inside Integer variable.

int value = digitalRead(8);

It was designed that way to easily cast values between Integer and Boolean. By (very smart) design choice Integer value of 0 (LOW) will be cast to false and every other value (for example 1 or HIGH) will be cast to true.

The operator ! (Boolean NOT) can be used only with Boolean variables so the compiler needs to perform several "implicit casts". When it's smart enough it will assume you are know what you are doing and it will add the cast by itself.

By reading the error I see that the issue is happening when compiler tries to convert Boolean value into Integer. So this is implicitly evaluated correctly as a Boolean value

!digitalRead(pin_enable_line)

But when you try to put it inside a place for PinStatus you get the exception

cannot convert 'bool' to 'PinStatus'

Try adding an "explicit cast" to PinStatus:

digitalWrite(pin_enable_line, (PinStatus)!digitalRead(pin_enable_line));

It should fix the issue. But I suggest splitting the code into several lines and adding a temporary variable. It makes debugging much easier. Trust the code optimizer that's built into the compiler. It will understand what you want to do and actual machine code will be the same.

  • if you write an answer, why you don't try it? the cast to int doesn't compile – Juraj Apr 19 at 10:19
  • @Juraj Strange. Line digitalWrite(9, (int)!digitalRead(8)); works perfectly in Tinkercad. – Filip Franik Apr 19 at 10:21
  • for megaAVR core (Uno WiFi 2) with PinStatus enum? – Juraj Apr 19 at 10:27
  • @Juraj You got me. I was reading wrong source code, and tested it on wrong simulator. Post answer about using ternary operator and I will delete mine. – Filip Franik Apr 19 at 10:36
  • cast to PinStatus could work. it compiles, but I would like to know if it works. but I don't have a board which would use the updated Arduino API. but the enum could be tested on it's own. – Juraj Apr 19 at 10:40

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.