duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Adding a second button to my project

Sun May 05, 2019 7:40 pm

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
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

r = requests.post("http://bit.ly/xxxxI41y6") # 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 = requests.post("http://bit.ly/xxxUP1TQI")
            logicright = 1
    else:
        if logicright == 1:
            GPIO.output(ledRUPin, GPIO.LOW)
            GPIO.output(ledRDPin, GPIO.HIGH)
            r = requests.post("http://bit.ly/xxxxKaZ0P")
            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 = requests.post("http://bit.ly/xxxxTdahg")
            logicleft = 1
    else:
        if logicleft == 1:
            GPIO.output(ledLUPin, GPIO.LOW)
            GPIO.output(ledLDPin, GPIO.HIGH)
            r = requests.post("http://bit.ly/xxxxP6T2t")
            logicleft = 0
    time.sleep(0.1)
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?
You can't know your limits until you exceed them.

scotty101
Posts: 3671
Joined: Fri Jun 08, 2012 6:03 pm

Re: Adding a second button to my project

Sun May 05, 2019 8:48 pm

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.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: Adding a second button to my project

Sun May 05, 2019 10:25 pm

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 hackster.io to share how others can make a project like this.
You can't know your limits until you exceed them.

Return to “Beginners”