Help with single button to multiple GPIO
Posted: Mon Apr 13, 2015 9:28 pm
Hi all im new to programming and have read many tutorials to get thus far but im having some issues in how to get my single button to execute multiple of outputs based from other inputs, currently this kind of works but the button gets stuck and the label doesn't appear until all the script has run.
Code: Select all
from Tkinter import *
import RPi.GPIO as GPIO
import time
import Tkinter as tk
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT, initial=0)
GPIO.setup(22, GPIO.OUT, initial=0)
GPIO.setup(18, GPIO.OUT, initial=0)
GPIO.setup(23, GPIO.OUT, initial=0)
GPIO.setup(24, GPIO.OUT, initial=0)
GPIO.setup(21, GPIO.OUT, initial=0)
GPIO.setup(17, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.IN, GPIO.PUD_DOWN)
root = tk.Tk()
root.title("Tester")
def toggle_text():
if button["text"] == "Begin Test":
button["text"] = "Stop Test"
label["text"] = "Test Began"
label.config(bg='green', fg='black')
time.sleep(0.5)
GPIO.output(21, 1)
time.sleep(0.5)
else:
button["text"] = "Begin Test"
label["text"] = "Test Ended"
label.config(bg='dark red', fg='white')
GPIO.output(21, 0)
GPIO.output(27, 0)
GPIO.output(22, 0)
GPIO.output(18, 0)
GPIO.output(23, 0)
GPIO.output(24, 0)
time.sleep(0.5)
if GPIO.input(25) == TRUE:
print GPIO.input(25)
label["text"] = "Relay 1"
label.config(bg='blue', fg='yellow')
time.sleep(2)
GPIO.output(27, 1)
time.sleep(2)
else:
print "Relay 1 failed"
time.sleep(0.5)
if GPIO.input(25) == TRUE:
print GPIO.input(25)
label["text"] = "Relay 2"
label.config(bg='black', fg='white')
time.sleep(2)
GPIO.output(22, 1)
time.sleep(2)
else:
print ["Relay 2 failed"]
time.sleep(0.5)
if GPIO.input(25) == TRUE:
print GPIO.input(25)
label["text"] = "Relay 3"
label.config(bg='pink', fg='green')
time.sleep(2)
GPIO.output(18, 1)
time.sleep(2)
else:
print ["Relay 3 failed"]
time.sleep(0.5)
if GPIO.input(25) == TRUE:
print GPIO.input(25)
label["text"] = "Relay 4"
label.config(bg='yellow', fg='black')
time.sleep(2)
GPIO.output(23, 1)
time.sleep(2)
else:
print ["Relay 4 failed"]
time.sleep(0.5)
if GPIO.input(25) == TRUE:
print GPIO.input(25)
label["text"] = "Relay 5"
label.config(bg='orange', fg='black')
time.sleep(2)
GPIO.output(24, 1)
time.sleep(2)
else:
print ["Relay 5 failed"]
time.sleep(0.5)
if GPIO.input(17) == TRUE:
print GPIO.input(17)
label["text"] = "Cancelled"
label.config(bg='red', fg='white')
time.sleep(2)
GPIO.output(21, 0)
time.sleep(2)
GPIO.output(27, 0)
time.sleep(2)
GPIO.output(22, 0)
time.sleep(2)
GPIO.output(18, 0)
time.sleep(2)
GPIO.output(23, 0)
time.sleep(2)
GPIO.output(24, 0)
time.sleep(2)
else:
print ["input 2 failed try press a button"]
time.sleep(0.5)
button = tk.Button(text="Begin Test", width=12, command=toggle_text)
button.pack(padx=100, pady=10)
button2 = tk.Button(text="Quit", command=exit)
button2.pack(padx=50, pady=10)
label = tk.Label(text=" ", width=24, font='bold')
label.pack(padx=200, pady=10)
root.mainloop()
GPIO.cleanup()