<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python - One momentary switch, LED, and a shut-off timer]]></title><description><![CDATA[<p dir="auto">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</p>
<ul>
<li><strong>First button pres</strong>s: 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.</li>
<li><strong>Second button press</strong>: Lights turn off and python is waiting for the first button press (again).</li>
</ul>
<p dir="auto">Below is one version that I been working on where I wanted to divide the lights (side lights (“low”), then side&amp;top lights (“high”), then all off), but at this point I would settle for just all lights at the same time.</p>
<p dir="auto">The auto-shutoff is required to ensure they aren’t left on for days at time. :-)</p>
<p dir="auto">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.</p>
<pre><code>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)


</code></pre>
]]></description><link>https://forum.magicmirror.builders/topic/2452/python-one-momentary-switch-led-and-a-shut-off-timer</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Jul 2026 07:47:58 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/2452.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 Apr 2017 12:25:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Python - One momentary switch, LED, and a shut-off timer on Fri, 05 May 2017 01:17:26 GMT]]></title><description><![CDATA[<p dir="auto">Here’s a pic of the work in progress.<br />
<img src="https://lh3.googleusercontent.com/dv8n_mgiUmSZvIKqR6N2aAxsmuXedOJybtDgmfck3FRh5us6fgu4KwVTAr2jRshadnXWaQ_Wku-khb56ScFptN4dTn19OojHu0POhkN3U17oYaYJ8od6r-jpt1pDXI3inOulAT6UEF-ylGVg5myfqa4Amb_rsEyyN8ah4Y1gd2xwvI7j0qz3VzajdsHnaX-a0KbZAkP7jO2jEl74N3UQ4MnIaTYA2MBfijYW7Lrv0IOUAKs7GA9WFcVDZS6lrDX_GaEJVQN17R-BNxEMRqYgDXgCEHT5-hj0Zm3RRDErlTdoWkI7sVKOSn8zSesemU35afEeyneqadh_I4GqUQMO0L-mj5A875ZzGLMjDcPYPK6ow9l3q0Dc_ubngzUFdjOFnIQUZRQFvGNoBCQRs_wcJ4swDwHEdT_zLFKdik6KDDulHsT9XMixJE5mS316OzEpMYLdQg1Qh4gqV_eB8lvz2R72j5z62w7SJqQrzCBbUePKHFCba_XdVIsYkJaWpNHAGWpu_-cdxLMEsDVzVw2pzII5anUo-mPO5cOEtKHI0MA7zH4ZDS8YXB-JgRtLRWKBh-q9FMGm_C3JknqDMUgL5vTd9v7dkPBy5rsHfxEtW6tBBKfJdxV0=w388-h688-no" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.magicmirror.builders/post/21187</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/21187</guid><dc:creator><![CDATA[Unbored]]></dc:creator><pubDate>Fri, 05 May 2017 01:17:26 GMT</pubDate></item><item><title><![CDATA[Reply to Python - One momentary switch, LED, and a shut-off timer on Fri, 05 May 2017 01:21:11 GMT]]></title><description><![CDATA[<p dir="auto">Re: <a href="LED">Python - One momentary switch</a></p>
<p dir="auto">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 :face_palm_tone2:</p>
<p dir="auto">I am still working on it and will post a thread in the correct forum when it is done.</p>
<p dir="auto">The answers are here: <a href="https://raspberrypi.stackexchange.com/questions/66639/python-one-momentary-switch-led-and-a-shut-off-timer?answertab=active#tab-top" target="_blank" rel="noopener noreferrer nofollow ugc">https://raspberrypi.stackexchange.com/questions/66639/python-one-momentary-switch-led-and-a-shut-off-timer?answertab=active#tab-top</a></p>
<pre><code>#!/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 &gt;= 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)
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/21186</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/21186</guid><dc:creator><![CDATA[Unbored]]></dc:creator><pubDate>Fri, 05 May 2017 01:21:11 GMT</pubDate></item></channel></rss>