Digital Pin as Parameter

Hi,

I'm not sure this will work, so if anyone can help...
In my code there's the following function:

void updateBCDOutput(boolean bcdAValue, boolean bcdBValue, boolean bcdCValue, boolean bcdDValue) {
digitalWrite(A0, bcdAValue);
digitalWrite(A1, bcdBValue);
digitalWrite(A2, bcdCValue);
digitalWrite(A3, bcdDValue);

Pins A0, A1, A2 and A3 are previously initialized:

// Pin mode setup for BCD output.
void BCDPinsInitialize() {
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);

digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
}

I would like to call the updateBCDOutput and pass the pin state. Example:

updateBCDOutput(HIGH, LOW, HIGH, LOW);

I have checked the Arduino documentation and HIGH and LOW and defined as constants:

By defining void updateBCDOutput(boolean bcdAValue, boolean bcdBValue, boolean bcdCValue, boolean bcdDValue) I am afraid that I'm casting HIGH and LOW to boolean values (true and false) and that this will not work on a digitalWrite().
Nevertheless, it should be possible to pass HIGH and LOW as parameters to a function.
What would be the correct way of doing so?
Thank you!

Have you tried it?
Did it work?

If it did not work why not define the parameters as byte rather than boolean?

...R

It's a fair question. I do not own an Arduino (it's ordered and on it's way so that I can do my own tests).
I wrote this code to a friend and he told me that probably (he tested but he is not sure) there's a problem with this function.
But thank you for the suggestion. I'll try byte, have my friend test and report back.
Again, thanks!

Nevertheless, it should be possible to pass HIGH and LOW as parameters to a function.
What would be the correct way of doing so?

The correct way would be to use a byte sized variable. This would normally be declared as a byte but at a push you could get away with a boolean but why would you want to ?

As an experiment try

void setup()
{
  Serial.begin(115200);
  Serial.print("true\t");
  Serial.println(true);
  Serial.print("false\t");
  Serial.println(false);
  Serial.print("HIGH\t");
  Serial.println(HIGH);
  Serial.print("LOW\t");
  Serial.println(LOW);
}

void loop()
{
}

Robin2:
Have you tried it?
Did it work?

If it did not work why not define the parameters as byte rather than boolean?

...R

If you are using IDE version 1.5.9 or below, boolean is a byte!

In 1.6.0 and greater, it is a real bool. However either type will work identically as integral promotion rules mandate a boolean value be promoted to an integer of either 0 or 1. E.g:

** bool b = 5;**
** Serial.print( b ); //Prints 1**

Hi, I'm using IDE arduino-1.6.0.

I found this on file

D:\arduino-1.6.0\hardware\arduino\sam\cores\arduino\wiring_constants.h

#define HIGH 0x1
#define LOW 0x0

Putting '0x' in front of the number allows to enter Hexadecimal numbers into the source code e.g. 0x1 = 1.
So, the correct type is, for sure, byte?

Not sure yet.

My best bet is int as the link above states:

Normally, integer constants are treated as base 10 (decimal) integers, but special notation (formatters) may be used to enter numbers in other bases.

Base Example Formatter Comment
16 (hexadecimal) 0x7B leading "0x" characters 0-9, A-F, a-f valid

I will try int (not byte) and report back.
Thank you all that replied.

jmcl:
I found this on file

D:\arduino-1.6.0\hardware\arduino\sam\cores\arduino\wiring_constants.h

#define HIGH 0x1
#define LOW 0x0

Putting '0x' in front of the number allows to enter Hexadecimal numbers into the source code e.g. 0x1 = 1.
So, the correct type is, for sure, byte?

HIGH and LOW are literals which will implicitly cast to the type of whatever expression its used in.
The type is irrelevant, all comparisons are done using arithmetic and a bool is always promoted to 0 or 1, which is the values of HIGH and LOW.

The "correct" type is whatever digitalWrite() takes as its second argument:

void digitalWrite(uint8_t pin, uint8_t val);

So the "correct" type is "uint8_t" (a.k.a. "unsigned char").

Having said that, pretty much any integer type (boolean, char, short, int, unsigned, long) will work. The digitalWrite() function looks for "val == LOW" which pretty much reduces any integer to a boolean: false if value == 0, otherwise true.