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
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum PiFace input delays
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 18 replies
  • Answers 1 answer
  • Subscribers 667 subscribers
  • Views 2542 views
  • Users 0 members are here
  • rasberry_pi_3_b_plus
  • raspberry_pi
Related

PiFace input delays

jmac67
jmac67 over 6 years ago

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

  • Sign in to reply
  • Cancel
Parents
  • jomoenginer
    0 jomoenginer over 6 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to jomoenginer

    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

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 6 years ago in reply to jmac67

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to Jan Cumps

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jomoenginer
    0 jomoenginer over 6 years ago in reply to jmac67

    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?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 6 years ago in reply to jmac67

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to jomoenginer

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to Jan Cumps

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

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 6 years ago in reply to jmac67

    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?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to Jan Cumps

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jmac67
    0 jmac67 over 6 years ago in reply to 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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • jmac67
    0 jmac67 over 6 years ago in reply to 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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • jomoenginer
    0 jomoenginer over 6 years ago in reply to jmac67

    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:

    • 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