saras.nes
Posts: 4
Joined: Mon Jun 04, 2018 6:08 am

Line counter

Mon Jun 04, 2018 6:20 am

Hello,

I've Raspberry and SHARP 2Y0A02 distance sensor. I wish to make a counter of moved object, but I've a problem that my code is counting every time it detects object and until it's finished it counting like million times. What I need to do is to count only +1 until signal lost and wait for signal once again detected and count +1 next object. My code is:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIN = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

counting = 0

try:
        while True:
               if GPIO.input(GPIN) == 1 :
                        counting = counting + 1
                        print(counting)
                        time.sleep(1)
               else:
                        counting = counting
                        print (counting)
                        time.sleep(1)

except KeyboardInterrupt:
        GPIO.cleanup()
Output look like Image

As you see when signal lost everything seems ok it keeps last digit, but when signal is detected it counts endlessly +1 until signal lost. Is there a solution for making this sensor work as counter (button like style)? Thank you.

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Line counter

Mon Jun 04, 2018 9:29 am

Try this version

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

GPIN = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

counting = 0

try:
    while True:
        if GPIO.input(GPIN):
            counting = counting + 1
            print(counting)
        else:
            counting = counting
            print (counting)
        time.sleep(1)

except KeyboardInterrupt:
    GPIO.cleanup()
BTW, you may want to avoid using GPIO04 as that's the default pin for 1-wire sensors.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

saras.nes
Posts: 4
Joined: Mon Jun 04, 2018 6:08 am

Re: Line counter

Mon Jun 04, 2018 10:10 am

Hello,

I'm still very noob on Raspberry but I like it very much :-)

Nah it's the same when the sensor is covered it keeps counting +1. But i need that it count like only one time if sensor is covered and wait till other interruption. Now it's printing:

0 #No sensor covered
0 #No sensor covered
0 #No sensor covered
0 #No sensor covered
0 #No sensor covered
1 #Sensor covered 1st time
2 #Sensor covered 1st time
3 #Sensor covered 1st time
4 #Sensor covered 1st time
5 #Sensor covered 1st time
6 #Sensor covered 1st time
7 #Sensor covered 1st time
8 #Sensor covered 1st time
9 #Sensor covered 1st time
10 #Sensor covered 1st time
11 #Sensor covered 1st time
12 #Sensor covered 1st time
12 #No sensor covered
12 #No sensor covered
12 #No sensor covered
13 #Sensor covered 2nd time
14 #Sensor covered 2nd time
15 #Sensor covered 2nd time

And I need output like:
0 #No sensor covered
0 #No sensor covered
1 #Sensor covered 1st time
1 #Sensor covered 1st time
1 #Sensor covered 1st time
1 #No sensor covered
1 #No sensor covered
2 #Sensor covered 2nd time
2 #Sensor covered 2nd time
2 #Sensor covered 2nd time
2 #No sensor covered
2 #No sensor covered

I hope now is a more clear what I want to reach.

And what PIN i should use than for this project? I'm on Raspberry Pi 3 model B+

Thank you.

hippy
Posts: 5340
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Line counter

Mon Jun 04, 2018 10:58 am

DougieLawson wrote:
Mon Jun 04, 2018 9:29 am
Try this version
Not sure how that will work better as it mostly seems to be a refactoring of the original code with the two 'time.sleep()' commands taken out from inside the 'if' command and specifying a pull-down.

The problem appears to be that it keeps counting while in contact. You need to create some sort of flag which can determine if it's a new touch or a continuation of the last touch for example, in pseudo code -

Code: Select all

count = 0
alreadyCounted = False
While True
  If contact() and ( not alreadyCounted )
    count = count + 1
    alreadyCounted = True
  Else
    alreadyCounted = False

PiGraham
Posts: 3548
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Line counter

Mon Jun 04, 2018 11:06 am

You need to count one when the input changes from low to high. You are counting whenever the input is high.
You can keep a flag to detect the change or use one of many edge detection functions.

Code: Select all

    while True:
                   GPIO.wait_for_edge(GPIN, GPIO.RISING)
                   counting = counting + 1
                   print (counting)

saras.nes
Posts: 4
Joined: Mon Jun 04, 2018 6:08 am

Re: Line counter

Mon Jun 04, 2018 12:10 pm

hippy your solutions seems to work thank you, just the problem now when i change ranges of object it also counting +1 so next question is how we can eliminate counting from range of object? It's getting different V in range so that's why it counts. So i just need like signal 1 no signal 0 and count these between signals. Maybe then i can describe somehow in V in range?

PiGraham
Posts: 3548
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Line counter

Mon Jun 04, 2018 1:20 pm

saras.nes wrote:
Mon Jun 04, 2018 12:10 pm
hippy your solutions seems to work thank you, just the problem now when i change ranges of object it also counting +1 so next question is how we can eliminate counting from range of object? It's getting different V in range so that's why it counts. So i just need like signal 1 no signal 0 and count these between signals. Maybe then i can describe somehow in V in range?
Use a comparator to detect if the sensor output voltage is above or below an adjustable threshold.
If you just connect the output to a gpio pin the gpio has a fixed threshold.
You will have to choose a working distance. Near that limit you will get some multiple triggers. Try to arrange for your objects to pass across the view of the sensor rather than coming toward t head on so you can avoid the transition range that can cause extra counts.
You can also use some "debounce timeouts to that you don't count until the sensor has been steady in one state or the other for some minimum time.

Why is this the a "line counter" and what distance are you measuring with the sharp sensor?

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Line counter

Mon Jun 04, 2018 1:38 pm

A simple way would be to store the pin state. The just increase the counter by one when the state changes.
Using GPIO 13

Code: Select all

import RPi.GPIO as GPIO
import time

GPIN = 13
Pinstate = False
counting = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

try:

  while True:
    
    if GPIO.input(GPIN) and not Pinstate:
      counting += 1
      Pinstate = True 
      
    elif not GPIO.input(GPIN) and Pinstate:
        Pinstate = False

    time.sleep(1)
    print (('Sensor covered (%s)' if Pinstate else 'No sensor covered (%s)') % counting)
    
except KeyboardInterrupt:
        GPIO.cleanup()

saras.nes
Posts: 4
Joined: Mon Jun 04, 2018 6:08 am

Re: Line counter

Mon Jun 04, 2018 1:40 pm

I'm not measuring distance with that, I wan't it to use as counter of items comes trough line. I.e. i want to count how many watermelons was passed trough the line in some give time. I just ordered this in thoughts that i can convert it as counter not a distance meter. But it start looking i'm not that skilled yes to make it count, but with your help i'll get to another level :-)

gordon77
Posts: 3999
Joined: Sun Aug 05, 2012 3:12 pm

Re: Line counter

Mon Jun 04, 2018 1:42 pm

saras.nes wrote:
Mon Jun 04, 2018 12:10 pm
hippy your solutions seems to work thank you, just the problem now when i change ranges of object it also counting +1 so next question is how we can eliminate counting from range of object? It's getting different V in range so that's why it counts. So i just need like signal 1 no signal 0 and count these between signals. Maybe then i can describe somehow in V in range?
How do you have it wired ? It appears it has an analogue output(0.4 to 2.8v) dependent on distance, if you have it directly connected to a GPIO input then the input is just seeing a 0 or a 1 when it exceeds the threshold.

If you want to measure range you will need a ADC.


https://www.sparkfun.com/products/8958

PiGraham
Posts: 3548
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Line counter

Mon Jun 04, 2018 1:55 pm

saras.nes wrote:
Mon Jun 04, 2018 1:40 pm
I'm not measuring distance with that, I wan't it to use as counter of items comes trough line. I.e. i want to count how many watermelons was passed trough the line in some give time. I just ordered this in thoughts that i can convert it as counter not a distance meter. But it start looking i'm not that skilled yes to make it count, but with your help i'll get to another level :-)
You could use a through beam or retroreflecting optical sensor for a more positive detection.

https://uk.rs-online.com/web/c/automati ... c-sensors/

https://www.ebay.co.uk/itm/Sick-WL12-2P ... SwFq1bEok3

Industrial sensors will probably perform better but you may need to level shift the output to suit the Pi 3.3V inputs.

hippy
Posts: 5340
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Line counter

Mon Jun 04, 2018 3:50 pm

saras.nes wrote:
Mon Jun 04, 2018 12:10 pm
just the problem now when i change ranges of object it also counting +1 so next question is how we can eliminate counting from range of object?
Not quite sure how you mean. You sensor seems to be an analogue device indicating how far away an object is. If you have an ADC you can read that, determine when an object is present at some particular distance, and have some other level which can be used to indicate when it no longer is. Only count when an object comes into range. Something like ...

Code: Select all

While True
  While readSensor() < 20
    Do Nothing
  count = count + 1
  While readSensor() > 10
    Do Nothing

pcmanbob
Posts: 6205
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Line counter

Mon Jun 04, 2018 4:14 pm

Try this code, it waits for a change from gpio = 0 and then from gpio =1

so it only adds to the count on change of gpio state.

it should sill work even if you are using an analogue sensor as a digital input because when the range is such that the output voltage exceeds the low threshold and becomes a high, it will not change again until the voltage drops sufficiently that it falls below the high threshold and so becomes a low once again. ( I think that makes sense I know it work,s just putting it in to words is the hard bit to make it understandable. )

Code: Select all

import RPi.GPIO as GPIO
import time

GPIN = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

counting = 0

try:
        # this is the main loop
        while True:
               	# now we wait in a new loop for gpio to equal 1
                while GPIO.input(GPIN) == 0:
                    time.sleep(0.5)
                    
                # add 1 to your count  and print
                counting = counting + 1
                print(counting)
                
                # now we wait in yet another loop to the gpio to equal 0
                while GPIO.input(GPIN) == 1:
                    time.sleep(0.5)
                # then we return to the start of the main loop

except KeyboardInterrupt:
        GPIO.cleanup()
        
untested code expect error.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PiGraham
Posts: 3548
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Line counter

Mon Jun 04, 2018 4:16 pm

The sharp sensor will cross the gpio input voltage threshold as some fixed distance so it can operate as a digital present / not present detector. Anything closer than X will send the input high (or low). Using it direct works but allows no adjustment other than moving the sensor physically.

Reading on a curved melon could cause problems with multiple triggers as the distance changes over the curve.

pcmanbob
Posts: 6205
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Line counter

Mon Jun 04, 2018 4:26 pm

PiGraham wrote:
Mon Jun 04, 2018 4:16 pm
The sharp sensor will cross the gpio input voltage threshold as some fixed distance so it can operate as a digital present / not present detector. Anything closer than X will send the input high (or low). Using it direct works but allows no adjustment other than moving the sensor physically.

Reading on a curved melon could cause problems with multiple triggers as the distance changes over the curve.
may be , you might be able to solve it by putting the sensor as close to the line as possible, so that the you get a high as soon as a melon comes in to range, the only way to know for sure is to do some field tests.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”