INPUT_PULLUP

Quick question :-

I have come across the following code, and it doesn't seem right to me, according to reference info for pin_Mode() and digital_Write()

pinMode(btn_Up, INPUT);
digitalWrite(btn_Up, INPUT_PULLUP);

Your comments please....

The second line does not make sense but it may work. The usual way to use INPUT_PULLUP is in pinMode().

However if you write a HIGH to a pin set as INPUT it switches on the internal pullup.

…R

Robin2:
The second line does not make sense but it may work. The usual way to use INPUT_PULLUP is in pinMode().

However if you write a HIGH to a pin set as INPUT it switches on the internal pullup.

...R

I don't like "may work".....

And I don't like using "INPUT_PULLUP" as an alias for "HIGH".

Think I'll stick with the traditional "pinMode(btn_UP, INPUT_PULLUP);" - probably compiles just the same, but at least it follows the rules laid down in the reference.

For digitalWrite, if the value parameter is zero, then LOW will be written, and any non-zero value will cause a HIGH to be written.

This is a very poor example of code - under which stone did you find it?

abtrain:
I don’t like “may work”…

I only used “may” because I have not tested it myself.

…R

arduino.h

#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2

Since a non-zero value probably works, you can send any non-zero nonsense to it and it will "work":

    pinMode(btn_Up, INPUT);
    digitalWrite(btn_Up, CHANGE);

You have the option of writing obscure and silly code.

abtrain:
probably compiles just the same

I doubt it.

You really want me to "name & shame" ??

abtrain:
You really want me to "name & shame" ??

Why not?

AWOL:
Why not?

OK, its from here - GitHub - sheaivey/rx5808-pro-diversity: DIY project to create your own 5.8ghz FPV diversity basestation - based off the rx5808 receiver module. Project includes basic Arduino Nano implementation to advanced custom PCB board and introduction to digital switches 4066 chip.

Header suggests many contributors, so can't point a specific finger, lol

I can now see how it does work, (and work it does), since INPUT_PULLUP #defined as 0x02 from arduino.h, so it's non-zero.

Still think it's sloppy ....

I've changed it to the following, which in my opinion is 100% correct and a lot clearer.

// minimum control pins
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonMode, INPUT_PULLUP);

// optional control
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonSave, INPUT_PULLUP);

On the basis of the values #defined in arduino.h, you could do the following, and confuse the hell out of everybody....

pinMode(buttonUp, INPUT);
digitalWrite(buttonMode, OUTPUT);

Using INPUT_PULLUP as a non-zero value is just plain daft when you have HIGH to use, and why do the digitalWrite when you can set the pin mode with pinMode()

I've never seen the need to delve into arduino.h, I just assume it's there, automatically #INCLUDEd, and working fine as it should....

Still think it's sloppy ....

Amen.
If the intention is to turn on the pull-ups, then a write of HIGH and a comment of the intent is required, IMO.

abtrain:
pinMode(btn_Up, INPUT);
digitalWrite(btn_Up, INPUT_PULLUP);

I belive that's very old code - the way you used to change an INPUT pin from floating to pullup was to write HIGH to it?

PaulMurrayCbr:
I belive that's very old code - the way you used to change an INPUT pin from floating to pullup was to write HIGH to it?

That's most likely the best explanation yet as to why it looks strange... Presumably the Arduino is in constant development, so it's reasonable to expect to see odd things from days gone....

Still think it's weird to use INPUT_PULLUP instead of HIGH, but the programmer might like to do it to remind him why he's writing to an input pin. Could be he was just lazy and didn't want to comment what the code was doing.

pinMode can take 3 values for the second parameter. So what do you suggest for the 3rd one?

And INPUT_PULLUP makes absolutely clear what it does, HIGH does not.

sterretje:
pinMode can take 3 values for the second parameter. So what do you suggest for the 3rd one?

And INPUT_PULLUP makes absolutely clear what it does, HIGH does not.

It is only clear if you have been told, or dived into arduino.h (and who needs to), that LOW = 0x00, HIGH = 0x01, and INPUT_PULLUP = 0x02.

So the digitalWrite() is just writing a non-zero value to a digital pin, which compiles as TRUE or HIGH

This whole thread would not have been started if he had put :-

digitalWrite(btn_up, HIGH); // enable input pull-up

that would have made sense, firstly using HIGH, and then adding a comment what the code is achieving.

I prefer explicit to implicit