Shop OBEX P1 Docs P2 Docs Learn Events
Counting With a Ping Sensor — Parallax Forums

Counting With a Ping Sensor

DmageeDmagee Posts: 20
edited 2012-01-15 20:28 in BASIC Stamp
Hi, I'm trying use a Ping Sensor to count ten black bottles as they travel down a conveyor belt.

After ten bottles have been counted a entry gate opens and a bunch of other stuff happens.

The Ping Sensor is easy but the count ten and then continue with the rest of the program I'm having problems with.

I've been looking for some examples in the Parallax text that might help me but I haven't found one yet.

Any help would be appreciated.
Thanks, Dave.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-12-07 13:08
    You have to post your code. Follow these directions ...

    attachment.php?attachmentid=78421&d=1297987572
  • DmageeDmagee Posts: 20
    edited 2011-12-07 13:24
    The problem is that I haven't came up with any code that works. I knew that counting would be my biggest problem so I started with it.
  • FranklinFranklin Posts: 4,747
    edited 2011-12-07 13:28
    Perhaps, if you post the code that doesn't work we can figure out why.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-07 13:42
    Dave,

    To help you get started, let's go over some general principles first:
    1. Try mounting your Ping))) about 3" from the moving bottles, perpendicular to the direction of travel.

    2. Mount a backstop behind the bottles for the Ping))) to sense reflections from when there's no bottle in the way. This will prevent long recovery times, which may cause you to miss a bottle.

    3. Establish a starting distance threshold for bottle vs. no-bottle.

    4. Take samples as fast as you can. When the distance becomes less than the distance threshold, add one to your count and increase the distance threshold a little. This adds hysteresis to the detection and helps to prevent multiple counts.

    5. When the sensed distance becomes greater than the distance threshold, decrease the distance threshold by the same amount you increased it before.

    6. In a noisy bottling environment, be prepared for false data from the Ping))). You may need to filter the data returned by considering more than one or two samples.

    7. Don't expect a sensor like the Ping))) to be able to count bottles quickly. A few bottles a second may be all that you can achieve. If they are moving too fast -- especially if they are round -- you could miss them entirely.

    -Phil
  • DmageeDmagee Posts: 20
    edited 2011-12-07 14:03
    Ok this looks bad and needs a lot of tweaking, I've just been trying to get something to work.

    cmDistance  VAR   Word
    inDistance  VAR   Word
    time        VAR   Word
    Bottle_Count       VAR   Word
    Bottle      PIN   1
    Led         PIN   13
    Capture         CON     1000
    cycles          VAR     Word
    PAUSE 200
    SEROUT 14, 84, [22, 12]
    PAUSE 5
    
    DO
      PULSOUT 15, 5
      PULSIN 15, 1, time
    
      inDistance = inConstant ** time
     
    
      IF inDistance <6 THEN   HIGH led
      IF inDistance >5 THEN    LOW led
      IF led=1 THEN
       Bottle_Count =  Bottle_Count +1
       ENDIF
       DEBUG CR, DEC  Bottle_Count, CR
       SEROUT 14, 84, [148, DEC3 Bottle_Count, CR]
    LOOP
    
  • DmageeDmagee Posts: 20
    edited 2011-12-07 14:07
    I haven't debounced it so it kind of runs away.
  • ercoerco Posts: 20,256
    edited 2011-12-07 14:15
    An IR break-beam sensor could count much faster and more reliably than a PING sensor, at lower cost.
  • DmageeDmagee Posts: 20
    edited 2011-12-07 14:16
    This is working better, still needs to be cleaned up and tweaked.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    CmConstant    CON   2260
    InConstant    CON   890
    cmDistance  VAR   Word
    inDistance  VAR   Word
    time        VAR   Word
    Bottle_Count       VAR   Word
    Bottle      PIN   1
    Led         PIN   13
    led2        PIN   11
    Capture         CON     1000
    cycles          VAR     Word
    PAUSE 200
    SEROUT 14, 84, [22, 12]
    PAUSE 5
    
    DO
      PULSOUT 15, 5
      PULSIN 15, 1, time
    
      inDistance = inConstant ** time
     
    
      IF inDistance <6 THEN   HIGH led
      IF inDistance >5 THEN    LOW led
      IF led=1 THEN
       Bottle_Count =  Bottle_Count +1
       ENDIF
       DEBUG CR, DEC  Bottle_Count, CR
       SEROUT 14, 84, [148, DEC3 Bottle_Count, CR]
      IF Bottle_Count = 10  THEN
      HIGH led2
      PAUSE 500
      Bottle_Count = 0
      LOW led2
      ENDIF
    LOOP
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-07 14:21
    Dave,

    Two things:

    1. Your attempt at hysteresis is backwards and could lead to oscillation. Better would be:
      IF inDistance < 5 THEN   HIGH led
      IF inDistance > 6 THEN    LOW led
    

    2. You're gating your count with the LED pin. IOW, you keep incrementing as long as it remains high. What you should be doing is adding to the count only when the pin changes from low to high.

    -Phil
  • DmageeDmagee Posts: 20
    edited 2011-12-07 14:22
    It's not realy the type of sensor that I care about, I have lots of sensors. I just haven't messed with counting codes. AAdfsfdsfaasfyjdtkflg;uioly
  • waltsailingwaltsailing Posts: 40
    edited 2012-01-15 20:28
    Hi,

    Here is one way to do this problem.. We just did almost this same problem with the ping. What we did was use the ping and the basic stamp, actually on a boe-bot, to count a drill based coil winder. A large circle, about 1 foot in diameter was cut from card board. Then this was cut in half and taped to the side of the shaft coming out of the variable speed battery operated drill. Every time the drill would go around, the stamp on the boe-bot would count it and output this to both the LCD and the computer via the debug. The way it works is to have nothing in front of the ping, then when the half circle turns, it cuts the beam of the ping. The ping gets an echo, at a closer range. I used 8 inches, for when the circle is in the way, otherwise the ping goes as far out as it normally does. This has to be counted with a bit of tricks to insure that the counter works when you move the drill around a bit. This seems to work as long as the speed of the drill is not more than the rate the ping pings at. Seems to be good for well over a 100 RPMs. Any faster and the ping will 'skip' counts. It also beeps so you can hear it counting. It's kind of a ping insturment, wave your hand in front and you get a different tone for the measured range. It is limited by the 'word' max value.

    I posted the code below. Its not clear to me how to put this in a panel by itself, so sorry for that. But this does work. We used this the past few days to wind some 1000 turn coils, and it worked pretty good.

    note the logic with the indistanceflg... This counts once, and then waits until the range goes long again.

    Maybe this code will help you. You can see the count results on both the PC and the LCD here.

    Regards,
    Walt,
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    CmConstant    CON   2260
    InConstant    CON   890
    indistanceflg VAR Bit
    cmDistance  VAR   Word
    inDistance  VAR   Word
    time        VAR   Word
    count1      VAR Word
     p VAR Word
    
     count1 = 0
    
    
    DO
      PULSOUT 2, 5
      PULSIN 2, 1, time
      inDistance = inConstant ** time
    
    
    
      DEBUG HOME , CR, DEC3 inDistance, " in"
    
    
      '                      0   1    2    3    4    5    6   7     8
      LOOKUP indistance ,[3000,3100,3200,3300,3400,3600,3700,4000,2000],p
    
      IF indistance <9 THEN
      FREQOUT 1, 100,p
    
      ENDIF
    
    
    
       IF indistance => 8 THEN indistanceflg =1
    
        IF indistance < 8  AND indistanceflg =1 THEN
          count1=count1+1
          indistanceflg=0
        ENDIF
        DEBUG HOME ,CR, "Counter ", DEC4 COUNT1
    
    TXpIN        CON 0
    baud19200    CON 32
    i VAR Byte
    HIGH TXPIN
    SEROUT txpin, baud19200,2,["Robo kitty RULES"]
    SEROUT txpin, baud19200,2,["counter ", DEC4 count1, " ",CR]
     LOOP
    
Sign In or Register to comment.