11 Replies Latest reply on Nov 27, 2016 2:23 PM by urkraft

    Python 3 code Question

    ellip

      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

      • Reply
        • Re: Python 3 code Question
          clem57

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

          • Re: Python 3 code Question
            jdlui

            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')
            
            • Re: Python 3 code Question
              ellip

              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. 

                • Re: Python 3 code Question
                  urkraft

                  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)

                  • Re: Python 3 code Question
                    dougw

                    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()

                  • Re: Python 3 code Question
                    urkraft

                    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!

                      • Re: Python 3 code Question
                        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)

                         

                          • Re: Python 3 code Question
                            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)

                             

                            1 of 1 people found this helpful
                          • Re: Python 3 code Question
                            dougw

                            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.