Python Forum
Name 'function' not defined, although it is...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Name 'function' not defined, although it is...
#1
Hey,

I want to write some program for a 4x4 button matrix, where it simply outputs the number of the button I press.

I'm using this code:

import time
import RPi.GPIO as GPIO

GPIO.setwarnings(True)

class keypad():
	def __init__(self, columnCount = 4):
		GPIO.setmode(GPIO.BCM)
		#Button settings
		if columnCount is 4:
			self.KEYPAD = [
				[1,2,3,4],
				[5,6,7,8],
				[9,10,11,12],
				[13,14,15,16]
			]
		#RPI PIN-Allocation
			self.ROW = [18,23,24,25]
			self.COLUMN = [4,17,22,21]
		else:
			return

	def getKey(self):
		#Setting all columns as output
		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.OUT)
			GPIO.output(self.COLUMN[j], GPIO.LOW)
		#Setting all rows as input
		for i in range(len(self.ROW)):
			GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
		#Scanning rows for pressed buttons
		rowVal = -1
		for i in range(len(self.ROW)):
			tmpRead = GPIO.input(self.ROW[i])
			if tmpRead == 0:
				rowVal = i

		if rowVal < 0 or rowVal > 3:
			self.exit()
			return

		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

		GPIO.setup(self.ROW[rowVal], GPIO.OUT)
		GPIO.output(self.ROW[rowVal], GPIO.HIGH)

		colVal = -1
		for j in range(len(self.COLUMN)):
			tmpRead = GPIO.input(self.COLUMN[j])
			if tmpRead == 1:
				colVal = j

		if colVal < 0 or colVal > 3:
			self.exit()
			return

		#Returning the pressed button
		self.exit()
		return self.KEYPAD[rowVal][colVal]

	def exit(self):
		#Re-initializing all rows and columns
		for i in range(len(self.ROW)):
			GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down = GPIO.PUD_UP)

	if __name__ == '__main__':
		#Initializing the keypad
		kp = keypad()
		#Endless loop to check for pressed buttons
		while True:
			digit = None
			while digit == None:
				digit = kp.getKey()
			#Return of the button pressed
			print digit
			time.sleep(0.5)
But when running it, I get the error:
Error:
Traceback (most recent call last): File "matrix.py", line 6, in <module> class keypad(): File "matrix.py", line 71, in keypad kp = keypad() NameError: name 'keypad' is not defined
Apparently the main function doesn't know that keypad() doesn't exists...

I've searched in other threads for solutions, but haven't found any that were fitting to my problem.
From these I only learned that you need to define functions, before you use them, but here that's clearly the case in my eyes. In fact, it's pretty much the first thing I did.

So I am a little baffled...

Any help is appreciated! Big Grin
Reply
#2
Indentation of lines 69 and next is wrong. They should not be part if the class. Dedent by one level
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-23-2020, 12:38 PM)buran Wrote: Indentation of lines 69 and next is wrong. They should not be part if the class. Dedent by one level

now it seems so obvious...

Thanks tho! Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Function defined by branches antoniogalante 1 547 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  function call at defined system time? Holon 5 1,187 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Function will not return variable that I think is defined Oldman45 6 1,388 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 876 Jul-24-2020, 01:39 PM
Last Post: Gribouillis
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 817 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  python library not defined in user defined function johnEmScott 2 1,000 May-30-2020, 04:14 AM
Last Post: DT2000
  Problem with user defined main menu function stefzeer 3 1,010 Mar-27-2020, 06:12 AM
Last Post: buran
  Getting the error like function not defined .. suggest correct code raghava 1 929 Feb-04-2020, 11:20 PM
Last Post: micseydel
  list from defined function Coastal 3 1,279 Jan-06-2020, 10:20 AM
Last Post: perfringo
  User Defined function not working Raj_Kumar 4 1,198 Dec-17-2019, 12:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020