-2

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()
clissolo
  • 1
  • 1
  • Take a look at this SO post: [https://stackoverflow.com/questions/419163/what-does-if-name-main-do](https://stackoverflow.com/questions/419163/what-does-if-name-main-do). If you move the definitions of `root` and `c` into your if statement there it should start to work more how you want it to. – Preston Hager Jan 15 '20 at 06:37
  • Do you want to keep turning solenoid on and off when you click the `Start` button, and stop the on/off of solenoid when you click the `Stop` button? – acw1668 Jan 15 '20 at 07:23
  • acw1668- no i do not, just wanna hit start, run my guesstimated range and let r' buck – clissolo Jan 15 '20 at 07:36
  • Preston Hager - i moved the root and c in every space and every indentation (when i got the error), and it did not work... – clissolo Jan 15 '20 at 07:47
  • @clissolo: Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Jan 15 '20 at 09:01

1 Answers1

0

To start, look at the

self.quitButton = Button(frame, text ="Stop", bg="red", fg="black", command = lambda: solenoid_on(21), width=20, height=5)

Line. Pretty sure there is a 'space' too much. Maybe that happened while copy-pasting...just to make aware of it.

I think moving:

    root = Tk()
    root.geometry('500x500')      #size of window

    c=CanSmasher(root)
    root.mainloop()`

into the "if-statement" should be the answer as stated in @Preston Hager's comment

Last Hint: Consider reading/following the python style-guide. That will make your code easier to use and read for other python-developers. For example, That would include changing printButton to print_button and the same for quitButton
Python-Style-Guide

sxeros
  • 668
  • 6
  • 21
  • sxeros - thank you for the input, it is definitely a mess not gonna lie, I'm kinda wishing there was a copy and paste method for this....didn't look for it i guess, but this was me researching, reading, watching videos and executing lol ....all within good time I'll get it figured out!! – clissolo Jan 15 '20 at 08:07