Python Forum

Full Version: C++ to Python conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
You can greatly simplify your code:
import time

GPIO.setmode(GPIO.BCM)

pinlist = [2,3,4,17,27,22,10,9]

GPIO.setup(pinlist, GPIO.OUT)
GPIO.output(pinlist,GPIO.HIGH)

def main():
   port_sequence = {
       'Rounds': [2, 3, 4, 17, 27, 22, 10, 9],
       'Queens': [22, 4, 27, 10, 3, 17, 22, 9],
       'RQueens': [10, 27, 4, 2, 3, 17, 22, 9],
       'Titums': [2, 27, 3, 22, 4, 10, 17, 9]
   }

   SleepA = 0.2
   SleepB = 1

   def play_tune(name):
       print(name)
       for val in port_sequence[name]:
           GPIO.output(val, GPIO.LOW)
           time.sleep(SleepA);
           GPIO.output(val, GPIO.HIGH)
           time.sleep(SleepA);
       GPIO.cleanup()

   for name in ['Rounds','Queens','RQueens','Titums']:
       play_tune(name)
   count = 0

   while count < 2:
       random.choice(Tune)()
       count += 1

       if count > 2:
           break

main()
I am unable to test
Larz60+ you little beauty!! ive not tested it yet, but it looks pretty good!!
Hello,

It didnt like the shortened version of code but it is someything i will come back to onc ei have it all up and running.

My next problem is..... The code below now runs the first loop but then comes up with the error

Error:
Traceback (most recent call lat): File  "/home/pi/DoorBell/py", line 167, in <module> random.choice(Tune)() File "home/pi/DoorBell.py", line 20, in Rounds GPIO.output (2, GPIO.LOW) Runtime Error: please set pin numbering mode using GPIO.setmod (GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
import random
import RPi.GPIO as GPIO
import time

try:
    
    GPIO.setmode(GPIO.BCM)

    pinlist = [2,3,4,17,27,22,10,9]

    GPIO.setup(pinlist, GPIO.OUT)
    GPIO.output(pinlist,GPIO.HIGH)

    SleepA = 0.2
    SleepB = 2
    
    def Rounds():
        print ("Rounds")
        GPIO.output(2, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(2, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.cleanup()
       

    def Queens():
        print ("Queens")
        GPIO.output(2, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(2, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.cleanup()

    def RQueens():
        print ("RQueens")
        GPIO.output(10, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(2, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(2, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.cleanup()
       
    def Titums():
        print ("Titums")
        GPIO.output(2, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(2, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(27, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(3, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(22, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(4, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(10, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(17, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.LOW)
        time.sleep(SleepA);
        GPIO.output(9, GPIO.HIGH)
        time.sleep(SleepA);
        GPIO.cleanup()
        
    Tune = [Rounds,Queens,RQueens,Titums]
    count = 0

    while count < 2:
        random.choice(Tune)()
        time.sleep(SleepB)
        count += 1

        if count > 2:

            break

except KeyboardInterrupt:
    print("Quit")

    GPIO.cleanup
If the code has run once already with no problems why does it then tell me i havent set the GPIO's up properly?
Pages: 1 2