0

Update So this is now the code I'm running. It now works but is bouncing/floating. - I believe this can be controlled within the code itself by looking for an edge value as opposed to a high or low, OR it can be controlled with a resistor in the hardware.

    import os, RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(22,GPIO.IN)
GPIO.setup(23,GPIO.IN)
GPIO.setup(24,GPIO.IN)

while True:
   if(GPIO.input(22) == GPIO.LOW):
      print ("22 Input")
      # more code for pin 22 here
   elif(GPIO.input(23) == GPIO.LOW):
      print ("23 Input")
      # more code for pin 23 here
   elif(GPIO.input(24) == GPIO.LOW):
      print ("24 Input")
      # more code for pin 24 here

Original Post Could someone please help me write a python script. I'm using a 3 momentary button switches as inputs and I'm really struggling with the code. I understand there are a lot of tutorials on this but I can't seem to get it right!

here's a picture of how I have my 3 buttons wired up - https://learn.adafruit.com/system/assets/assets/000/024/487/large1024/raspberry_pi_pi_jukebox_buttons_bb.png?1429117506

I've been through two python scripts that looked promising but they kept floating/bouncing

Also is it a good idea to use GPIO.cleanup() even if the program is meant to be running all the time?

Extra points if it starts on boot and runs in the background :P

I'm running Wheezy, and thank you so much guys this community is amazing.

I've tried two different bits of code.

    #!/usr/bin/env python

import os
from time import sleep

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)

while True:
    if (GPIO.input(22) == False):
        os.system('echo hello &')

    if (GPIO.input(23) == False):
        os.system('echo hello &')

    if (GPIO.input(24)== False):
        os.system('echo hello &')

    sleep(0.1);

Also I've tried this code

import RPi.GPIO as GPIO
import time
import  os
GPIO.setmode(GPIO.BCM)
GPIO.setop(22, GPIO.IN)
while True:
    if GPIO.input(22):
    os.system("echo hello")
    prev_input = input
time.sleep(0.005)
  • As the wiring diagram appears correct, your software must be at fault. – joan May 22 '15 at 10:16
  • Hi Joan, thank you. I believe this to be the case also. I am having trouble with my python scripts. – totalconfusion May 22 '15 at 10:20
  • If you post a script for reading one switch we may be able to help. – joan May 22 '15 at 10:24
  • Certainly, bear with me 10 minutes I'll post them up. Thanks! – totalconfusion May 22 '15 at 10:25
  • Edit your original post and add the code with the {} in the middle top of the edit area. – joan May 22 '15 at 10:35
1

Without seeing your code I would say you are doing everything right :) With such simple connection you are bound to get debouncing and you can solve it with software - by adding hysteresis. There is a tutorial on debouncing that might come handy in solving the problem. For additional help - it would be nice if you could post the code, so we can help you with that. You can also see hardware solution to debounce effect in the attached document.

  • I just added the code, thanks for helping. I found that article a bit heavy to be honest. I'm not really much of a programmer. From what I know the bounce should be stopped by having a resistor in the circuit and/or by looking for a trailing edge instead of a HIGH or LOW. – totalconfusion May 22 '15 at 10:56

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.