Page 1 of 1

Adding a second button to my project

Posted: Sun May 05, 2019 7:40 pm
by duckredbeard
So I got this running nicely, with much help from this community. I am eager to expand this to monitoring a second door. I present to you...

Code: Select all

import requests
import RPi.GPIO as GPIO
import time

r ="") # This URL hit causes my phone to make a notification that the script is running.

buttonRDPin = 15
buttonRUPin = 16
ledRDPin = 11
ledRUPin = 12

buttonLDPin = 38
buttonLUPin = 40
ledLDPin = 35
ledLUPin = 37

GPIO.setup(ledRUPin, GPIO.OUT)
GPIO.setup(ledRDPin, GPIO.OUT)
GPIO.setup(buttonRUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonRDPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(ledLUPin, GPIO.OUT)
GPIO.setup(ledLDPin, GPIO.OUT)
GPIO.setup(buttonLUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonLDPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

logicright = 1 
while True:
    buttonState = GPIO.input(buttonRUPin)
    if buttonState == False:
        if logicright == 0:
            GPIO.output(ledRUPin, GPIO.HIGH)
            GPIO.output(ledRDPin, GPIO.LOW)
            r ="")
            logicright = 1
        if logicright == 1:
            GPIO.output(ledRUPin, GPIO.LOW)
            GPIO.output(ledRDPin, GPIO.HIGH)
            r ="")
            logicright = 0

logicleft = 1 
while True:
    buttonState = GPIO.input(buttonLUPin)
    if buttonState == False:
        if logicleft == 0:
            GPIO.output(ledLUPin, GPIO.HIGH)
            GPIO.output(ledLDPin, GPIO.LOW)
            r ="")
            logicleft = 1
        if logicleft == 1:
            GPIO.output(ledLUPin, GPIO.LOW)
            GPIO.output(ledLDPin, GPIO.HIGH)
            r ="")
            logicleft = 0
So the first set is working, all the stuff associated with the RIGHT door. I get the LEDs to switch, the URL is getting the hit.

Since the Right was all working, I copied those lines and changed all references of R or right to L and left. Thought it would just move on through the Right stuff and react to when the Left is switched. I am not even getting the LEDs associated with the left. How do I get it to monitor the right the left at the same time?

Re: Adding a second button to my project

Posted: Sun May 05, 2019 8:48 pm
by scotty101
You need to place the reading and processing of the two buttons in to the same 'while True' loop.

At the moment your code gets stuck in the first loop and never enters the second to process the second button.

Re: Adding a second button to my project

Posted: Sun May 05, 2019 10:25 pm
by duckredbeard
Awesome...that is the fix! After doing the edit, I was still having issues. When I went to move the jumpers to different pins to monitor the NC versus the NO positions, I realized the swith wasn't even connected!

After connecting it, I have success! I will do the hardware mount tonight and repost the complete code after real world implementation. Thanks for being here for us beginners!

I will be crossposting this project in Tasker's forum and to share how others can make a project like this.