BrendanSimon / micropython_experiments

A place to share my micropython experimental code
24 stars 4 forks source link

Running keypad_timer.py on esp8266 results in error #3

Open mraess opened 4 years ago

mraess commented 4 years ago

Hey, I'm trying to implement a key/button matrix on the esp8266/esp32 development board and came across your implementation. I realized I have to switch out pyb for machine and use time_sleep() for delay. I was also able to upload the main.py file to the esp8266. However, I'm getting the following error(s):

main_test(): start Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 197, in <module> File "<string>", line 175, in main_test File "<string>", line 37, in __init__ File "<string>", line 63, in init File "<string>", line 63, in <listcomp> TypeError: can't convert str to int

I'd really appreciate any help/advice you might have here. Did you make your own button matrix? If yes, do you have a wiring diagram?

Again, any insights would be greatly appreciated!

BrendanSimon commented 4 years ago

Hi. The membrane keypad I was using is very similar to these.

https://www.ebay.com.au/i/133024948873

Not sure about the error. I've never used EPS devices.

Which file are using exactly?

https://github.com/BrendanSimon/micropython_experiments/blob/master/keypad/keypad_timer.py

It might be related to these issues. Looks like the Pin object needs to have pin numbers, instead of strings (str). I'm guessing that's a difference in API between pyb and machine.

https://www.tutorialfor.com/questions-289240.htm https://github.com/micropython/micropython-esp32/issues/33

mraess commented 4 years ago

Hey,

Thanks for the reply here! I was able to implement a much simpler solution, which I got from https://www.raspberrypi.org/forums/viewtopic.php?t=211657, which I re-wrote to work on the esp8266/esp32.

My only problem now is that the code works locally when my MAC is connected to the machine in upyCraft - all keys get recognized by themselves (the only change I made to the wiring is I put in diodes to suppress potential ghosting) - It works even as a 1x1 or 1x2 matrix. However, when the code is in stand-alone mode with only power connected, the script isn't executing like it is when the board is connected.

from machine import Pin
import time

led = Pin(2, Pin.OUT)

matrix = [1, 2, 3, 4]

COL = [5, 12]
ROW = [0, 14]

for j in range(2):
            #machine.Pin(COL[j], machine.Pin.OUT)
            Pin(COL[j], Pin.OUT).value(0)
            print("COL = ", Pin(COL[j]).value())

for i in range(2):
          Pin(ROW[i], Pin.IN)
          print("ROW =", Pin(ROW[i]).value())

while True:
    for j in range(2):

        Pin(COL[j]).value(1)
        for i in range(2):
            if Pin(ROW[i]).value() == 1: 

                print ("col = ",(j+1))
                print ("row = ",(i+1))

                m = ((i * 2) + j)
                print ("postion = ",(m))
                print ('button = ', matrix [m])                
                time.sleep(0.1)

                while Pin(ROW[i]).value() == 1:
                    led.on()
                    time.sleep (0.2)

        led.off()
        Pin(COL[j]).value(0)

Sample output when board is first connected - then in REPL/upyCraft I get the below outputs when I press them.

COL =  0
COL =  0
ROW = 0
ROW = 0

Button 1 pressed

col =  1
row =  1
postion =  0
button =  1

Any ideas are of course welcome. I believe I have to wrap the whole thing in a function and somehow store the key_index or something.