B4R Library Keypad

SS-2016-10-30_11.52.39.jpg


Simple library that monitors keypad key presses.

It is based on the Arduino Keypad library: http://playground.arduino.cc/Code/Keypad

You need to initialize the Keypad with the characters, row pins and column pins. Note that if you are not sure about the order then just connect the pins and try it. Change the pins order in the Initialize method based on the keys that are raised.

Example that waits for a password to be entered:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private pad As Keypad
   Private password As String = "123456" 'the password is *123456
   Private passwordBuffer(6) As Byte
   Private passwordIndex As Int
   Private bc As ByteConverter
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   pad.Initialize("123A456B789C*0#D", Array As Byte(9, 8, 7, 6),  Array As Byte(5, 4, 3, 2), _
     "pad_KeyPressed")
End Sub

Sub Pad_KeyPressed (Key As String)
   If Key = "*" Then
     passwordIndex = 0
     'this is not really required, but it makes the logs nicer.
     bc.ArrayCopy("000000", passwordBuffer)
   Else if passwordIndex < passwordBuffer.Length Then
     'put the key in the buffer.
     passwordBuffer(passwordIndex) = Asc(Key)
     passwordIndex = passwordIndex + 1
     If passwordIndex = passwordBuffer.Length Then
       If passwordBuffer = password Then
         Log("Well done!!!")
       End If
     End If
   End If
   Log(passwordBuffer)
End Sub
 

Attachments

  • rKeypad.zip
    9.3 KB · Views: 802

Mostez

Well-Known Member
Licensed User
Longtime User
how can I use this library with 3x4 keypad, just remove last element from array?
 

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I tried your example with 4x4 keypad, but I've got no response at all, except this message at start up, I tried also to change pin assignment without any success!

AppStart
2
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Same as @Mostez on B4Rv1.20 - B4R1.50 with old Arduino IDE

Works ok updating Arduino IDE to 1.6.11 as stated on B4R page docs
 

vali khandangoll

Active Member
hi all of you.
My B4R compiler read BC variable with error like attached picture.
my you help me ?
 

Attachments

  • key error.jpg
    key error.jpg
    197.3 KB · Views: 458

pappicio

Active Member
Licensed User
Longtime User
Hi all, I'd like to use this library, tested with nodemcu and wemos d1 mini (both esp8266) never worked, sems that no key was pressed, tried a lot of pin combination for rows and cols, not ork...
 

Cableguy

Expert
Licensed User
Longtime User
pad.Initialize("123A456B789C*0#D", Array As Byte(9, 8, 7, 6), Array As Byte(5, 4, 3, 2),
Almost 100% sure it's a pin choice issue.
Take note that the 9, 8, 7, 6 are NOT pin numbers, but gpio pin numbers.
What does this mean? Well, do a Google search for your board pinout. You will find images of the board with the relevant info along side each pin. If your board doesn't have a gpio7 or any other, simply replace that declaration by one of the available pins on your board.
If that doesn't work, post an image of the board you are using, so that we can suggest pins for you to use.
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Found this for Wemos D1 Mini:

1705261874970.png
1705261955611.png


Which shows us that GPIO2 to GPIO5 do exist but not GPIO6 to GPIO9. A suitable alternative pin declaration would be GPIO12 to GPIO15
 

pappicio

Active Member
Licensed User
Longtime User
Almost 100% sure it's a pin choice issue.
Take note that the 9, 8, 7, 6 are NOT pin numbers, but gpio pin numbers.
What does this mean? Well, do a Google search for your board pinout. You will find images of the board with the relevant info along side each pin. If your board doesn't have a gpio7 or any other, simply replace that declaration by one of the available pins on your board.
If that doesn't work, post an image of the board you are using, so that we can suggest pins for you to use.
On wemos d1 mini I've tried with pins GPIO, not D1, D2 etc...
 
Last edited:

pappicio

Active Member
Licensed User
Longtime User
thanks for reply, I've tested both with nodemcu and d1 mini, with pins in red boxes, and shure, I've declared gpio id and not pin name (D1, D2 etc, but gpioX...) never worked, as no button press, tried also inverting all pins (from rows to cols and viceversa), tested with keypad as in 1st post, and also with different keypad that has row/col/ros/col/row/row/col pinout, nothing to do....
particulary for d1 mini, I've followed an arduino code exaples that used D1,D2,D3,D4/D5,D6,D7 and D8, plug that, with dupont wires and declaring on this library the corresponding pis (GPIOX), ever inverting also, nothing.
 

Attachments

  • d1mini.png
    d1mini.png
    260.2 KB · Views: 25
  • nodemcu.png
    nodemcu.png
    177.9 KB · Views: 22

Cableguy

Expert
Licensed User
Longtime User
I don't have a num pad to test.... Can you post your last code version? I have a wemos Mini and I think I can simulate the key pad....

I can't seem to simulate any key press, I guess the keypad uses an array of resistors, and I don't have any at hand.
However, when pressing the Wemos Reset button, I get the AppStart log and also a keyPressed event log (I added the log statement to the sub), which is weird...
Maybe this keypad in particular isn't compatible with ESP8266 or there's a lib issue...
Anyway, If you can mesure the resistance between connections in the keypad, me or some other dev may be able to re-invent this well and turn it into a code module.
 
Last edited:

pappicio

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private pad As Keypad
    Private password As String = "123456" 'the password is *123456
    Private passwordBuffer(6) As Byte
    Private passwordIndex As Int
    Private bc As ByteConverter
End Sub

Private Sub AppStart

    Serial1.Initialize(115200)
    Log("AppStart")
 
    'matrix keypad is a 3X4 , not 4X4, but I think is same, assuming has only 3 cols and 4 rows.
    pad.Initialize("123456789*0#", Array As Byte(14,12,13),  Array As Byte(5, 4, 0, 2), "pad_KeyPressed")
   '''pad.Initialize("123456789*0#", Array As Byte(14,12,13),  Array As Byte(1,3,5,4), "pad_KeyPressed")
 

    'this is only a combination, I've tried a lot of them...

End Sub

Sub Pad_KeyPressed (Key As String)
    log(key)
    If Key = "*" Then
        passwordIndex = 0
        'this is not really required, but it makes the logs nicer.
        bc.ArrayCopy("000000", passwordBuffer)
    Else if passwordIndex < passwordBuffer.Length Then
        'put the key in the buffer.
        passwordBuffer(passwordIndex) = Asc(Key)
        passwordIndex = passwordIndex + 1
        If passwordIndex = passwordBuffer.Length Then
            If passwordBuffer = password Then
                Log("Well done!!!")
            End If
        End If
    End If
    Log(passwordBuffer)
End Sub

this is one of pins variants I've tried with no success, on D1 mini!

and, yes, you can simulate keypad with 2/3 dupont wires, connecting a "row" with a "col" with one of them to simulate "key pressed"
 
Last edited:
Top