1

Hi I'm having a problem with trying to detect GPIO input on a PIN within a while loop. Turnstile B Entry button is what I am troubleshooting the statement if tsB_entry_button == False:

import RPi.GPIO as GPIO
import time
import _thread

GPIO.setmode(GPIO.BOARD) # Use mode as BOARD for wiring simplicity

# Inputs
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Barrier A - Sensor 1, Warehouse Side
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Barrier A - Sensor 2, Passage Side
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Remote Entrance A Button 1
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Remote Entrance B Button 2
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Turnstile A - Entry Button
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Turnstile A - Exit Button
GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Barrier B - Sensor 1, Warehouse Side
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Barrier B - Sensor 2, Passage Side
GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Turnstile B - Entry Button
GPIO.setup(35, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Turnstile B - Exit Button
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Reset Button
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Turnstile A Signal
GPIO.setup(38, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Tunrstile B Signal

# Outputs - output to relays, activate turnstiles and barriers
GPIO.setup(8, GPIO.OUT) # Turnstile A - Inwards Movement
GPIO.setup(10, GPIO.OUT) # Turnstile A - Outwards Movement
GPIO.setup(12, GPIO.OUT) # Barrier A - Activation
GPIO.setup(16, GPIO.OUT) # Barrier B - Activation
GPIO.setup(18, GPIO.OUT) # Turnstile B - Inwards Movement
GPIO.setup(22, GPIO.OUT) # Turnstile B - Outwards Movement
#GPIO.setup(37, GPIO.OUT) # Pedestrian in Warehouse Light
#GPIO.setup(38, GPIO.OUT) # Forklift in Warehouse Light

try:
    print("Please press the reset button to set the system to its default state.\n",
          "Refer to the help guide for usage information. Press Ctrl+C to Quit.\n")

    # Inital Setups
    global forklift_count
    global person_count
    global barrier_remote_pressed_a
    global barrier_remote_pressed_b
    global tsA_signal
    global tsB_signal
    forklift_count = 0
    person_count = 0
    barrier_remote_pressed_a = False
    barrier_remote_pressed_b = False

    GPIO.output(8, 1)
    GPIO.output(10, 1)
    GPIO.output(12, 1)
    GPIO.output(16, 1)
    GPIO.output(18, 1)
    GPIO.output(22, 1)    

    while True:

        # Input buttons and sensors
        tsA_entry_button = GPIO.input(11) # Turnstile A Entry Button
        tsA_exit_button = GPIO.input(13) # Turnstile A Exit Button
        vbA_sensor_one = GPIO.input(15) # Barrier A Sensor 1
        vbA_sensor_two = GPIO.input(19) # Barrier A Sensor 2
        vbA_remote_b1 = GPIO.input(21) # Remote Button Linked to Barrier A
        vbB_remote_b2 = GPIO.input(23) # Remote Button Linked to Barrier B
        vbB_sensor_one = GPIO.input(29) # Barrier B Sensor 1
        vbB_sensor_two = GPIO.input(31) # Barrier B Sensor 2
        tsB_entry_button = GPIO.input(33) # Turnstile B Entry Button
        tsB_exit_button = GPIO.input(35) # Turnstile B Exit Button
        reset_button = GPIO.input(40) # Reset Counts Button
        tsB_signal = GPIO.input(37)
        tsA_signal = GPIO.input(38)

        # Turnstile A Entry Pressed
        if tsA_entry_button == False:            
            turnstile_entry_button("A")
            time.sleep(3.5)

        # Turnstile B Entry Pressed
        if tsA_exit_button == False:
            turnstile_exit_button("A")
            time.sleep(3.5)


        # Barrier A Remote Button Pressed
        # Check to prevent spam inputs from remote
        if vbA_remote_b1 == False and barrier_remote_pressed_a == False:            
            print("Remote Button Linked to Vehicle Barrier A Pressed")
            GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Vehicle Barrier A - Sensor 1
            GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Vehicle Barrier A - Sensor 2            
            launch_thread('A')
            time.sleep(1.5)

        # Barrier B Remote Button Pressed
        # Check to prevent spam inputs from remote

        if vbB_remote_b2 == False and barrier_remote_pressed_b == False:
            print("Remote Button Linked to Vehicle Barrier B Pressed")
            GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Vehicle Barrier B - Sensor 1
            GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Vehicle Barrier B - Sensor 2            
            launch_thread('B')
            time.sleep(1.5)
        ###############################################################
        # Turnstile B Entry Pressed
        # Part to try and fix 
        if tsB_entry_button == False:            
            triggered = False
            if check_forklift_count() == 0:
                timeout1 = time.time() + 20
                while True:
                    if time.time() > timeout1:
                        print("turnstile not activated in time")
                        break
                    if triggered == False: # Works
                        GPIO.output(18, 0)
                        time.sleep(2)
                        GPIO.output(18, 1)
                        print("Turnstile B - Triggered Enter Warehouse")
                        triggered = True
                    if tsB_signal == False: # Never executes so noone ever counted in/out
                        print("Signal received")
                        person_count += 1
                        print("Person Count = %s" % check_person_count())
                        break
            else:
                print("Turnstile B - There are forklifts %s in the warehouse or there are forklifts entering the warehouse" % check_forklift_count())
                break

        # Turnstile B Exit Pressed
        if tsB_exit_button == False:            
            turnstile_exit_button('B')
            time.sleep(3.5)


        # Reset Button pressed - changed for testing
        if reset_button == False:
            abc()
            #person_count = 0
            #forklift_count = 0
            #check_lights()
            #print("Reset button pressed")
            #print("Person Count:",check_person_count())
            #print("Forklift Count:",check_forklift_count())'''

            time.sleep(0.5)

        '''if tsA_signal == False:
            print("a")
        if tsB_signal == False:
            print("b")'''

except KeyboardInterrupt:
    print("Program exiting\nCleaning up GPIO Inputs")
    GPIO.cleanup()

This is the part of the program that is causing me issues. What the above code does is it detects that a turnstile entry button has been pressed, checks to see that the warehouse is clear of forklifts, activates the turnstile if there are no forklifts, and then it waits for a signal from the turnstile to ensure that the person has actually entered before changing the person count.

The awaiting signal from turnstile part below is the part that does not work. tsB_signal never goes to 1 or True even when it has voltage and a signal has been sent.

# Never goes false so never runs
if tsB_signal == False: # Never executes even when signal sent
   print("Signal received")
   person_count += 1
   print("Person Count = %s" % check_person_count())
   break

If I do the below statement inside the main while True loop and send a signal it is detected without issue so I know this is not a wiring issue.

# Goes false without issue when signal sent prints that it has been received
if tsB_signal == False: 
   print("Signal Received")
   sleep(0.2)

If anyone has any ideas or suggestions what could be causing the signal to be detected within the first while loop but not the second it would be hugely appreciated.

  • 1
    I see no evidence that tsB_entry_button is ever set. If it is we need the full context. – joan Jun 19 '18 at 4:46
  • Hi tsB_entry_button is set I just omitted it and it is detected when pressed. I have updated the original post now with everything minus the functions that are working correctly and unrelated to this issue. – Russ Jun 19 '18 at 6:26
  • Have you tried changing the if statement that does not execute from == False to == True just to confirm your code is even getting this far? – linhartr22 Jun 20 '18 at 1:19
0

The line tsB_signal = GPIO.input(37) is not like a C define, it does not declare that tsB_signal is a function that reads the GPIO attached to board pin 37.

It is an assignment of the value when the script reaches that point.

Perhaps you need to read the GPIO again?

  • Hi thanks very much for your thoughts and that would make sense. I'm a bit confused as to how you mean read the GPIO again, what exactly do you mean by that? Thanks again – Russ Jun 19 '18 at 21:04
  • If you want to know the current value of a GPIO you need to read the GPIO (i.e. call a function which tests if the GPIO has a high or low voltage). Add the line tsB_signal = GPIO.input(37) whenever you need to know the current value of GPIO37. – joan Jun 19 '18 at 21:18
  • @joan I'm pretty sure he is refreshing the value of the tsB_signal variable since he is reading all the other inputs in the same way. – linhartr22 Jun 20 '18 at 1:16
  • I'm not sure how to make the statement any clearer. You need to read the GPIO to know the current value. The variable will not magically update. Look at the loops. – joan Jun 20 '18 at 3:53

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.