1

Looking for a simple input pulse counter for no more then 2000 counts per minute. Right now I have it set

if gpio.input(countswitch):
   data["count"] += 1

which works but I get 100 plus counts for each pulse I only want one count per pulse.

  • 1
    Not really Pi specific. You need to read the pin and only increment the count when it changes state from low to high, i.e. if the last read was high don't bump the count in the new read. – joan Jul 9 at 17:09
  • would you know of an example of how to set that up – uspc1 Jul 9 at 17:25
1

Here is a pigpio Python example. You use the same logic in any programming language.

In practice you would use GPIO callbacks on level change rather than polling.

The time.sleep(0.01) is kind to other programs which want to run.

#!/usr/bin/env python

import time
import pigpio

GPIO=4

pi = pigpio.pi()

if not pi.connected:
   exit()

count=0
last_state = pi.read(GPIO)

while True:
   now = pi.read(GPIO)
   if now != last_state:
      last_state = now
      if now == 1:
         count += 1
         print(count)
   time.sleep(0.01)
0

So I feel, your problem here is that one "high" input from the gpio is being read as more than one pulse in your variable. I'm pretty sure that you are using a while loop, as in

while 1: if gpio.input(countswitch): data["count"] += 1

so I suggest u should try this code out

def Check_For_ONE_CLICK():    
    while 1:
        c=0
        if gpio.input(coutswitch):
            if c==0:
                data["count"]+=1
                c+=1
            else:
                break

If you use the Code Snippet I have given as it is REPLACE MY SNIPPET EXACTLY WITH YOUR CODE WHICH MEANS IF YOU WANT THE CODE TO CHECK FOR A LONGER TIME YOU WILL NEED TO ADD A WHILE LOOP or any other loop!!

Each time u need to check for one click [Exactly what u wanted your code to do] use the function I have defined...

Hope it helps you...

  • Why are you crying so loud with big and upper letters? – Ingo Jul 10 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.