Question: The code is supposed to detect which of the GPIO Pins is pulled high by a switch ( in reality, it has four if statements

The code is supposed to detect which of the GPIO Pins is pulled high by a switch ( in reality, it has four "if" statements (one "if" for each of the four pins) but for simplicity only showing one)
My question is: how do I turn this into a function? The input of the function should be the the four pin numbers to which the switches are connected to (i.e 17,27, 22, 5) and the output should print which of those pins was pulled high (switch closed). Ultimately, I want to be able to import this file to another file and simply call the function. Thanks!
import RPi.GPIO as GPIO import time import sys GPIO.setmode (GPIO.BCM) pin_1 = 17 # BCM17 pin_2= 27 pin_3= 22 pin_4= 5 GPIO.setup(pin_1, GPIO.IN) GPIO.setup(pin_2, GPIO.IN) GPIO.setup(pin_3, GPIO.IN) GPIO.setup(pin_4, GPIO.IN) try: while True: inputvalue1 = GPIO.input(17) # GPIOs that swicthes are connected to inputvalue2 = GPIO.input (27) inputvalue3 = GPIO.input (22) inputvalue4 = GPIO.input(5) if inputvalue1==1: # active high swicth print("GPIO number: ",pin_1," is pressed") while inputvalue1==1: # so that message doesnt print continuosly inputvalue1=GPIO.input(pin_1) import RPi.GPIO as GPIO import time import sys GPIO.setmode (GPIO.BCM) pin_1 = 17 # BCM17 pin_2= 27 pin_3= 22 pin_4= 5 GPIO.setup(pin_1, GPIO.IN) GPIO.setup(pin_2, GPIO.IN) GPIO.setup(pin_3, GPIO.IN) GPIO.setup(pin_4, GPIO.IN) try: while True: inputvalue1 = GPIO.input(17) # GPIOs that swicthes are connected to inputvalue2 = GPIO.input (27) inputvalue3 = GPIO.input (22) inputvalue4 = GPIO.input(5) if inputvalue1==1: # active high swicth print("GPIO number: ",pin_1," is pressed") while inputvalue1==1: # so that message doesnt print continuosly inputvalue1=GPIO.input(pin_1)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
