nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Basic on/off switch

Fri Nov 11, 2016 12:51 am

Seemed pretty easy to make it first but it's tricky to do it right.

I'm trying to make my xbox 360 controller to turn on and off the leds connected to GPIO pins.

Code: Select all

    if joy.A():
        if light >1:
          light=0
        else:
            light = (light + 1)

    if light >0:
        GPIO.output(36,1)
        GPIO.output(38,1)
        GPIO.output(40,1)
        GPIO.output(33,1)
        GPIO.output(35,1)
        GPIO.output(37,1)
    if light ==0:
        GPIO.output(36,0)
        GPIO.output(38,0)
        GPIO.output(40,0)
        GPIO.output(33,0)
        GPIO.output(35,0)
        GPIO.output(37,0)
But the problem is, it switches on and off too often. I don't want it to cycle between on and off states as long as the button is pressed, it should only switch once per button press.

User avatar
BirchJD
Posts: 159
Joined: Tue Nov 13, 2012 7:43 pm

Re: Basic on/off switch

Fri Nov 11, 2016 5:30 am

You need to do something like:

In start up code:
JoyA = False
LastJoyA = False

Before the if:
JoyA = joy.A()

Change the if to:
if JoyA != LastJoyA and JoyA:

After the if:

LastJoyA = JoyA
http://www.newsdownload.co.uk/

Return to “Python”