简体   繁体   中英

Raspberry Pi input reading through endless while loop

I'm detecting a touch throuth a ttp223b touch sensor via python on a raspberry pi. It works pretty good but i need to wait for a second after a touch detection to prevent more than one execution so i just added a "time.sleep(1)".

The problem is that i also get multiple outputs, they are just time offset to 1 second, it seems that the routine is triggering multiple times at once.

import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)

GPIO.setup(3, GPIO.IN)


while 1:

  if GPIO.input(3) == GPIO.HIGH:
    print "touched :3"
    time.sleep(1)

Any suggestions how i could solve the issue?

add a sentinal

last_update = 0
while 1:
   if time.time() - last_update > 1.5 : #1 and a half seconds
          if GPIO.input(3) == GPIO.HIGH:
              print "touched :3"

this will allow the GPIO to continue flushing so you dont pick up the old press (at least I think im not entirely sure how GPIO buffers work here)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM