Skip to main content
6 of 7
indented code to display correctly
goldilocks
  • 60.1k
  • 17
  • 115
  • 230

Toggle on/off with a push button (WiiMote)

I want to make a on/off signal with only a push button, exually its for my WiiMote but I dont think that that makes a difference. I want a led to turn it on ans off.

I was thinking about a counter in Python, state 1 is signal ON and state 2 is reset to 0(0 would be OFF). I have seen other posts about this and tried them, but none off them works:(

This is what I tried.

while true:
  if (GPIO.input(11) == 1):
    if (status == 0):
      GPIO.output(22, 0)
      status = 1

    if (status == 1):
      GPIO.output(22, 1)
      status = 0

And:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(22, GPIO.OUT) # LED
GPIO.setup(11, GPIO.IN) # Button

def toggleLEDcallback(channel):
    if (GPIO.input(22) == True):
        GPIO.output(22, False)
    else:
        GPIO.output(22, True)

while 1:
    ...
    # do some other stuff
    ...
GPIO.cleanup()

I tried also with 2 transistors but that didnt work either. Any other hardware Solutions?

I hope one off you would like to help me building my robot!!

Below I paste a photo off my robot, for in case you where wondering how it looks like;)

Thanks!