Arduino Uno WiFi Rev 2 Using Keypad Library

Just got the Arduino Uno WiFi Rev 2, need to use a keypad (model is HC543 3x4 Keypad) with it, though the library is not working with the board. Keep getting the following errors;

Arduino: 1.8.8 (Windows 7), Board: "Arduino Uno WiFi Rev2, ATMEGA328"

In file included from C:\Users\Netlab\Documents\Arduino\sketch_dec10c\sketch_dec10c.ino:1:0:

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:48:2: warning: #warning "Using pinMode() INPUT_PULLUP AVR emulation" [-Wcpp]

#warning "Using pinMode() INPUT_PULLUP AVR emulation"

^

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h: In member function 'virtual void Keypad::pin_mode(byte, byte)':

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:55:23: warning: invalid conversion from 'int' to 'PinStatus' [-fpermissive]

digitalWrite(_pin, 1); \

^

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:50:30: note: in expansion of macro '_mypinMode'

#define pinMode(_pin, _mode) _mypinMode(_pin, _mode)

^

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:87:50: note: in expansion of macro 'pinMode'

virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }

^

In file included from C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/api/ArduinoAPI.h:52:0,

from C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/Arduino.h:23,

from sketch\sketch_dec10c.ino.cpp:1:

C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/api/Common.h:104:6: note: initializing argument 2 of 'void digitalWrite(pin_size_t, PinStatus)'

void digitalWrite(pin_size_t pinNumber, PinStatus status);

^

In file included from C:\Users\Netlab\Documents\Arduino\sketch_dec10c\sketch_dec10c.ino:1:0:

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:57:22: warning: invalid conversion from 'byte {aka unsigned char}' to 'PinMode' [-fpermissive]

pinMode(_pin, _mode); \

^

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:50:30: note: in expansion of macro '_mypinMode'

#define pinMode(_pin, _mode) _mypinMode(_pin, _mode)

^

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:87:50: note: in expansion of macro 'pinMode'

virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }

^

In file included from C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/api/ArduinoAPI.h:52:0,

from C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/Arduino.h:23,

from sketch\sketch_dec10c.ino.cpp:1:

C:\Users\Netlab\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.6.24\cores\arduino/api/Common.h:103:6: note: initializing argument 2 of 'void pinMode(pin_size_t, PinMode)'

void pinMode(pin_size_t pinNumber, PinMode pinMode);

^

In file included from C:\Users\Netlab\Documents\Arduino\sketch_dec10c\sketch_dec10c.ino:1:0:

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h: In member function 'virtual void Keypad::pin_write(byte, boolean)':

C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h:88:81: error: cannot convert 'boolean {aka bool}' to 'PinStatus' for argument '2' to 'void digitalWrite(pin_size_t, PinStatus)'

virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }

^

exit status 1
Error compiling for board Arduino Uno WiFi Rev2.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

The code we are using for this is as follows;

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey();

if (customKey){
Serial.println(customKey);
}
}

Any help would be great, thank you

though the library is not working with the board

You mean that the library is not written for the board you are using.

Post a link to where you got the library from. Please use the chain links icon on the forum toolbar to make it clickable. Or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

PaulS:
You mean that the library is not written for the board you are using.

What would the library be for this board then? if there is one...

The Arduino Uno WiFi Rev 2 uses a very new microcontroller that has never been used by the Arduino community before. For this reason, you will find that some libraries and sketches which have architecture-specific code will need some modifications before they'll work. That's always going to be the way with the shiny new boards. Support is great for the AVR boards because they've been in use for a decade. However, in this case I suspect it is a very minor issue that can easily be fixed. To know, I need to look at the library you're using. There are many different versions of this library floating around and I don't want to waste time looking for the right one and maybe looking at completely different code from what you're using. So if you want my help, you need to do what I asked in my last reply.

pert:
The Arduino Uno WiFi Rev 2 uses a very new microcontroller that has never been used by the Arduino community before. For this reason, you will find that some libraries and sketches which have architecture-specific code will need some modifications before they'll work. That's always going to be the way with the shiny new boards. Support is great for the AVR boards because they've been in use for a decade. However, in this case I suspect it is a very minor issue that can easily be fixed. To know, I need to look at the library you're using. There are many different versions of this library floating around and I don't want to waste time looking for the right one and maybe looking at completely different code from what you're using. So if you want my help, you need to do what I asked in my last reply.

Thank you for the information you gave there, it has helped our understanding alot. And apologies for not replying so soon, the library we are using is the following -
https://playground.arduino.cc/code/keypad

It is the most current version we are using. Hope this helps

jamie98:
It is the most current version we are using. Hope this helps

From the output, I don't think you are. The most current version I found is 3.1.1, installed from the Library Manager and it has a different folder structure. So I'm looking at different code than you are using, which makes this unnecessarily complicated. However, the same errors occur with the version and I doubt there are significant differences so the fix should be about the same:

Open C:\Users\Netlab\Documents\Arduino\libraries\Keypad/Keypad.h in a text editor

Change these lines:

virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
 virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }

to:

#if defined(ARDUINO_ARCH_MEGAAVR)
 virtual void pin_mode(byte pinNum, PinMode mode) { pinMode(pinNum, mode); }
 virtual void pin_write(byte pinNum, PinStatus level) { digitalWrite(pinNum, level); }
#else // defined(ARDUINO_ARCH_MEGAAVR)
 virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
 virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }
#endif // defined(ARDUINO_ARCH_MEGAAVR)

Save the file.

After that change, the code does compile. I don't own an Uno WiFi Rev2 so I can't say whether it will work, but I don't see anything obvious that would make it not work. There are still some warnings about some other things in the library that are not ideal, but I don't think they'll actually break anything.

Here's an explanation of the cause of the error. The library uses the byte type for the pin mode parameter and the boolean type for the pin state parameter. In the Arduino AVR Boards core library used by the Uno, Mega, Leonardo, etc. boards, these are defined like this:
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Arduino.h#L40-L45

#define HIGH 0x1
#define LOW  0x0

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

so that works fine. But in the Arduino megaAVR Boards core library used by the Uno WiFi Rev2, they are defined like this:

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

typedef enum {
  INPUT           = 0x0,
  OUTPUT          = 0x1,
  INPUT_PULLUP    = 0x2,
  INPUT_PULLDOWN  = 0x3,
} PinMode;

So the pin mode is of the PinMode type and the pin state is of the PinStatus type.

This is not an architecture-specific issue. The idea behind the ArduinoCore-API project (code named "Project Chainsaw") is that there is currently a lot of non architecture-specific code duplicated in the core library of each hardware package. This is very inefficient to maintain and makes it more work than necessary for people to create 3rd party hardware packages for different architectures. So the plan is to move all that code into its own repository. The Arduino megaAVR Boards package is the first place this is being tried out. So in the near future the same error you're having using this library with the Arduino WiFi Rev2 will occur even when it's used with the AVR boards that it's worked fine with for the last decade. This makes me wonder how much other code this change could break. I do think it's probably better to use an enum for this purpose rather than macros, but maybe we've gone too far down the macro path to turn back now? I'd be interested to hear if anyone has any thoughts on this.

wow... i'm amazed, thank you so much, never would have thought of that. works perfectly now, thank you for explaining it too rather than just giving the solution. :slight_smile:

You're welcome. I'm glad to hear it's working now. Enjoy!
Per