been working on it for a couple days, building an automated pop can smasher...so.... the solenoid(using a relay just for simplicity), kicks on when started, stays on, but i can use my "stop" button to stop it, and my "start" button to start it again. i cant figure out how to include my "if" statement and "range(2) string to my "start" button, so say i run it 100 times and want to "stop" it at any given time.....then once i exit my window(widget) my "IF" statement executes the rest, my code is correct without any errors, just positioning problems ..maybe...thanks for all the help and info, it is greatly appreciated!
import RPi.GPIO as GPIO
import time
from tkinter import *
import tkinter.font as font
root = Tk()
root.geometry('500x500') #size of window
class CanSmasher:
def __init__(self, master):
frame = Frame(master)
frame.pack()
myFont = font.Font(size=20) #define Font
self.printButton = Button(frame, text="Start", bg="green", fg="black", command = lambda: solenoid_off(21),width=20, height=5)
self.printButton['font'] = myFont
self.printButton.pack()
self.quitButton = Button(frame, text ="Stop", bg="red", fg="black", command = lambda: solenoid_on(21), width=20, height=5)
self.quitButton['font'] = myFont
self.quitButton.pack()
channel = 21
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def solenoid_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn solenoid on
def solenoid_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn solenoid off
c=CanSmasher(root)
root.mainloop()
if __name__ == '__main__':
try:
for i in range(2): # Number of times ran is writen in ==> [range(put run times here)]
solenoid_on(channel)
time.sleep(1) # Sets lag time
solenoid_off(channel)
time.sleep(1) # Sets run time
print("Cans Crushed", i+1)
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()