I am trying to write the following program (8Relay.py
) using python on the Raspberry Pi.
I want to use the input from a GPIO pin to turn an LED on or off, depending on what input was pressed (1~8).
For example, if I input 17, GPIO17 should be turned on. When I input 17 again, GPIO17 should be turned off. This is what I want.
I'm not quite sure how I should go about doing this. My current code is listed below:
8Relay.py
#! /usr/bin/python
import os, sys, time
import RPi.GPIO as GPIO
Relays = [False, False, False, False, False, False, False, False]
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(4, GPIO.OUT)
G1 = 17
G2 = 18
G3 = 27
G4 = 22
G5 = 23
G6 = 24
G7 = 25
G8 = 4
for A in [17, 18, 27, 22, 23, 24, 25, 4]:
GPIO.output(G1, 0)
GPIO.output(G2, 0)
GPIO.output(G3, 0)
GPIO.output(G4, 0)
GPIO.output(G5, 0)
GPIO.output(G6, 0)
GPIO.output(G7, 0)
GPIO.output(G8, 0)
def Toggle(Relay):
Current_situation = Relays[Relay]
print Relays[Relay]
Relays[Relay] = not Current_situation
GPIO.output([17, 18, 27, 22, 23, 24, 25, 4][Relay-1], not Current_situation)
return
Choice = "True"
while Choice:
os.system("clear")
for A, B in enumerate(Relays):
if B:
B = "High"
else:
B = "Low"
print "Relay({}) [{}]".format([G1, G2, G3, G4, G5, G6, G7, G8][A], B)
try:
Choice = int(input("GPIO number :"))
if Choice >= 1 and Choice <= 8 :
Toggle(Choice)
except:
print "Something Failed."
time.sleep(0.5)