JustASchKid
Posts: 19
Joined: Thu Jun 04, 2015 12:56 am

Keypad Password

Wed Jul 29, 2015 6:31 am

I had got a keypad hooked up with MCP3008
And below are the codes i use:

Code: Select all

#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from gpiospiadc import *

GPIO.setmode(GPIO.BOARD)
DEBUG = 1

SPICLK = 23
SPIMISO = 21
SPIMOSI = 19
SPICS = 24

GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)

GPIO.setup(11, GPIO.OUT)     #GP0/OP1
GPIO.setup(12, GPIO.OUT)     #GP1/OP2
GPIO.setup(13, GPIO.OUT)     #GP2/OP3
GPIO.setup(15, GPIO.OUT)     #GP3/OP4
GPIO.setup(16, GPIO.OUT)     #GP4/OP5
GPIO.setup(18, GPIO.OUT)     #GP5/OP6
GPIO.setup(22, GPIO.OUT)     #GP6/OP7
GPIO.setup(7, GPIO.OUT)      #GP7/OP8




while True:
        sensor_adc = 3
        sensor_value = readadc(sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        if sensor_value in range(119, 121):
                print("1")
        elif sensor_value in range(217, 219):
                print("4")
        elif sensor_value in range(163, 165):
                print("7")
        elif sensor_value in range(104, 106):
                print("*")
        
        sensor_adc = 2
        sensor_value = readadc(sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        if sensor_value in range(629, 631):
                print("2")
        elif sensor_value in range(783, 785):
                print("5")
        elif sensor_value in range(711, 713):
                print("8")
        elif sensor_value in range(593, 595):
                print("0")

        sensor_adc = 1
        sensor_value = readadc(sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        if sensor_value in range(266, 268):
                print("3")
        elif sensor_value in range(427, 429):
                print("6")
        elif sensor_value in range(342, 344):
                print("9")
        elif sensor_value in range(237, 239):
                print("#")
        

Now i need to use the keypad to input a 4digit password (example: 1234) to light up an LED. Any pointers or codes?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Keypad Password

Wed Jul 29, 2015 11:28 am

Have you ever created a function in python before? I'd suggest modifying your code to turn it in to a function that waits for a single keypress and returns the result

Then you can do something like this (untested code so probably won't execute)

Code: Select all

correct_pass = "1234"
pass = ""
while len(pass) < 4:
    key = getKeyPress()
    pass = pass + key
if key == pass:
    print("Access Granted")
else:
    print("Access Denied")
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

JustASchKid
Posts: 19
Joined: Thu Jun 04, 2015 12:56 am

Re: Keypad Password

Thu Jul 30, 2015 1:19 am

scotty101 wrote:Have you ever created a function in python before? I'd suggest modifying your code to turn it in to a function that waits for a single keypress and returns the result

Then you can do something like this (untested code so probably won't execute)
No I had not created a function before. Is there any links you can introduce to me?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Keypad Password

Thu Jul 30, 2015 4:16 am

JustASchKid wrote:
scotty101 wrote:Have you ever created a function in python before? I'd suggest modifying your code to turn it in to a function that waits for a single keypress and returns the result

Then you can do something like this (untested code so probably won't execute)
No I had not created a function before. Is there any links you can introduce to me?
http://lmgtfy.com/?q=python+function

mawallace
Posts: 13
Joined: Tue Mar 28, 2017 11:07 pm

Re: Keypad Password

Thu Apr 06, 2017 5:40 pm

where will i define the getkeypadpassword im getting this error that it's not defined

mawallace
Posts: 13
Joined: Tue Mar 28, 2017 11:07 pm

Re: Keypad Password

Thu Apr 06, 2017 5:51 pm

getting error key_pressed not defined?

Return to “Beginners”