Python script with many elif for GPIO in and out

mredhwk10

I wrote a python script to detect momentary button presses and record each to a file. Upon each press, an LED will also light up through GPIO.OUT. The original file works wonders, however I'm currently needing to add two more buttons into the mix (4 total now) and each will also write to a file plus have an LED light up. When I execute the script I thought should work, only button 1 works. Any help for how to debug the non-working code is appreciated.

NON-WORKING CODE (4 BUTTON):

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import serial
import syslog
import MySQLdb as mdb
import subprocess

poursdir = '/var/www/RPints'
path = '/var/www/RPints/includes/pours.php'

outpin1 = 26
inpin1 = 18
outpin2 = 16
inpin2 = 23
outpin3 = 6
inpin3 = 17
outpin4 = 5
inpin4 = 22

GPIO.setmode(GPIO.BCM)
GPIO.setup(outpin1, GPIO.OUT)
GPIO.setup(inpin1, GPIO.IN)
GPIO.setup(outpin2, GPIO.OUT)
GPIO.setup(inpin2, GPIO.IN)
GPIO.setup(outpin3, GPIO.OUT)
GPIO.setup(inpin3, GPIO.IN)
GPIO.setup(outpin4, GPIO.OUT)
GPIO.setup(inpin4, GPIO.IN)

while True:
    value1 = GPIO.input(inpin1) 
    value2 = GPIO.input(inpin2)
    value3 = GPIO.input(inpin3)
    value4 = GPIO.input(inpin4)
    if not value1 or value2 or value3 or value4:     
        GPIO.output(outpin1, 0)
        GPIO.output(outpin2, 0)
        GPIO.output(outpin3, 0)
        GPIO.output(outpin4, 0)
    else:
        if value1:
            GPIO.output(outpin1, 1)
            subprocess.call(["php", path, "1", "1"])
        elif value2:
            GPIO.output(outpin2, 1)
            subprocess.call(["php", path, "2", "1"])
        elif value3:
            GPIO.output(outpin3, 1)
            subprocess.call(["php", path, "3", "1"])
        elif value4:
            GPIO.output(outpin4, 1)
            subprocess.call(["php", path, "4", "1"])
    time.sleep(.3)
GPIO.cleanup()

WORKING CODE (2 BUTTON):

 #!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import serial
import syslog
import MySQLdb as mdb
import subprocess

poursdir = '/var/www/RPints'
path = '/var/www/RPints/includes/pours.php'

outpin1 = 26
inpin1 = 18
outpin2 = 16
inpin2 = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(outpin1, GPIO.OUT)
GPIO.setup(inpin1, GPIO.IN)
GPIO.setup(outpin2, GPIO.OUT)
GPIO.setup(inpin2, GPIO.IN)

while True:
    value1 = GPIO.input(inpin1) 
    value2 = GPIO.input(inpin2)
    if not value1 and not value2:    
        GPIO.output(outpin1, 0)
        GPIO.output(outpin2, 0)
    else:
        if value1 and not value2:
            GPIO.output(outpin1, 1)
            subprocess.call(["php", path, "1", "1"])
        elif value2 and not value1:
            GPIO.output(outpin2, 1)
            subprocess.call(["php", path, "2", "1"])
    time.sleep(.3)
    time.sleep(.05) 
GPIO.cleanup()
bpeikes

Shouldnt you be checking for the full set of values? ie

if not value1 and not value2 and not value3 and not value4:
       # reset
elif value1 and not value2 and not value3 and not value4:
      #light up led 1
elif not value1 and value2 and not value3 and not value4:
      #light up led 2
elif not value1 and not value2 and value3 and value4:
     #light up 3
elif not value1 and not value2 and not value3 and value4
      #light up 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related