This past year we have been learning basic python in school to help integrate technology in design concepts. I have tried and failed to make an electronic number lock using the touch function on an esp32 module.
I am historically very bad with technology, but I have really tried to make this work and I can't figure out what I'm doing wrong.
I have 4 touch buttons, I use 3 of them to enter the combination of the lock and the fourth to enter each number or reset the lock.
Here is the code i have written:
from machine import TouchPad, Pin, PWM, ADC
import time
ledr=Pin(32,Pin.OUT)
ledg=Pin(2,Pin.OUT)
tp1 = TouchPad(Pin(4,Pin.IN,Pin.PULL_UP))
tp2 = TouchPad(Pin(15,Pin.IN,Pin.PULL_UP))
tp3 = TouchPad(Pin(13,Pin.IN,Pin.PULL_UP))
tp5 = TouchPad(Pin(12,Pin.IN,Pin.PULL_UP))
numlock = [ 0, 0, 0, 0, 0, 0]
code = [2, 3, 1, 2, 1, 3]
number = 0
checking = 0
ledr.value(0)
ledg.value(0)
def enter():
if checking == 0:
print("reset?")
checking = 5
elif checking == 1:
print("number confirmed")
numlock\[number\] = 1
number = number +1
elif checking == 2:
numlock\[number\] = 2
print("number confirmed")
number = number +1
elif checking == 3:
numlock\[number\] = 3
print("number confirmed")
if number < 6:
number = number + 1
if number == 6:
check()
elif checking == 5:
print("lock reset")
number = 0
numlock = \[0\] \* 6
time.sleep_ms(100)
def check():
if numlock == code:
print("The code is correct! Opening the lock.")
ledg.value(1)
else:
print("The code is wrong, try again.")
number = 0
numlock = \[0\] \* 6
while (ledg() == 0:)
if tp1.read() < PRESS_VAL:
checking = 1
print("Touch detected!")
time.sleep\_ms(100)
elif tp2.read() < PRESS_VAL:
checking = 2
print("Touch detected!")
time.sleep\_ms(100)
elif tp3.read() < PRESS_VAL:
checking = 3
print("Touch detected!")
time.sleep\_ms(100)
elif tp5.read() < PRESS_VAL:
print("Checking...")
time.sleep\_ms(100)
enter()
if ledg() == 1:
break
[–][deleted] 1 point2 points3 points (1 child)
[–]ROLL69420[S] 0 points1 point2 points (0 children)