#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Help to run program over and over every 2 seconds


    Hi everyone, I am still a new coder so please excuse any rudimentary mistakes or questions I ask. I have some code below that does work and run. However, I need this program to go back and run again, and again, every 2 seconds.

    I tried doing some things but they only repeated the the outcome of what happened the first time it ran. I need it to go back and actually run the program again, because the results could (and should) be different.

    Code:
    import piface.pfio as piface
    #turn on piface
    ###I believe I also need to import time here###
    
    piface.init()
    #initialize PiFace
    
    #read switch state
    switch0 = piface.digital_read(0)
    switch1 = piface.digital_read(1)
    switch2 = piface.digital_read(2)
    switch3 = piface.digital_read(3)
    switch4 = piface.digital_read(4)
    switch5 = piface.digital_read(5)
    switch6 = piface.digital_read(6)
    switch7 = piface.digital_read(7)
    
    ##I believe this is where I need code to begin how
    ##to run again/repeat starting here
    
    #show swich state
        print switch0
        if switch0 > 0:
            piface.digital_write(0,1)
        
        print switch1    
        if switch1 > 0:
            piface.digital_write(1,1)
        
        print switch2    
        if switch2 > 0:
            piface.digital_write(2,1)
        
        print switch3    
        if switch3 > 0:
            piface.digital_write(3,1)
        
        print switch4    
        if switch4 > 0:
            piface.digital_write(4,1)
        
        print switch5    
        if switch5 > 0:
            piface.digital_write(5,1)
        
        print switch6    
        if switch6 > 0:
            piface.digital_write(6,1)
        
        print switch7    
        if switch7 > 0:
            piface.digital_write(7,1)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    Your mistake is where you think the code for repeating needs to go. Look at your own comments again. If you're only repeating the part where you "show switch state", why are you expecting anything to change?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    You would use time.sleep inside a while() or for() loop. The following is pseudo-code since we don't know exactly what is happening.
    Code:
    ##while True:     ## infinite loop
    for ctr in range(10):     ## 10 is enough to begin testing with
        #read switch state
        switch_results = []
        for ctr in range(8):
            switch_state = piface.digital_read(ctr)
            ## you don't need to store the state if you don't use it later
            switch_results.append(switch_state)
            if switch_state > 0:
                piface.digital_write(ctr,1)
    
        print switch_results     ## for testing
        time.sleep(2.0)
    Last edited by dwblas; June 5th, 2013 at 10:42 PM.

IMN logo majestic logo threadwatch logo seochat tools logo