3

I want to make a on/off signal with only a push button, actually its for my WiiMote but I don't think that that makes a difference. I want a led to turn it on and 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 of them work:(

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 True:
    ...
    # do some other stuff
    ...
GPIO.cleanup()

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

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

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

Thanks!

  • Basically this is not possible (without external hardware). What exactly have you tried, and what are you trying to achieve? – Milliways Sep 30 '15 at 23:45
  • @Milliways I dont know the exact code but it begins with State = 0 If (input) =1: If State == 1: State = 0 Elif state == 0: State = 1 I tried also with 2 transistors but that didnt work either. Any other hardware Solutions? – Rutger S Oct 1 '15 at 6:31
  • You still haven't explained WHAT you are trying to do. Do you want to shut the Pi down or turn power off? If you want to shut down and restart this can be done with 2 buttons. If you want to turn off power this requires hardware, and depends on how you are powering the Pi. There are actually dozens (if not hundreds) on post on this site discussing this in more detail. – Milliways Oct 1 '15 at 7:27
  • @Milliways I want to turn a led on and off. I cant find any other posts. – Rutger S Oct 1 '15 at 8:07
  • Your question is quite misleading. This is (almost) the most basic Pi project. Almost every introductory tutorial describes how to do these things. I suggest you get one of the basic intros. First try to write code to turn a LED on and off then try reading an input. If you don't know what code you have written how do you expect us to help. – Milliways Oct 1 '15 at 9:52
1

If I press that button, the led is going ON. If I release that button, it stays on. But if I press it again, it will go off.

That's what I would expect looking at your first example:

if (GPIO.input(11) == 1):
      [...]

That condition is based on the GPIO's state being 1; there is no corresponding condition for when it is 0. So that code will only respond to that -- if 1 means the button is depressed (button circuits can done either way), then this code only responds to when the button is pressed down, not when it is released.

You then toggle based on the last change you made to (what I presume is) an internal variable, state. Everytime you press the button, this will alternate. But releasing the button does nothing.

-1
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23
GPIO.setup(24, GPIO.OUT)  #LED to GPIO24


try:
    times = 0
    debounce = 0.4
    state = True
    previous = False
    while True:
         reading = GPIO.input(23)
         if reading == False and previous == False and time.time()-times > debounce:
             if state == True:    
                 state = False
             else: 
                 state = True
             times = time.time()
         GPIO.output(24, state)
         previous = reading

             #print (button_state)
except:
    GPIO.cleanup()
  • 1
    How does this answer the question? You did not even comment your code to help the OP understand how or if this is supposed to work. – Steve Robillard Nov 27 '18 at 5:05
  • It sures answers the first question on the very top. That's it. – Lalo Quera Nov 28 '18 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.