0

I have 1 input button PIN 18 and 2 outputs PIN 12 and 16, when the input is HIGH, I would like the outputs to be activated and deactivate when input is LOW. I have attempted to use

    if (GPIO.input(18)==True):

    GPIO.output(16, False)
    GPIO.output(12, False)

However, the outputs are only activated if the input is already HIGH before I run the program and it doesn't reactivate when I release the button and press it again. This is a snippet of my code right now

class Controller(tk.Tk):
def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    tk.Tk.wm_title(self, "Controller")

    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0,weight=1)


    menubar = tk.Menu(container)
    moremenu = tk.Menu(menubar, tearoff=0)
    moremenu.add_command(label="Contact Info", 
                         command=lambda:self.show_frame(ContactPage))
    moremenu.add_command(label="Exit", command=exitmenu)
    menubar.add_cascade(label="More", font=MENU_FONT ,menu=moremenu)
    menubar.add_cascade(label="    ", state="disabled")


    explorer= tk.Menu(menubar, tearoff=0)
    explorer.add_command(label="Manual Ventilator Ctrl",
                         command=lambda:self.show_frame(ManualCtrl))
    explorer.add_command(label="Battery Fault Bypass",
                         command=lambda:self.show_frame(BatteryBypass))
    explorer.add_command(label="Fault History",
                         command=lambda:self.show_frame(Faulthist))
    explorer.add_command(label="Manual Battery Charging",
                         command=lambda:self.show_frame(Manualcharge))
    menubar.add_cascade(label="Explorer", font=MENU_FONT, menu=explorer)
    menubar.add_cascade(label="    ", state="disabled")


    syssetupmenu= tk.Menu(menubar, tearoff=0)
    setup= tk.Menu(menubar, tearoff=0)
    setup.add_command(label="Clock Setup",
                      command=lambda:self.show_frame(Clocksetup))
    setup.add_cascade(label="System Setup", menu=syssetupmenu)
    setup.add_command(label="Factory Default",
                      command=lambda:self.show_frame(Factorydef))
    syssetupmenu.add_command(label="Timer Setting",
                             command=lambda:self.show_frame(Timerset))
    syssetupmenu.add_command(label="Latch Setting",
                             command=lambda:self.show_frame(Latchset))
    syssetupmenu.add_command(label="Change User Password",
                             command=lambda:self.show_frame(Changeuserpw))
    syssetupmenu.add_command(label="Change Access Password",
                             command=lambda:self.show_frame(Changeaccesspw))
    syssetupmenu.add_command(label="Change Lockout Password",
                             command=lambda:self.show_frame(Changeadminpw))
    syssetupmenu.add_command(label="Battery Display Calibration",
                             command=lambda:self.show_frame(Batterydisp))
    syssetupmenu.add_command(label="Daily Auto Charging",
                             command=lambda:self.show_frame(Autocharg))
    syssetupmenu.add_command(label="Fault Record Reset",
                             command=lambda:self.show_frame(Faultrec))
    syssetupmenu.add_command(label="Diagnostic Records",
                             command=lambda:self.show_frame(Diag))
    syssetupmenu.add_command(label="Battery Fault Setpoints",
                             command=lambda:self.show_frame(Batterysetpts))
    menubar.add_cascade(label="Setup", font=MENU_FONT ,menu=setup)


    tk.Tk.config(self, menu=menubar)


    self.frames = {}

    for F in (DefaultScreen, ContactPage, ManualCtrl,BatteryBypass, Faulthist, Manualcharge,
              Clocksetup,Timerset,Latchset,Changeuserpw,Changeaccesspw,Changeadminpw,
              Batterydisp,Autocharg,Faultrec,Diag,Batterysetpts,Factorydef):

        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(DefaultScreen)

def show_frame(self,cont):
    frame = self.frames[cont]

    frame.tkraise()

if (GPIO.input(18)==True):

    GPIO.output(16, False)
    GPIO.output(12, False)

I would really appreciate any help right now, an alternative method would be appreciated as well. Thank you so much :)

  • Try putting the 'if (GPIO.input(18)==True):' code in a function and using root.after() (I think self.after() in this case) to call the function intermittently to check the state of GPIO.input(18). Example usage of the after method: https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method – 10SecTom May 11 '18 at 11:55
  • no problem, hope it helped. – 10SecTom May 14 '18 at 07:37

1 Answers1

0

Use the interrupts and edge detection functionality provided by RPi.GPIO. https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

You can either use the tkinter .after method to periodically check if an event has occurred since you last checked.

#Use this when you setup your program
GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on 

#Use this inside your periodically called function
if GPIO.event_detected(channel):
    print('Button pressed')

Or you can register a callback to a function when an event occurs

GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_function)

then inside my_function would be the code to change the state of the outputs.

I'd suggest the second method for a quick response to the change in input state.

scotty3785
  • 6,763
  • 1
  • 25
  • 35