Keypad help please

If doesn't fit into any other category ....
Post Reply
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Keypad help please

Post by Zim »

Hi
I am trying to build a keyless entry using a keypad/resistors. Can't find any examples with Annex. Can anyone translate this C++ to Annex for me?

Thanks
Zim
2024-04-30 13_13_07-HariFun #143 - How to read a 4x4 keypad using just one Arduino pin! - YouTube — .jpg
You do not have the required permissions to view the files attached to this post.
User avatar
cicciocb
Site Admin
Posts: 2080
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 445 times
Been thanked: 1370 times
Contact:

Re: Keypad help please

Post by cicciocb »

this should be a "raw" conversion of the code

Code: [Local Link Removed for Guests]

dim thresholds(16) = 2, 77, 144, 202, 244, 290, 331, 368, 394, 424, 452, 488, 496, 518, 556
dim keypad$(16) = "1", "2", "3", "4", "5", "6", "B", "7", "8", "9", "C", "*", "0", "#", "D"

adc_pin = 39
do
  value = adc(adc_pin )
  for i = 0 to 15
    if abs(value - thresholds(i)) < 5 then
      print keypad$(i)
      while adc(adc_pin) < 1000
        pause 100
      wend
    end if
  next i
loop
However, you must consider the resolution of the ADC that is 12 bits vs 10 bits of the Arduino so the table must be changed (with a raw approximation multiplied by 4).

Anyway, I don't think is a good idea as the ADC converter of the ESP32 is not very stable and the resistor values could change with the temperature so I suggest to use a classic scan technique.
You can find a good inspiration from a demo project I did some time ago on WOKWI

https://wokwi.com/projects/343447488070419027
image.png
You do not have the required permissions to view the files attached to this post.
bugs
Posts: 144
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 47 times
Been thanked: 55 times

Re: Keypad help please

Post by bugs »

Also, the arduino design used 5 volts so with 3.3 volts it is likely all the thresholds will need changing and the "width" of each will be only 2/3 of the original.
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Re: Keypad help please

Post by Zim »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Wed May 01, 2024 8:11 am this should be a "raw" conversion of the code

Code: [Local Link Removed for Guests]

dim thresholds(16) = 2, 77, 144, 202, 244, 290, 331, 368, 394, 424, 452, 488, 496, 518, 556
dim keypad$(16) = "1", "2", "3", "4", "5", "6", "B", "7", "8", "9", "C", "*", "0", "#", "D"

adc_pin = 39
do
  value = adc(adc_pin )
  for i = 0 to 15
    if abs(value - thresholds(i)) < 5 then
      print keypad$(i)
      while adc(adc_pin) < 1000
        pause 100
      wend
    end if
  next i
loop
However, you must consider the resolution of the ADC that is 12 bits vs 10 bits of the Arduino so the table must be changed (with a raw approximation multiplied by 4).

Anyway, I don't think is a good idea as the ADC converter of the ESP32 is not very stable and the resistor values could change with the temperature so I suggest to use a classic scan technique.
You can find a good inspiration from a demo project I did some time ago on WOKWI
Thanks a ton for the interpretation and the suggestions! Exactly what I needed!
Zim
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Re: Keypad help please

Post by Zim »

cicciocb
I hate to be a pest....could you modify this for a 4x3 keypad with one less column?
I changed "dim cols(4) = 35, 34, 39, 36" to "dim cols(3) = 35, 34, 39"
dim sym$(16).... to dim sym$(12)......

seems to work except the last column
it gives a error on line 59
any ideas?
Thanks
Zim

Code: [Local Link Removed for Guests]

' demo use of the keypad scan
' a complete help of Annex32 can be found here
' https://cicciocb.com/annex32help/V1.442/
dim rows(4) = 26, 25, 33, 32
dim cols(3) = 35, 34, 39
'dim sym$(16) = "1", "2", "3", "A", "4", "5", "6", "B", "7", "8", "9", "C", "*", "0", "#", "D"

dim sym$(12) = "1", "2", "3", "4", "5", "6", "7", "8", "9",  "*", "0", "#"      'added this???????????????


' set the pins as input and output
for z = 0 to 3
  pin.mode rows(z), output
    pin(rows(z)) = 0
next z  

for z = 0 to 2									'added this ??????????????????
pin.mode cols(y), input, pulldown					'added this ?????????????????
next y





'maxscroll.setup 8, 21
'maxscroll.print "Press Keys"
'maxscroll.show 63, 5
k$ = ""
Kp$ = ""
line$ = space$(16)
do
  scan k$
  if (k$ <> kp$) and (k$ <> "") then
    print k$
    line$ = right$(line$ + k$, 10)
   ' maxscroll.text line$
    'maxscroll.show 64, 5
  end if
  kp$ = k$
  'pause 100

loop

sub scan(key$)
local r, c, l, code
code = -1
for r = 0 to 3
  pin(rows(r)) = 1
  for c = 0 to 3
    l = pin(cols(c))
    if (l=1) then
      code = r*4 + c
      exit for
    end if
  next c
  pin(rows(r)) = 0
  if (code <> -1) then exit for
next r
if (code <> -1) then key$ = sym$(code) else key$ = ""
end sub
User avatar
cicciocb
Site Admin
Posts: 2080
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 445 times
Been thanked: 1370 times
Contact:

Re: Keypad help please

Post by cicciocb »

You must change the line 49

from

Code: [Local Link Removed for Guests]

  for c = 0 to 3
to

Code: [Local Link Removed for Guests]

  for c = 0 to 2
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Re: Keypad help please

Post by Zim »

Thank you so much for your quick reply!!

Zim
Post Reply