MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Python - One momentary switch, LED, and a shut-off timer

    Troubleshooting
    1
    3
    2999
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Unbored
      Unbored last edited by Unbored

      Working on programming a LED strip lights around the mirror, with a single push button. I am good with the GPIOs and the relay required, but seeking some guidance on the code

      • First button press: Lights activate. Have the option to move to the second button press or if the button is not pressed a second time, then the side lights will turn off automatically after 30 minutes.
      • Second button press: Lights turn off and python is waiting for the first button press (again).

      Below is one version that I been working on where I wanted to divide the lights (side lights (“low”), then side&top lights (“high”), then all off), but at this point I would settle for just all lights at the same time.

      The auto-shutoff is required to ensure they aren’t left on for days at time. 🙂

      It is possible that I need to pull in another module, but I can’t seem to hack my way into figuring out the shutoff timer or where to place it, so it is time to call in a lifeline. Thanks.

      import RPi.GPIO as GPIO
      from time import sleep
      
      GPIO.setmode(GPIO.BCM)
      
      LEDPinLow = 26
      LEDPinHigh = 22
      buttonPin = 5
      
      # Setup the pin the LED is connected to
      GPIO.setup(LEDPinLow, GPIO.OUT)
      GPIO.setup(LEDPinHigh, GPIO.OUT)
      
      # Setup the button
      GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
      
      buttonPress = True
      ledStateLow = False
      ledStateHigh = False
      
      while (1):
              print("Press it")
              buttonPress = GPIO.input(buttonPin)
              if buttonPress == False and ledStateLow == False and ledStateHigh == False:
                  GPIO.output(LEDPinLow, True)
                  GPIO.output(LEDPinHigh, False)
                  print("LED ON - Side lights only")
                  ledStateLow = True
                  ledStateHigh = False
                  sleep(.25)
                  
              elif buttonPress == False and ledStateLow == True and ledStateHigh == False:
                  GPIO.output(LEDPinLow, True)
                  GPIO.output(LEDPinHigh, True)
                  print("LED ON - Side and Top Lights ")
                  ledStateLow = True
                  ledStateHigh = True
                  sleep(0.25)
      
              elif ledStateLow == True and ledStateHigh == True:
                  sleep(5)
                  GPIO.output(LEDPinLow, False)
                  GPIO.output(LEDPinHigh, False)
                  print("LEDs OFF")
                  ledStateLow = False
                  ledStateHigh = False
                  sleep(0.5)
      
              sleep(0.15)
      
      
      
      1 Reply Last reply Reply Quote 0
      • Unbored
        Unbored last edited by Unbored

        Re: Python - One momentary switch

        I found the answer over on stackexchange and figured I would share. The mirror is a college graduation present for my youngest daughter; she requested the light ring (I heard the word make-up followed by blah, blah, blah, blah…, kidding of course.). The 30 minute timer was not a joke, there’s no way I am leaving responsibility of turning off the light to a college grad 🤦🏼

        I am still working on it and will post a thread in the correct forum when it is done.

        The answers are here: https://raspberrypi.stackexchange.com/questions/66639/python-one-momentary-switch-led-and-a-shut-off-timer?answertab=active#tab-top

        #!/usr/bin/env python
        
        import time
        import pigpio
        
        LEDPin = 26
        buttonPin = 5
        
        LEDOn = False
        minutesOn = 0
        
        def callback(gpio, level, tick):
           global minutesOn, LEDOn
           if level == 0: # button press
              if LEDOn:
                 print("LED off")
                 LEDOn = False
                 pi.write(LEDPin, 0)
              else:
                 print("LED on")
                 LEDOn = True
                 pi.write(LEDPin, 1)
                 minutesOn = 0
           elif level == pigpio.TIMEOUT:
              if LEDOn:
                 minutesOn += 1
                 print("LED on for {} minutes".format(minutesOn))
                 if minutesOn >= 30:
                    print("LED off")
                    LEDOn = False
                    pi.write(LEDPin, 0)
        
        pi = pigpio.pi()
        if not pi.connected:
           exit()
        
        # Setup the pin the LED is connected to
        pi.set_mode(LEDPin, pigpio.OUTPUT)
        
        # Setup the button
        pi.set_mode(buttonPin, pigpio.INPUT)
        pi.set_pull_up_down(buttonPin, pigpio.PUD_UP)
        pi.set_glitch_filter(buttonPin, 5000)
        
        pi.set_watchdog(buttonPin, 60000)  # watchdog every minute
        
        cb = pi.callback(buttonPin, pigpio.EITHER_EDGE, callback)
        
        while True: # all the work is done in the callback
           time.sleep(1)
        
        1 Reply Last reply Reply Quote 0
        • Unbored
          Unbored last edited by

          Here’s a pic of the work in progress.
          alt text

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy