element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Python 3 code Question
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 12 replies
  • Answers 3 answers
  • Subscribers 51 subscribers
  • Views 2608 views
  • Users 0 members are here
  • Code Exchange
Related

Python 3 code Question

ellip
ellip over 8 years ago

Hi.  I am new to the Raspberry Pi.  I have a Raspberry Pi 3 and a PiFace 2.  I am trying to update a Christmas decoration made over 40 years ago by my father. It was run by a rotisserie motor and a wooden cylinder with copper strips and screws to make contact and light the stars up in sequences.

 

I have 5 stars in the following formation:

 

     1          2

 

          5

 

     4          3

 

I am trying to have the stars light up in a certain sequence with a small delay in between each function

 

1

2

3

4

5

1,2,3

2,3,4

3,4,1

4,1,2

1,2,5

2,3,5

3,4,5

4,5,1

1,2,3,4,5

then begin the sequence again.

I have tried following as many of the posts as I can and receive errors on everything from syntax to int and tuples.

I am sure that it is something that is simple to the rest of you but just eludes me.  Any help in any way will be greatly appreciated.

Elli

  • Sign in to reply
  • Cancel

Top Replies

  • jdlui
    jdlui over 8 years ago +3 suggested
    Hi Ellen, Suggestion: With any problem I think it's helpful try and break things into pieces. Share more info with us on what you've tried, what isn't working, what code you are using. I.E. Have you tested…
  • mgillett
    mgillett over 7 years ago in reply to dougw +3
    I appreciated the two of you demonstrating two solutions to this problem. Further I appreciate seeing the first examples of going line by line through the code then the "better" coding style making the…
  • urkraft
    urkraft over 8 years ago in reply to urkraft +1 suggested
    To Douglas Wong: Sorry, i see now that your post was not directed to me. Was not very focused when i read it. I did notice that there was a very important bug in my code in that it did not handle interrupts…
  • clem57
    0 clem57 over 8 years ago

    Create an array with the options you wish like  http://www.thegeekstuff.com/2013/08/python-array/

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ellip
    0 ellip over 8 years ago in reply to clem57

    Is there a way to "dumb" this down....I am a new "newbie" and haven't a lot of experience with Python....sorry if this is too ridiculous.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jdlui
    0 jdlui over 8 years ago

    Hi Ellen,

     

    Suggestion: With any problem I think it's helpful try and break things into pieces. Share more info with us on what you've tried, what isn't working, what code you are using.

    I.E. Have you tested a basic python script to get PiFace to turn on a single light yet? Confirm that this is working before you proceed. Test it on a random light like an LED. Then test it on your ornament.

     

    Hardware - PiFace

    I've never worked with PiFace so I don't know what your hookup options are to your ornament. I assume you have already made your electrical connections? Completed to the point where you can turn on a light if you run some basic blink script? There seems to

     

     

    Code - The Loop

    What code have you tried so far? Share it and we can help you more easily.

    Here is an example code that will look through your lighting combinations. This will go through each lighting combination and wait 1 second in between. Try running it and see how it works. You can modify the wait time as well.

     

    Hope this helps!

     

    J

     

    import time
    # Example of a script to cycle through and change light sequence
    
    
    # Every lighting combination can be summarized as a '1' for an on light and an '0' for an off light.
    # Those combinations are shown below.
    lights = ['10000','01000','00100','00010','00001','11100','01110','10110','11010','11001','01101','00111','10011','11111']
    
    
    # Define wait time for our function
    waittime = 1 # Number of seconds between light sequences, measured in seconds.
    
    
    # We want this script to run perpetually so we create a while True loop
    while True:
      # We iterate through each item in our list of light combinations, which is called 'lights'
      for index,item in enumerate(lights):
      # Putting a bunch of text print outs so you can see how the loop iterates
      print('We are now printing combination number',index+1,'in our light sequence')
      print('Light combination is',item)
    
      # Here is where you would send the data to the PiFace to control its lighting.
      print('If we send these commands to a lighting system \nlight 1 is',item[0],'light 2 is',item[1],'light 3 is',item[2],'light 4 is',item[3],'light 5 is',item[4],'\n')
      # Insert some lighting command here. Use the on/off data stored in the item variable to turn your lights on or off.
    
    
    
    
      # Now we use time command to put a delay in between lighting instruction
      time.sleep(waittime)
      print('Completed all sequences. repeating')

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • ellip
    0 ellip over 8 years ago

    Ok...thank you for your reply

     

    i have the 5 stars (1-5) wired to the pi/piface.  They light up in sequence 5,4,3,2,1 then shut off in the same sequence 5,4,3,2,1,.

    This is the code i used

     

    from time import sleep

    import pifacedigitalio

     

    DELAY = 0.5  # seconds

     

    if __name__  ==  "__main__":

         pifacedigital = pifacedigitalio.PiFaceDigital()

         while True:

              pifacedigital.leds[5].toggle

              sleep(DELAY)

              pifacedigital.leds[4].toggle()

              sleep(DELAY)

              pifacedigital.leds[3].toggle()

              sleep(DELAY)

              pifacedigital.leds[2].toggle()

              sleep(DELAY)

              pifacedigital.leds[1].toggle

              sleep(DELAY)

     

    the lights will sequence until i stop them. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • urkraft
    0 urkraft over 8 years ago in reply to ellip

    This code appears to give the specified results:

     

    #!/usr/bin/env python3

    import pifacedigitalio, time

     

    pfd = pifacedigitalio.PiFaceDigital()

     

    while True:

         pfd.output_port.value = 0x02

         time.sleep(0.5)

         pfd.output_port.value = 0x04

         time.sleep(0.5)

         pfd.output_port.value = 0x08

         time.sleep(0.5)

         pfd.output_port.value = 0x10

         time.sleep(0.5)

         pfd.output_port.value = 0x20

         time.sleep(0.5)

         pfd.output_port.value = 0x0E

         time.sleep(0.5)

         pfd.output_port.value = 0x1C

         time.sleep(0.5)

         pfd.output_port.value = 0x1A

         time.sleep(0.5)

         pfd.output_port.value = 0x16

         time.sleep(0.5)

         pfd.output_port.value = 0x26

         time.sleep(0.5)

         pfd.output_port.value = 0x2C

         time.sleep(0.5)

         pfd.output_port.value = 0x38

         time.sleep(0.5)

         pfd.output_port.value = 0x32

         time.sleep(0.5)

         pfd.output_port.value = 0x3E

         time.sleep(0.5)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dougw
    0 dougw over 8 years ago in reply to ellip

    It sounds like you have everything working, just a bit of code modification to get the right sequence.

    The main thing in your sequence is to toggle the devices you want on and then after a delay toggle the same devices off before toggling the next set on:

     

         while True:

              pifacedigital.leds[1].toggle

              sleep(DELAY)

              pifacedigital.leds[1].toggle

              pifacedigital.leds[2].toggle()

              sleep(DELAY)

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

                sleep(DELAY)

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

                sleep(DELAY)

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

                sleep(DELAY)

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[1].toggle

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

                sleep(DELAY)

              pifacedigital.leds[1].toggle

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[2].toggle

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

                sleep(DELAY)

              pifacedigital.leds[2].toggle

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[3].toggle

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[1].toggle()

                sleep(DELAY)

              pifacedigital.leds[3].toggle

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              sleep(DELAY)

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[5].toggle()

              sleep(DELAY)

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[5].toggle()

              sleep(DELAY)

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

              sleep(DELAY)

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[1].toggle()

              sleep(DELAY)

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

              sleep(DELAY)

              pifacedigital.leds[1].toggle()

              pifacedigital.leds[2].toggle()

              pifacedigital.leds[3].toggle()

              pifacedigital.leds[4].toggle()

              pifacedigital.leds[5].toggle()

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • urkraft
    0 urkraft over 8 years ago

    To Douglas Wong:

     

    Strange, the code resulted in the exact sequence specified. If you had bothered to test the code that i published you would have seen that it did actually produce the specified sequence - and with much less code than the gobeltygoop you posted to "correct" me!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • urkraft
    0 urkraft over 8 years ago in reply to urkraft

    To Douglas Wong:

    Sorry, i see now that your post was not directed to me. Was not very focused when i read it. I did notice that there was a very important bug in my code in that it did not handle interrupts and could easily enter an undesirable state when stopped. A better solution to my submission follows:

     

    #!/usr/bin/env python3

    import pifacedigitalio, time, sys

     

    pfd = pifacedigitalio.PiFaceDigital()

     

    try:

         while True:

              pfd.output_port.value = 0x02

              time.sleep(0.5)

              pfd.output_port.value = 0x04

              time.sleep(0.5)

              pfd.output_port.value = 0x08

              time.sleep(0.5)

              pfd.output_port.value = 0x10

              time.sleep(0.5)

              pfd.output_port.value = 0x20

              time.sleep(0.5)

              pfd.output_port.value = 0x0E

              time.sleep(0.5)

              pfd.output_port.value = 0x1C

              time.sleep(0.5)

              pfd.output_port.value = 0x1A

              time.sleep(0.5)

              pfd.output_port.value = 0x16

              time.sleep(0.5)

              pfd.output_port.value = 0x26

              time.sleep(0.5)

              pfd.output_port.value = 0x2C

              time.sleep(0.5)

              pfd.output_port.value = 0x38

              time.sleep(0.5)

              pfd.output_port.value = 0x32

              time.sleep(0.5)

              pfd.output_port.value = 0x3E

              time.sleep(0.5)

    except:

         pfd.output_port.all_off()

         pfd.deinit_board()

         print('\nExiting due to unexpected interrupt.')

         sys.exit(0)

     

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • urkraft
    0 urkraft over 8 years ago in reply to urkraft

    Since there is a lot of unnecessary repetition and generally bad programming practices in my previous posts on this topic, i have cleaned up my code a bit. This is my alternative solution:

     

    #!/usr/bin/env python3

    import pifacedigitalio, time, sys

     

    pause_time = 0.5

    the_sequence = [0x02, 0x04, 0x08, 0x10, 0x20, 0x0E, 0x1C, 0x1A, 0x16, 0x26, 0x2C, 0x38, 0x32, 0x3E]

     

    pfd = pifacedigitalio.PiFaceDigital()

     

    try:

         while True:

              for seq in the_sequence:

                   pfd.output_port.value = seq

                   time.sleep(pause_time)

    except:

         pfd.output_port.all_off()

         pfd.deinit_board()

         print('\nExiting due to unexpected interrupt.')

         sys.exit(0)

     

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • dougw
    0 dougw over 8 years ago in reply to urkraft

    sorry - I didn't see your code before posting. My code was not trying to be efficient, just an easy extrapolation of the working code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube