Shazyzang
Posts: 3
Joined: Sat Dec 10, 2016 10:16 am

Keypad (matrix 4x4) code lock

Wed Aug 30, 2017 9:52 pm

Hey, I am a python beginner and for now I am working on a alarm system. The hardware is already set up and wired. But I have problems with writing a code lock code.. I tried everything and searched for solutions but I found only not working solutions or nothing that would help me.

I just want to enter a 5 digit code. If it is correct it should change a global variable (alarm) and when it is wrong it should restart. I have written two different codes maybe someone can help me with finding a solution. The code is attached.

Code: Select all

import RPi.GPIO as GPIO
     
    GPIO.setmode(GPIO.BOARD)
     
    MATRIX = [ [1, 2 ,3 , 'A'],
               [4, 5, 6,  'B'],
               [7, 8, 9,  'C'],
               ['*',0,'#','D'] ]
     
    ROW = [21,4,27,6]
    COL = [5,13,19,26]
    code = [1,2,3,4,"#"]
	digits= []
	global alarm
	
    for j in range(4):
            GPIO.setup(COL[j], GPIO.OUT)
            GPIO.output(COL[j], 1)
     
    for i in range(4):
            GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
     
    try:
            while(True):
                    for j in range(4):
                            GPIO.output(COL[j],0)
     
                            for i in range(4):
                                    if GPIO.input(ROW[i]) == 0:
                                            print MATRIX[i][j]
                                            key = MATRIX[i][j]
											
											while(GPIO.input(ROW[i]) == 0):
                                                    pass
                            GPIO.output(COL[j],1)
							
							if key == "*" && if len(digits) > 0:
								del digits[-1]
								length=len(digits)
								lcd.clear()
								for i in length:
									lcd.message("*")

							else:	
								digits.append(key)
								lcd.message("*")

							if len(digits)==4:
								if digits == code:
									if alarm=0:
										lcd.message("CODE CORRECT")
										time.sleep(1)
										alarm = 1
										lcd.clear()
										lcd.message("SYSTEM ARMED")
									elif alarm >0
										lcd.message("CODE CORRECT")
										time.sleep(1)
										alarm = 0
										lcd.clear()
										lcd.message("SYSTEM UNARMED")
								else:
									lcd.clear()
									lcd.message("Wrong Code")
									time.sleep(0.5)
									lcd.clear()
									digits=[]
									
						
									
									
							
    except KeyboardInterrupt:
            GPIO.cleanup()

Code: Select all

from pad4pi import rpi_gpio
import RPi.GPIO as GPIO
import time
 
import Adafruit_CharLCD as LCD
 
 
# Raspberry Pi pin setup
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 2
 
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
 
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_$
 
 
 
KEYPAD = [
        [1,2,3,"A"],
        [4,5,6,"B"],
        [7,8,9,"C"],
        ["*",0,"#","D"]
]
 
 
 
 
COL_PINS = [5,13,19,26] # BCM numbering
ROW_PINS = [21,4,27,6] # BCM numbering
 
factory = rpi_gpio.KeypadFactory()
 
# Try factory.create_4_by_3_keypad
# and factory.create_4_by_4_keypad for reasonable defaults
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
 
def printKey(key):
   while 1:
        
        if len(code)==0:
            lcd.clear()
            code=[]
            lcd.message("ENTER CODE:")
            print("Enter your code: ")
        
        print(key)
        if key == "*" && len(code)>0:
            del code[-1]
            length=len(code)
            lcd.clear()
            for i in length:
                lcd.message("*")
            
        else:
            lcd.message("*")
            time.sleep(0.4)
            code.append(key)
        
        if len(code) == 5:
            print(code)
            print("\n")
            if code == [1,2,3,4,'#']:
                print("Very good")
                lcd.clear()
                lcd.message("Code accepted")
                
                if alarm == 0:
                    alarm=1
                    time.sleep(1)
                    lcd.clear()
                elif alarm > 0:
                    alarm = 0
                    time.sleep(1)
                    lcd.clear()
                
                
                
            else:
                print("Wrong Code")
                lcd.message("Wrong Code")
                time.sleep(1)
                code=[]
                lcd.clear()
                
                
                
# printKey will be called each time a keypad button is pressed
keypad.registerKeyPressHandler(printKey)
 

Thank you so much already

Return to “Beginners”