18 Replies Latest reply on Nov 15, 2018 3:25 PM by jack.chaney56

    PiFace input delays

    jmac67

      I have a PiFace on a RaspberryPi, and it works great, but there is a several second delay between when the input switches to when the script responds. Can someone tell me where to find/change this delay time? Thanks, Jim

        • Re: PiFace input delays
          Jan Cumps

          How are you talking to the PiFACE? I'm using C++ and the effect is almost instant.

          Here is my code: SCPI on a Linux Board - Part 2b: PiFace Digital C++ programming

           

          Edit: here's an example that supports interrupts to react real time on input changes: https://github.com/piface/libpifacedigital/blob/master/example.c

          2 of 2 people found this helpful
            • Re: PiFace input delays
              jmac67

              I'm using a Python script to run a simple routine when an input triggers, but the delay that I don't want, makes it not work for what I need it to do. I'm not very good at this programming, but I don't see anything that would cause it to have this lag. Thanks, Jim McNamee

                • Re: PiFace input delays
                  Jan Cumps

                  Ah. I have no experience with Python script.

                  • Re: PiFace input delays
                    jack.chaney56

                    I have noticed a small delay when using Python, particularly when performing serial communications. I presumed it was primarily due to running an interpreter instead of actual machine instructions, but I never did that investigation.  I use Python for determining if an procedure or algorithm will provide me with what I want, because it is well structured enough and the write-run-test loop is very quick.  Almost always, after I get things the way I want, I switch over to C to get performance levels up. What's fun is in many cases it only requires putting semi colons on the end of the lines.

                     

                    Jack

                • Re: PiFace input delays
                  shabaz

                  Hi Jim,

                   

                  Unfortunately there's not enough information to help in a good way. If you could share your code it will help (ideally using syntax highlighting as shown here: How to Ask Questions using the Create Discussion tool   because Python uses spaces, and it will come out as a mess to the reader unless monospaced).

                  Also, if you can explain what the code is to be used for, there may be a better solution based on context. For instance, if you're trying to activate (say) a camera shutter for capturing a balloon burst, then that requires a very different solution than activating a camera or alarm on intruder detection (this is just an example).

                  Without seeing your code, it is not possible to tell if there is a deliberate or resolvable delay that can be changed in the code (and if the minimal delay will be sufficient for your needs), or if a different solution is needed.

                  Right now it is not possible to tell if you need a script to execute and perform the unknown response within 10msec, or 1msec, or less.

                  • Re: PiFace input delays
                    jomoenginer

                    There are multiple PiFace products, so which PiFace device are you using?  

                     

                    I've used multiple PiFace devices in a previous project that used Python and I don't remember any unnecessary delays when talking to the boards, even from a remote system using a web browser interface.

                     

                    If possible, post the code you are using either an attachment or pasted in the syntax highlighting option.

                     

                    It is possible that there is something else going on with your config, so you may want to check the output of the 'dmesg' command to see if there are any conflicts with the PiFace device.

                      • Re: PiFace input delays
                        jmac67

                        What I have made is a Mirror Prop for a haunted house. It is a two-way mirror in a picture frame, with a flat panel LCD mounted behind the glass. There is a photo eye running a 12VDC relay triggering an input on the PiFace Digital2 IO Board on a Raspberry Pi. It shows a blackscreen JPEG until the eye is read, then it plays an audio clip and a scary image is then displayed on the monitor. The way it is now, if the person doesn't stand in front of the mirror for 2-3 seconds, it does nothing. I've checked the eye and relay, and know they are basically instantaneous in triggering. I will post the PYTHON code below. I don't know this language well, and used stuff I found on various websites for what I have.

                         

                        import pifacedigitalio

                        pifacedigitalio.init()

                        import pygame.sys

                        pygame.init()

                        pygame.mixer.init()

                        pygame.mixer.music.load("/media/inhale_scream2.mp3")

                        screen=pygame.display.set_mode((1280,1024),pygame.NOFRAME)

                        images=[]

                        images.append(pygame.image.load("media/BLACKSCREEN.jpg"))

                        images.append(pygame.image.load("media/creepyclown3.jpg"))

                        show=0

                        screen.blit(images[show], (0,0))

                        pygame.display.update()

                        pygame.time.delay(1)

                         

                        while True:

                             input_state0 = pifacedigitalio.digital_read(0)

                             input_state3 = pifacedigitalio.digital_read(3)

                             input_slate2 = pifacedigitalio.digital_read(2)

                             if input_state0==1:

                                  pifacedigitalio.digital_write(0,1)

                                  pygame.mixer.music.play()

                                  show=(show+1) % len(images)

                                  screen.blit(images[show], (0,0))

                                  pygame.display.update()

                                  while pygame.mixer.music.get_busy()==True:

                                       continue

                             else :

                                  pygame.time.delay(5750)

                                  show= % len(images)

                                  screen.blit(images[0], (0,0))

                                  pygame.display.update()

                                  pifacedigitalio.digital_write(0,0)

                         

                             if input_state2==1:

                                  pygame.quit()

                                  sys.exit()

                         

                             if input_state3==1:

                                  pygame.quit()

                                  sys.exit()

                             pygame.time.delay(10)

                             show=0

                          • Re: PiFace input delays
                            Jan Cumps

                            it looks that your while loop is introducing the delay. the code is not interface driven.

                             

                            You are running the loop and polling the status of the input. my bet is that reading the 3 input states is very fast. the rest of your loop, including 5 second delay,waiting for the audio mixer to complete, drawing, ..., hold up the loop.

                              • Re: PiFace input delays
                                jmac67

                                What would be a better way of polling the input for state change, and react when it goes high? I used this code from someone else's project for making a slideshow, and the input was to trigger the next image, then found the audio file info elsewhere and put it all together. I figured this wasn't the best way of doing this, but I'm not a programmer by trade, just an electronic tech/industrial electrician. This is hobby for me, building haunted house props. I usually use small PLCs or Arduinos, but I wanted to be able to work with video, so thought the Raspberry Pi would be good for this, but there's a lot to getting it ready to even function, especially when adding the PiFace.

                                  • Re: PiFace input delays
                                    jomoenginer

                                    When you post code, please use the ">>" insert symbol at the top of the edit screen, select Syntax Highlighting and select the language you are posting, or just Plan if unsure. 

                                     

                                    Uh, so you are not that familiar with Python and found some code on the web and just pasted it into your code hoping it works without issue and then blame the PiFace Digital2 IO Board? Okay.

                                     

                                    Looking at the code, in the else block you have "show= % len(images)".   Are you missing a (show+{num}) before the "%" cause I am not sure what it does otherwise since you are running the modulo on nothing.

                                     

                                    Also, your "pygame.time.delay(5750)" delay is like a 5sec delay, so if there is no input on pin 0 of the PiFace the loop will pause for 5 seconds before it does anything else.  Is that expected?

                                      • Re: PiFace input delays
                                        jmac67

                                        I'm not blaming anything, I just wanted help in removing the delay which caused it to not work consistently. People don't stand at a mirror for very long in a haunted house, so many miss the scare. I think the pygame.time.delay(5750) was intended to be a delay between the scare and the system being ready for the next event trigger, (at least that's what I want it to do), so it won't keep running if the eye is triggered again right away.

                                      • Re: PiFace input delays
                                        Jan Cumps

                                        I think this is the peace of code that makes everything slow (if the image loading is fast and nhale_scream2.mp3 is a short sound):

                                         

                                        pygame.time.delay(5750)

                                        As long as the button is not pressed, the program repeats pausing for 5+ seconds.

                                         

                                        (I think your code may also miss some key presses because you have to have the luck to have the key down just when the pifacedigitalio.digital_read() is executed)

                                         

                                        In your state ==0 part, Instead of pausing, you could use a variable that is increased every loop (declare the variable in the start of your code, update it with one just after your delay(10) at the loop end)..

                                        Remove the delay(5750) line

                                        I expect that a loop without your 5750 delay line would take approx 10 ms, because there's a loop delay of that time at the end.

                                        Once that  variable has reached 575 or more, execute the code in the else part, and reset the counter to 0.

                                        Also reset the counter to 0 in the part where the state == 1. That will reset the timer after someone pressed a button.

                                          • Re: PiFace input delays
                                            jmac67

                                            The mp3 is around 10 seconds long, but I will try what you have suggested and see how it works. Thanks!

                                              • Re: PiFace input delays
                                                Jan Cumps

                                                After reading your last comment (https://www.element14.com/community/message/247865/l/re-piface-input-delays#247865 ) I'm not sure if I understand well what your code wants to do.

                                                 

                                                Can you draw the scenario of what you try to achieve when no one presses a button (application in rest) , and when someone presses a button?

                                                  • Re: PiFace input delays
                                                    jmac67

                                                    The idea is the display has a black screen (blackscreen.jpg) so the mirror has the person's reflection on it.

                                                         This is what it does until the eye is triggered by somebody, so a small delay is ok, just not how long it is currently.

                                                    When the input goes high(the eye is triggered), I want the display to switch to the creepyclown.jpg image and the mp3 file plays until it is done.

                                                         I am also turning off a light with the relay output at the same time.

                                                    Once the audio file is done, I want it to reset back to the black screen and wait for the next person to trigger the eye.

                                                     

                                                    Everything actually works, surprisingly, except the delay that I inadvertently used improperly, causes it to wait for several seconds once the eye is triggered, to display the image and play the audio file.

                                                    And because of this mistake, the eye only does the True routine when it is held high for those several seconds, which is not how I want it. I will try to just change the delay time and see what that does.

                                                      • Re: PiFace input delays
                                                        jmac67

                                                        Well, I found out that whatever value I put in the else: pygame.time.delay(####) is how long the eye must be triggered before the True event starts. Does that make sense? I guess it will work now that I lowered the value, but I don't see why that effects the script when the input is true, and not just when it is not true.

                                                          • Re: PiFace input delays
                                                            jomoenginer

                                                            Python is a space based language so it's best to ensure the spacing is correct between blocks and that either spaces or tabs are used consistently; do not mix them however I prefer spaces since different editor set tabs to different space length.   Since you pasted your code as without Syntax Highlighting its not possible to see if this is an issue.

                                                             

                                                            Also, you might want to try to initialize the PiFace either before reading the device or at the end of your while loop to ensure the PiFace is at a known state before reading it. Just add what you had at the top of the code:

                                                            pifacedigitalio.init()

                                                             

                                                             

                                                            For the Remote Horse Feeder project I worked on sometime back, I had this note in one of the blog posts regarding the Digital 2:

                                                            [Pi IoT] -  Remote Horse Feeder System #6 : OpenHAB, Pi Face and MQTT

                                                            Motion Sensor to Pi Face Digital 2

                                                            The sensor I used for the Motion Sensing is the Parallax PI Sensor rev B.  This is connected to the Pi Face Digital 2 Input 3, 5V and Ground. (NOTE: Since the Digital 2 Inputs are by default pulled up to 5V, I ended up having to put a 10K resistor between GND and Pin 3 to pull the pin low when the PIR sensor was on.)
                                                            

                                                             

                                                            Perhaps when your sensor is on, it is pulling the pin low on the Digital 2 thus going to the else statement.  Try changing the check for if input_state0 to:

                                                             

                                                            if input_state0==0:
                                                          • Re: PiFace input delays
                                                            Roger Wolff

                                                            The structure of such a program would be something like:

                                                              while true

                                                                if eye signal active then

                                                                    do whatever needs to be done.

                                                                    while eye signal active

                                                                       do  nothing  / wait.

                                                                     end while

                                                               end if

                                                               // other stuff that may need checking/doing. But don't take too long!

                                                               sleep a short time.

                                                            end while

                                                             

                                                            For debugging you can add "print something" to the "other stuff" That way you can see how fast it runs through the loop. Sleep a short time, 10ms is reasonable, but will have your terminal window scrolling like mad. For debugging I prefer having it run a bit (10 to 50 times) slower. Currently working on a project with exaclty the same. In production the reporting will be done every 10ms, 100x per second. Now for debugging I prefer having it a bit slower.