Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ to Python conversion
#11
http://raspi.tv/2013/rpi-gpio-basics-3-h...ct-your-pi
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#12
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
Reply
#13
Larz60+ you little beauty!! ive not tested it yet, but it looks pretty good!!
Reply
#14
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I need to add data types to cython conversion python to c Good_AI_User 1 973 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,415 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  C to Python code conversion print problem anakk1n 1 2,137 May-22-2020, 04:15 PM
Last Post: deanhystad
  Python uppercase conversion conditions Jaypeng 7 2,893 Apr-29-2020, 11:24 AM
Last Post: jefsummers
  MATLAB to Python conversion stokd 10 4,920 Jan-19-2020, 09:14 PM
Last Post: stokd
  Excel Model Conversion into Python line7 2 2,224 Oct-09-2019, 07:51 AM
Last Post: line7
  python opencv grayscale conversion error Spandora 1 9,524 May-26-2019, 10:43 AM
Last Post: heiner55
  Vba conversion to Python stranger14u 1 3,538 May-26-2019, 08:01 AM
Last Post: heiner55
  Conversion of Time Duration in seconds in python jdevansh99 0 2,829 Jun-05-2018, 05:12 PM
Last Post: jdevansh99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020