SOLVED: Getting Error:- 'INPUT_PULLUP' was not declared in this scope

tack:
The boards.txt is already set to standard, as per above.

[quote author=Nick Gammon link=topic=142041.msg1066936#msg1066936 date=1357891265]
The pins default to input, so if you just digitalWrite (pin, HIGH) that will have the same effect.

This is an error being received with a library, so it's not code in my sketch. I guess I could go and modify all the INPUT_PULLUP uses in the library to PinMode(pin, INPUT) and digitalWrite(pin, HIGH) but I was hoping there was a more elegant solution, such as updating the mighty1284P core files so I don't have to change the library every time an update is released.
[/quote]

What if you just do this:

#ifndef INPUT_PULLUP
#define INPUT_PULLUP 2
#endif

?????

Does your 1284 variant use its own cores directory: "/Java/hardware/arduino/cores/"?

in /Java/hardware/arduino/cores/arduino/wiring_digital.c
there is an if-condition for INPUT_PULLUP (which turns on the internal pull-ups)

while INPUT_PULLUP is #defined in Arduino.h.

If your variant has its own "cores" directory, you may need to add the code from wiring.digital.c's pinMode and the #define from Arduino.h.

This was added in 1.01 or 1.02.

Hi tack,

tack:
This is an error being received with a library, so it's not code in my sketch. I guess I could go and modify all the INPUT_PULLUP uses in the library to PinMode(pin, INPUT) and digitalWrite(pin, HIGH) but I was hoping there was a more elegant solution, such as updating the mighty1284P core files so I don't have to change the library every time an update is released.

If you don't mind running a quick test for me...

Change line 46 in keypad.h from:

#if defined(ARDUINO) && ARDUINO < 101

to:

#if !defined INPUT_PULLUP

And see if it works for you. If so then I will update the library so you won't have to mess around with it.

-Mark Stanley

Mark, I'll give that a try for you shortly.

What I had done as a quick fix was alter the arduino.h file, in my 1284P cores folder, to add #define INPUT_PULLUP 0x02

I'm guessing it was dirty though as there is probably some code in another file (like suggested with wiring.h) where INPUT_PULLUP is mapped to the two normal pinMode and digitalWrite operations.

I'll remove the line and try your fix and report back. If it works then it would fix it for all permutations, whether the 1284P cores get fully updated or not. It'll either be fixed in the cores and your fix doesn't take effect, or your fix covers it if no INPUT_PULLUP defined.

I'll report back shortly.

Many thanks for taking the time to respond and PM me that you had done so.

OK, this certainly seems to fix the error.

As explained in my response to your PM, I had applied a fix in my 1284P arduino.h file.

I removed that and tried compiling and got the original error.

I then added the keypad.h fix like this

//#if defined(ARDUINO) && ARDUINO < 101
#ifndef INPUT_PULLUP // fix for MCU with custom bootloaders/cores that don't have the update, like 1284P
#define INPUT_PULLUP INPUT
#endif

This now compiles fine with the following custom MCU entries:-

Mighty1284P with Optiboot, using maniacbug, bobuino and avrdude variants
Custom 644P with Optiboot, using bobuino variant

It compile OK, but it doesn't do the same thing. All you have accomplished there is to make INPUT_PULLUP equivalent to INPUT. The _PULLUP extension is lost.

I then added the keypad.h fix like this

It masks the problem, not solving the problem.

I would change the code: whenever it uses INPUT_PULLUP, explicitly writes a '1' to the pin.

PaulS:
It compile OK, but it doesn't do the same thing. All you have accomplished there is to make INPUT_PULLUP equivalent to INPUT. The _PULLUP extension is lost.

Ah, I understand.

So all it does is change a written

pinMode(pin, INPUT_PULLUP);

to

pinMode(pin, INPUT);

But the internal pullup isn't enabled.

OK, so how about an actual fix to the cores?

In arduino.h I do

#define INPUT_PULLUP 0x20

and in wiring_digital.c under the pinMode(uint8_t pin, uint8_t mode) function I change

	if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		SREG = oldSREG;
	} else {
		uint8_t oldSREG = SREG;
                cli();
		*reg |= bit;
		SREG = oldSREG;
	}

To

        if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out &= ~bit;
		SREG = oldSREG;
	} else if (mode == INPUT_PULLUP) {
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out |= bit;
		SREG = oldSREG;
	} else {
		uint8_t oldSREG = SREG;
                cli();
		*reg |= bit;
		SREG = oldSREG;
	}

The only difference I can see is the reference to *out pointer, so I'm not 100% sure if that is a new variable somewhere else?

TBH, do I need to do anything unless I actually add code, or use libraries that make use of the INPUT_PULLUP option.

As far as I can see, keypad.h doesn't do that as it already had that masking of the issue for pre 1.0.

OR, are there other 'if defined(ARDUINO) && ARDUINO < 101' that make use of INPUT_PULLUP when the IDE version is 1.0 or above?

Somewhere outside of those snippets, out must be declared. The changes in those snippets look a lot more useful than redefining INPUT_PULLUP to be equivalent to INPUT.

TBH, do I need to do anything unless I actually add code, or use libraries that make use of the INPUT_PULLUP option.

No, you don't. I thought you were doing this because you did need INPUT_PULLUP supported.

Well I solved the problem when I upgraded to arduino IDE 1.0.3 by just copying the two files Arduino.h and wiring_digital.c from the 1.0.3 core folder to replace the ones in my custom hardware core folder in my 644P add-on user hardware folder. Seems to work fine. By the way the IDE 1.0 had not yet added the INPUT_PULLUP option.

Lefty

Your fix will work.

       if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out &= ~bit;

I would comment out the last line. pinMode() should really be operating on just the DDRx.

PaulS:

TBH, do I need to do anything unless I actually add code, or use libraries that make use of the INPUT_PULLUP option.

No, you don't. I thought you were doing this because you did need INPUT_PULLUP supported.

No, it was because the sketch that used keypad.h wouldn't compile when I changed my board from Uno to 1284P.

The library itself was looking at Arduino version and expecting define of INPUT_PULLUP for IDE versions over 1.0.

I'm not using INPUT_PULLUP myself at all.

lefty, I might just try what you did and copy the cores across for my 1284P and 644P entries. I'll back up what I have first.

"The only difference" is the line that turns on the internal pull-up.

I'm not using INPUT_PULLUP myself at all.

In that case, define it as "INPUT".

tack:

PaulS:

TBH, do I need to do anything unless I actually add code, or use libraries that make use of the INPUT_PULLUP option.

No, you don't. I thought you were doing this because you did need INPUT_PULLUP supported.

No, it was because the sketch that used keypad.h wouldn't compile when I changed my board from Uno to 1284P.

The library itself was looking at Arduino version and expecting define of INPUT_PULLUP for IDE versions over 1.0.

I'm not using INPUT_PULLUP myself at all.

lefty, I might just try what you did and copy the cores across for my 1284P and 644P entries. I'll back up what I have first.

I wasn't brave enough to copy the complete core files, just the two I mentioned as I think that is the only two files that are involved with the INPUT_PULLUP addition.

LEfty

tack:
No, it was because the sketch that used keypad.h wouldn't compile when I changed my board from Uno to 1284P.

The library itself was looking at Arduino version and expecting define of INPUT_PULLUP for IDE versions over 1.0.

I'm not using INPUT_PULLUP myself at all.

lefty, I might just try what you did and copy the cores across for my 1284P and 644P entries. I'll back up what I have first.

I thought INPUT_PULLUP == 0x02, not 0x20????

dhenry:
It masks the problem, not solving the problem.

Actually, it should be a resonable fix in the case of the keypad library. The offending lines in Keypad.cpp are:

// When sharing row pins with other hardware they may need to be re-intialized.
for (byte r=0; r<sizeKpd.rows; r++) {
	pin_mode(rowPins[r],INPUT_PULLUP);
	pin_write(rowPins[r],HIGH);	// Enable internal pullup resistors for Arduino<101
}

I did this to maintain some sort of backwards compatibility but my error was in deciding to test for Arduino versions instead of the existence of INPUT_PULLUP.

// See http://code.google.com/p/arduino/issues/detail?id=246
#if defined(ARDUINO) && ARDUINO < 101
#define INPUT_PULLUP INPUT
#endif

Becomes...

// See http://code.google.com/p/arduino/issues/detail?id=246
#if !defined INPUT_PULLUP
#define INPUT_PULLUP INPUT
#endif

My suggestion would be to not depend on it and fix the code not to depend on using INPUT_PULLUP
or even use it all since it is only available on such a recent Arduino release and then only if
the core supports it.
There are many other cores, libraries, and IDEs for other processors (maple, chipkit) that
may not have this recent addition.
Also many linux environments, Debian Squeeze, Ubuntu 12.04, Linux Mint 13/14 all have
Arduino 1.0 in their repositories, so they will not have this support if using the Arduino IDE
in the repository.

Assuming you are using this code: http://arduino.cc/playground/uploads/Code/keypad.zip
It looks like it already checks for the arduino revision and maps INPUT_PULLUP to INPUT
on older arduino versions. However, the problem with that is that while the Arduino IDE version may be 103
not all the cores being used have all the support in 103.
This is a great example of one of the many flaws in the Arduino build methodology.
The Arduino version reflects the version of the IDE and not really the version of the underlying core
being used.
Libraries often need to know more than just the IDE revision.
This problem is only going to get worse as other processor are introduced and the cores
start to differ in revision and functionality.

What is odd is that keypad code is already is setting the pullups by writing to the port after setting the port to input mode.
It seems crazy to be using the INPUT_PULLUP mode at all since it just adding to the confusion and is not needed.

I'd go in and rip out the all the INPUT_PULLUP stuff since it simply is not needed.


The saddest part of all this was that many people including the Arduino team fought adding this mode in literally for years.
(Teensy had it but it was rejected for main line arduino)

And while the Arduino team added stuff that literally broke 100% of all the existing libraries when going
from the beta release candidate to the 1.0 release, this useful simple addition still wasn't accepted and didn't make
it in to 1.0

The crazy part now is that because this is such a new feature that wasn't in 1.0,
library writers really can't depend on it being there and the other non AVR parts now have to have special code to emulate
the AVR way of turning on pullups by looking for writes to a port that is input mode to support all the existing
code that turns on pullups without using INPUT_PULLUP.
This slows things down for the non AVR processors.

--- bill