1

I'm designing a flow rate based traffic controller on raspberry pi using buttons as traffic simulators. the problem i'm facing is that the maximum value gets selected at first and there can be no increments possible to that value and the code runs in an infinite loop.

For ex if i press the button at road 1 on the circuit; it will take it as the maximum value as the count of other three roads viz. road two, road three, road 4 are zero and the loop continues with only the traffic lights at road 1 going green and red and the counts of the button presses at the other three streets are not considered at all. Please help me with the logic as i'm a newbie with python. Here's my code.

#!/usr/bin/python
import os
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
count = 0
count2 = 0
count3 = 0
count4 = 0
GPIO.setwarnings(False)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(25, GPIO.IN)
#red1
GPIO.setup(17,GPIO.OUT)
#yellow1
GPIO.setup(27,GPIO.OUT)
#green1
GPIO.setup(22,GPIO.OUT)
#RT1
GPIO.setup(12,GPIO.OUT)
#red2
GPIO.setup(14,GPIO.OUT)
#yellow2
GPIO.setup(15,GPIO.OUT)
#green2
GPIO.setup(18,GPIO.OUT)
#RT2
GPIO.setup(16,GPIO.OUT)
#red3
GPIO.setup(10,GPIO.OUT)
#yellow3
GPIO.setup(9,GPIO.OUT)
#green3
GPIO.setup(11,GPIO.OUT)
#RT3
GPIO.setup(24,GPIO.OUT)
#red4
GPIO.setup(2,GPIO.OUT)
#yellow4
GPIO.setup(3,GPIO.OUT)
#green4
GPIO.setup(13,GPIO.OUT)
#RT4
GPIO.setup(23,GPIO.OUT)


while True: 
    if (GPIO.input(20) == False):
        count=count+1
        time.sleep(1)
        print(count)
    if (GPIO.input(21) == False):
        count2=count2+1
        time.sleep(1)
        print(count2)
    if (GPIO.input(19) == False):
        count3=count3+1
        time.sleep(1)
        print(count3)
    if (GPIO.input(25) == False):
        count4=count4+1
        time.sleep(1)
        print(count4)
    if count > count2 :
        if count > count3:
            if count > count4:
                print ("Traffic on road 1 highest")
                #go go go...RT1+Str1
                GPIO.output(17,False)
                GPIO.output(27,False)
                GPIO.output(22,True)
                GPIO.output(12,True)
                GPIO.output(14,True)
                GPIO.output(15,False)
                GPIO.output(18,False)
                GPIO.output(16,False)
                GPIO.output(10,True)
                GPIO.output(9,False)
                GPIO.output(11,False)
                GPIO.output(24,False)
                GPIO.output(2,True)
                GPIO.output(3,False)
                GPIO.output(13,False)
                GPIO.output(23,False)
                time.sleep(10)
                #RT1 blinks
                GPIO.output(12,False)
                time.sleep(1)
                GPIO.output(12,True)
                time.sleep(1)
                GPIO.output(12,False)
                time.sleep(1)
                GPIO.output(12,True)
                time.sleep(1)
                GPIO.output(12,False)
                time.sleep(1)
                GPIO.output(27,True) #yellow
                time.sleep(3)
                GPIO.output(27,False)
                GPIO.output(17,True)
                time.sleep(1) #red

    elif count2 > count:
        if count2 > count3:
            if count2 > count4:
                print ("Traffic on road 2 highest")
                GPIO.output(11,False)
                GPIO.output(10,True)
                GPIO.output(18,True)
                GPIO.output(16,True)
                GPIO.output(14,False)
                GPIO.output(24,False)
                GPIO.output(17,True)
                GPIO.output(27,False)
                GPIO.output(22,False)
                GPIO.output(12,False)
                GPIO.output(15,False)
                GPIO.output(9,False)
                GPIO.output(11,False)
                GPIO.output(24,False)
                GPIO.output(2,True)
                GPIO.output(3,False)
                GPIO.output(13,False)
                GPIO.output(23,False)
                time.sleep(10)
                #RT2 blinks
                GPIO.output(16,False)
                time.sleep(1)
                GPIO.output(16,True)
                time.sleep(1)
                GPIO.output(16,False)
                time.sleep(1)
                GPIO.output(16,True)
                time.sleep(1)
                GPIO.output(16,False)
                time.sleep(1)
                GPIO.output(15,True)
                time.sleep(3)
                GPIO.output(15,False) #yellow
                time.sleep(1)
                GPIO.output(14,True) #red
                time.sleep(1)

    elif count3 > count:
        if count3 > count2:
            if count3 > count4:
                print ("Traffic on road 3 highest")
                GPIO.output(11,False)
                GPIO.output(10,False)
                GPIO.output(18,False)
                GPIO.output(16,False)
                GPIO.output(14,True)
                GPIO.output(24,False)
                GPIO.output(17,True)
                GPIO.output(27,False)
                GPIO.output(22,False)
                GPIO.output(12,False)
                GPIO.output(15,False)
                GPIO.output(9,False)
                GPIO.output(11,True)
                GPIO.output(24,True)
                GPIO.output(2,True)
                GPIO.output(3,False)
                GPIO.output(13,False)
                GPIO.output(23,False)
                time.sleep(10)
                #RT3 blinks
                GPIO.output(24,False)
                time.sleep(1)
                GPIO.output(24,True)
                time.sleep(1)
                GPIO.output(24,False)
                time.sleep(1)
                GPIO.output(24,True)
                time.sleep(1)
                GPIO.output(24,False)
                time.sleep(1)
                GPIO.output(9,True)
                time.sleep(3)
                GPIO.output(9,False) #yellow
                time.sleep(1)
                GPIO.output(10,True) #red
                time.sleep(1)
    elif count4 > count:
        if count4 > count2:
            if count4 > count3:
                print ("Traffic on road 4 highest")
                GPIO.output(11,False)
                GPIO.output(10,True)
                GPIO.output(18,False)
                GPIO.output(16,False)
                GPIO.output(14,True)
                GPIO.output(24,False)
                GPIO.output(17,True)
                GPIO.output(27,False)
                GPIO.output(22,False)
                GPIO.output(12,False)
                GPIO.output(15,False)
                GPIO.output(9,False)
                GPIO.output(11,False)
                GPIO.output(24,False)
                GPIO.output(2,False)
                GPIO.output(3,False)

                GPIO.output(13,True)
                GPIO.output(23,True)
                time.sleep(10)
                #RT2 blinks
                GPIO.output(23,False)
                time.sleep(1)
                GPIO.output(23,True)
                time.sleep(1)
                GPIO.output(23,False)
                time.sleep(1)
                GPIO.output(23,True)
                time.sleep(1)
                GPIO.output(23,False)
                time.sleep(1)
                GPIO.output(3,True)
                time.sleep(3)
                GPIO.output(3,False) #yellow
                time.sleep(1)
                GPIO.output(2,True) #red
                time.sleep(1)
  • You seem to have some debug printfs. Maybe you could post some sample output from the program. – XTL May 5 '15 at 19:18
0

I think the problem is that if (GPIO.input(20) == False) type reads happen instantly and time.sleep blocks. So the program spends its time in the sleep statements and after one branch is finished, it reads the buttons in a flash and goes into a branch where it sleeps some more. Unless you're holding down a button at the moment the read happens, there won't be a count. While the program sleeps, the buttons are ignored.

You could try holding a button down long enough for the next loop for a start.

You may need a slightly different approach than this loop for a real-time problem.

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.