## Cycle Relays when button is pressed##
from tkinter import *
import tkinter.font
import smbus
import sys
import RPi.GPIO as GPIO
### HARDWARE DEFINITIONS ###
DEVICE_BUS = 1
DEVICE_ADDR = 0x10
bus = smbus.SMBus(DEVICE_BUS)
# Set up GPIO input
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
### GUI DEFINITIONS ###
root = Tk()
root.title("Relay Test")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = "bold")
d = Entry (master = root)
d.grid(row=0,column=1)
#counter counts by 2s every time run goes through
root.counter=0
c=Label(text = "Rounds Made")
c.grid(row=6, column=1)
### Event Functions ###
def Run():
# How many times loop should be run (I want text box to enter how many times it should run)
count=int(d.get())
# gpio in series with proximity switch and stop button
while count > root.counter:
if GPIO.input(17) == 1:
root.after(500)
else:
# Time delay before lead pour.
root.after(1000)
# Relay #1 Lead pour time
bus.write_byte_data(DEVICE_ADDR, 1, 0xFF)
root.after(950)
# Lead cooldown time
bus.write_byte_data(DEVICE_ADDR, 1, 0x00)
root.after(6000)
# Relay #2 Mould eject and hold for cooldown timer
bus.write_byte_data(DEVICE_ADDR, 2, 0xFF)
root.after(6000)
bus.write_byte_data(DEVICE_ADDR, 2, 0x00)
root.counter += 2
c['text'] = 'Rounds Made: ' + str(root.counter)
#interrupt the cycle and stop everything where it is doesnt work, might need threading in def run
def Pause():
bus.write_byte_data(DEVICE_ADDR, 1, 0x00)
bus.write_byte_data(DEVICE_ADDR, 2, 0x00)
#turn off relays and reset counter to 0
def Clear():
bus.write_byte_data(DEVICE_ADDR, 1, 0x00)
bus.write_byte_data(DEVICE_ADDR, 2, 0x00)
root.counter=0
c['text'] = 'Rounds Made: ' + str(root.counter)
#Close and Clean up everything
def Close():
root.destroy()
### WIDGETS ###
# Button, triggers the connected command when it is pressed
pausebutton = Button(root, text='Pause', font=myFont, command=Pause, bg='bisque2', height=1, width=24)
pausebutton.grid(row=3,column=1)
runbutton = Button(root, text='Run', font=myFont, command = Run ,bg='bisque2', height=1, width=24)
runbutton.grid(row=2,column=1)
clearbutton = Button(root, text='Clear', font=myFont, command=Clear, bg='bisque2', height=1, width=24)
clearbutton.grid(row=4,column=1)
exitButton = Button(root, text='Exit', font=myFont, command=Close, bg='red', height=1, width=6)
exitButton.grid(row=5, column=1)
root.mainloop() # Loops forever