Shop OBEX P1 Docs P2 Docs Learn Events
Using Photocell to detect winner of race — Parallax Forums

Using Photocell to detect winner of race

mechjo16mechjo16 Posts: 12
edited 2015-04-03 14:22 in BASIC Stamp
Hello all,

I am new to using the BS2. I am working with my son and we are building a hot wheels track. There will be two separate tracks. Down hill drag strips if you will. What I am looking to accomplish is for a led it come on when the winning car blocks the light from the photo resistor. However, I do not what the led to come on when the losing car blocks its photo resistor (this is the part that has me stumped both cars get a light coming on).

Thank you in advance.

John

Updated 4-2-2015
Almost there. Got the phototransistors working and learned about subroutines. I'm just having problems with the subroutines executing properly. Any help would be appreciated.

' {$STAMP BS2}
' {$PBASIC 2.5}


'Declare variables
Counter VAR Word 'Defines variable as word


PAUSE 1000 'Initial 1 second pause
DEBUG "Program Running!", CR 'Display "Program Running"

'

'Switch control
DO
DEBUG ? IN0
PAUSE 20
IF (IN0 = 1) THEN

'

'Ready set go lights
'FOR counter = 1 TO 1 'Cycle code 1 time

'DEBUG ? counter
HIGH 15 'Red LED on
LOW 14 'Green LED off
LOW 13 'Yellow LED off
PAUSE 2500

FOR counter = 1 TO 3 'Flase Yellow LED 3 times
DEBUG ? counter
LOW 15 'Red LED off
LOW 14 'Green LED off
HIGH 13 'Yellow LED on.
PAUSE 1500
LOW 13 'Blink yellow light 3 times
PAUSE 500
NEXT

HIGH 14 'Green LED on
LOW 15 'Red LED off
LOW 13 'Yellow LED off
PAUSE 2500 'Green LED on for 2.5 seconds
'LOW 14 'Green LED off
'NEXT
'

' Servo Action

FOR counter = 1 TO 50 ' release cars
PULSOUT 12, 1200
PAUSE 20
DEBUG "Release cars", CR

NEXT
PAUSE 1000 ' set car release for 1 second
LOW 14 ' Turn green LED off
FOR counter = 1 TO 50 ' set car release back to hold position
PULSOUT 12, 750
PAUSE 20
DEBUG "Reset car release", CR
NEXT
'


'Which car won
IF IN6 = 0 THEN
GOSUB car1win
DEBUG "back from car1sub"

ELSEIF IN4 = 0 THEN
GOSUB car2win
DEBUG "back from car2sub"

ENDIF

'


'Subroutines
Car1win:

DEBUG ? IN6
PAUSE 20

IF (IN6 = 0) THEN

' FOR counter = 1 TO 1 'Cycle code 1 time

' DEBUG ? counter
HIGH 9
PAUSE 5000
LOW 9
' NEXT
' ELSE
PAUSE 20
ENDIF
RETURN

'

Car2win:

DEBUG ? IN4
PAUSE 20

IF (IN4 = 0) THEN

' FOR counter = 1 TO 1 'Cycle code 1 time

' DEBUG ? counter
HIGH 3
PAUSE 5000
LOW 3
' NEXT
' ELSE
PAUSE 20
ENDIF
RETURN
'


'Close the switches if statement
ELSE
PAUSE 20
ENDIF

LOOP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-26 07:46
    I'm a little confused about what you want to do. How many LEDs do you have and what do you want each one to show?
  • ercoerco Posts: 20,256
    edited 2015-03-26 07:57
    Make a loop which rapidly samples both sensors, exiting the loop when either senses a car. Then turn on the appropriate LED.

    BTW, photocells are slow. RCtime can be slow if component values are wrong. These could limit your ability to properly sense when 2 cars are very close. I'd recommend using two phototransistors, each with a series ~1 Meg resistor. That makes a pair of voltage dividers which can be read instantly as either high or low, much faster than photocells and RCtime.
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-26 08:03
    Mike Green wrote: »
    I'm a little confused about what you want to do. How many LEDs do you have and what do you want each one to show?



    At the finish line I have a photo resistor on each track to detect the winner. When the photo resistor light is blocked I want the LED to come on...Above the track I have a led over each lane. So 2 LED's Total. Right now as both cars finish the race, as the photo resistor light is blocked, both LED's come on.

    Let's say my son's car finishes first. I do not want my LED to come on. I want my light to stay off so my son can see he won...... So basically, only the winner gets the light.
  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-26 10:17
    Erco's suggestion would work. Another idea is to have a variable (call it WINNER) that's initialized to zero. You have a loop in your program that checks one photocell, then the other. If a photocell is blocked, you check WINNER. If WINNER = 0 then you set WINNER to the photocell number (1 or 2) and light up the corresponding LED. You can either reset the Stamp for each new race or have a pushbutton that's tested each time through the loop. If the pushbutton is pressed, the LEDs get turned off and WINNER is reset to zero. Here's a pseudo-code version:
    DO
      <measure photocell1 resistance>
      <measure photocell2 resistance>
      IF <pushbutton pressed> THEN
        LOW LED1 : LOW LED2 : WINNER = 0
      ELSEIF <photocell1 resistance < threshold> AND WINNER = 0 THEN
        WINNER = 1 : HIGH LED1
      ELSEIF <photocell2 resistance < threshold> AND WINNER = 0 THEN
        WINNER = 2 : HIGH LED2
      ENDIF
    LOOP
    
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-26 11:03
    Thanks for the tips guys. I will play with it some more this weekend. I appreciate you time experience.
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-27 07:57
    If it helps I tried to do a flow chart of what I am trying to accomplish. See below.

    I thought I could get the winning LED to come on, guess that is not working either.

    So far I have been able to go through the "ready, set, go" light cycle and get the servo to release the cars.


    HW Race Flow chart.pdf
  • kwinnkwinn Posts: 8,697
    edited 2015-03-27 08:54
    A BS2 seems to be a bit of overkill for this. You should be able to do this with a 556 (dual 555) timer when there are only two sensors.
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-27 09:04
    I'm not familiar with the 555 timer.

    I just figured I'd buy the BS2 kit, this way I could keep my sons attention if it was incorporated with things he likes like lego's and cars while at the same time learning a new useful skill.
  • PublisonPublison Posts: 12,366
    edited 2015-03-27 09:11
    mechjo16 wrote: »
    I'm not familiar with the 555 timer.

    I just figured I'd buy the BS2 kit, this way I could keep my sons attention if it was incorporated with things he likes like lego's and cars while at the same time learning a new useful skill.

    That's a perfectly valid concern, using the BS2. It is a great MicroController for young people.

    That said, I am going to move this to the BS2 Forums where it will get more exposure.
  • DomanikDomanik Posts: 233
    edited 2015-03-27 09:57
    mechjo16 wrote: »
    I'm not familiar with the 555 timer.

    I just figured I'd buy the BS2 kit, this way I could keep my sons attention if it was incorporated with things he likes like lego's and cars while at the same time learning a new useful skill.
    Sounds like loads of fun. You could do all sorts of things with your son while creating interactive things with the BS2. For example:
    1) A series of red LEDs on a narrow vertical stand that down counts to start the race.
    2) A speaker / buzzer to start when the last LED flashes
    3) A push button to reset the finish LEDs

    Kids love things that have cause and effect- push a button and get a sound or flashing light; like sound effects from a dragster race when a button is pushed. Legos are great for making Hot Wheels bridges or ramps to jump and crash. It would be great if your son used the BS2 to modify the cause & effect he gets.
    Just thinking...
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-27 10:12
    Domanik wrote: »
    Sounds like loads of fun. You could do all sorts of things with your son while creating interactive things with the BS2. For example:
    1) A series of red LEDs on a narrow vertical stand that down counts to start the race.
    2) A speaker / buzzer to start when the last LED flashes
    3) A push button to reset the finish LEDs

    Kids love things that have cause and effect- push a button and get a sound or flashing light; like sound effects from a dragster race when a button is pushed. Legos are great for making Hot Wheels bridges or ramps to jump and crash. It would be great if your son used the BS2 to modify the cause & effect he gets.
    Just thinking...



    Were still crawling my friend. I'm still trying to get my head wrapped around photo sensors. lol :)

    The BS2 does look like a lot of fun once you figure it out. Some of the things I've seen on-line blow my mind.....All I get is awww dad can we do this, can we do that...sure son (as bullets are sweating from my head).
  • GenetixGenetix Posts: 1,752
    edited 2015-03-27 14:49
    The BS2 is very easy to use so it's perfect for a newcomer.
    What's a Microcontroller? was written for someone without electronics or programming experience so it's highly recommended to start with that is you haven't already.
    https://www.parallax.com/product/28152

    What did you use to make that nice flow chart?
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-27 15:16
    Genetix wrote: »
    The BS2 is very easy to use so it's perfect for a newcomer.
    What's a Microcontroller? was written for someone without electronics or programming experience so it's highly recommended to start with that is you haven't already.
    https://www.parallax.com/product/28152

    What did you use to make that nice flow chart?

    Yeah the book came with the kit... just having a hard time with the phototransistor part...

    I did the flow chart in MS Visio. I'm not sure if the flow chart is right. I just captured what I am trying to do.
  • DomanikDomanik Posts: 233
    edited 2015-03-27 16:18
    mechjo16 wrote: »
    I did the flow chart in MS Visio. I'm not sure if the flow chart is right. I just captured what I am trying to do.
    RE Flowchart: The "Do Nothing"s needs to loop back to "Which car won?" until the decision block goes YES then fall thru and go back to start. As it's written you'll test for a winner then fall thru to END and stop.

    Mike Green's code looks pretty good.

    How many functions work? Read start switch, cycle LED, servo on/off, photo detection = TRUE/FALSE? It's always good to know what parts are solid.
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-28 11:42
    Got Phototransistors operating, having trouble with subroutines executing properly.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    'Declare variables
    Counter VAR Word 'Defines variable as word


    PAUSE 1000 'Initial 1 second pause
    DEBUG "Program Running!", CR 'Display "Program Running"

    '

    'Switch control
    DO
    DEBUG ? IN0
    PAUSE 20
    IF (IN0 = 1) THEN

    '

    'Ready set go lights
    'FOR counter = 1 TO 1 'Cycle code 1 time

    'DEBUG ? counter
    HIGH 15 'Red LED on
    LOW 14 'Green LED off
    LOW 13 'Yellow LED off
    PAUSE 2500

    FOR counter = 1 TO 3 'Flase Yellow LED 3 times
    DEBUG ? counter
    LOW 15 'Red LED off
    LOW 14 'Green LED off
    HIGH 13 'Yellow LED on.
    PAUSE 1500
    LOW 13 'Blink yellow light 3 times
    PAUSE 500
    NEXT

    HIGH 14 'Green LED on
    LOW 15 'Red LED off
    LOW 13 'Yellow LED off
    PAUSE 2500 'Green LED on for 2.5 seconds
    'LOW 14 'Green LED off
    'NEXT
    '

    ' Servo Action

    FOR counter = 1 TO 50 ' release cars
    PULSOUT 12, 1200
    PAUSE 20
    DEBUG "Release cars", CR

    NEXT
    PAUSE 1000 ' set car release for 1 second
    LOW 14 ' Turn green LED off
    FOR counter = 1 TO 50 ' set car release back to hold position
    PULSOUT 12, 750
    PAUSE 20
    DEBUG "Reset car release", CR
    NEXT
    '


    'Which car won
    IF IN6 = 0 THEN
    GOSUB car1win
    DEBUG "back from car1sub"

    ELSEIF IN4 = 0 THEN
    GOSUB car2win
    DEBUG "back from car2sub"

    ENDIF

    '


    'Subroutines
    Car1win:

    DEBUG ? IN6
    PAUSE 20

    IF (IN6 = 0) THEN

    ' FOR counter = 1 TO 1 'Cycle code 1 time

    ' DEBUG ? counter
    HIGH 9
    PAUSE 5000
    LOW 9
    ' NEXT
    ' ELSE
    PAUSE 20
    ENDIF
    RETURN

    '

    Car2win:

    DEBUG ? IN4
    PAUSE 20

    IF (IN4 = 0) THEN

    ' FOR counter = 1 TO 1 'Cycle code 1 time

    ' DEBUG ? counter
    HIGH 3
    PAUSE 5000
    LOW 3
    ' NEXT
    ' ELSE
    PAUSE 20
    ENDIF
    RETURN
    '


    'Close the swithes if statement
    ELSE
    PAUSE 20
    ENDIF

    LOOP
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2015-03-28 14:26
    As mentioned earlier the BS2 is really overkill for this application ... Also mentioned was the learning perspective and future gained skill set. While the learning advantage for a future skill could be argued, the art of analog circuit design is also a valuable skill set that can also be applied as well ... Here is a circuit design that uses a dual Op-Amp, a handful of resistors and a couple of transistors to create what is called a "Wheat-stone bridge" or balance bridge. When the balance of the bridge is thrown out of balance, it latches to one side or the other preventing the other sensor from flipping it to the other side. To "reset" the circuit you need to cycle the power. This circuit has the option of using a Photo transistor or Photo resistor, however you shouldn't mix or match the two.


    http://bscircuitdesigns.com/Parallax/GAME.JPG
  • mechjo16mechjo16 Posts: 12
    edited 2015-03-28 18:00
    Mike Green wrote: »
    Erco's suggestion would work. Another idea is to have a variable (call it WINNER) that's initialized to zero. You have a loop in your program that checks one photocell, then the other. If a photocell is blocked, you check WINNER. If WINNER = 0 then you set WINNER to the photocell number (1 or 2) and light up the corresponding LED. You can either reset the Stamp for each new race or have a pushbutton that's tested each time through the loop. If the pushbutton is pressed, the LEDs get turned off and WINNER is reset to zero. Here's a pseudo-code version:
    DO
      <measure photocell1 resistance>
      <measure photocell2 resistance>
      IF <pushbutton pressed> THEN
        LOW LED1 : LOW LED2 : WINNER = 0
      ELSEIF <photocell1 resistance < threshold> AND WINNER = 0 THEN
        WINNER = 1 : HIGH LED1
      ELSEIF <photocell2 resistance < threshold> AND WINNER = 0 THEN
        WINNER = 2 : HIGH LED2
      ENDIF
    LOOP
    

    I went and picked up some Phototransistors.... How do I incorporate this code into mine (see above), I know it needs to go after the servo action.... Also, how do I stay in this part of the code until a winner crossed the line before it goes back into looking for the reset switch input again?
  • ercoerco Posts: 20,256
    edited 2015-03-29 08:18
    One step at a time, moy drug! Test your ptx (phototransistors) with a series 1 megohm resistor in a voltage divider arrangement. Put 5V across the combo (polarity matters, unlike a photocell) and use a multimeter to check the voltage at the ptx/resistor junction as you shade the ptx from ambient light, pointing the ptx up in realistic indoor lighting conditions. You want a big voltage change from light to dark, with extremes on either side of 1.4V. The Stamp reads a pin under 1.4V as low, above 1.4V as high, perfect for using an IF check in your program to read the sensor.
  • DomanikDomanik Posts: 233
    edited 2015-03-29 09:18
    mechjo16 wrote: »
    how do I stay in this part of the code until a winner crossed the line before it goes back into looking for the reset switch input again?
    Generally you want to loop on a set of instructions and check, at the end, to see if something happened to tell you it's time to move. "DO LOOP" until one LED is on. The LED will be on when a car crosses the finish line. Somehow you define the block of code with end and start key statements. Like "DO --- LOOP", "WHILE (Condition) --- END WHILE". This is different than a FOR loop that increments a variable until it's reached its max count then falls through or an IF that executes some code if true one time.

    Phototransistors: Do you have a part number from the manufacture? Manufactures provide "datasheets" that describe their parts specific operating parameters and in some cases provide an example application circuit. Everything Sir Erco said is spot on to come up with a working circuit without a datasheet. A phototransistor is like a 1 meg pot that changes resistance as the light changes. It goes down in with brighter light and up as it gets dark. If you google "Phototransistors" there are plenty of images of circuits with just the two parts.
  • ercoerco Posts: 20,256
    edited 2015-03-29 11:05
    Good ambient lighting is key to using phototransistors successfully. From my experience, a 1-megohm series resistor yields good results in average indoor conditions, but will not work outdoors or in dim light. Worst case (for oddball indoor lighting conditions), you may have to add an IR LED shining at the phototransistor, creating a break-beam detector.

    Using DC light level sensors outdoors reliably is very difficult. Sunlight gets everywhere.
  • mechjo16mechjo16 Posts: 12
    edited 2015-04-02 18:30
    Updated the code to where I am at (see post 1) I'm stuck on executing subroutines properly. Any help would be appreciated.
  • ercoerco Posts: 20,256
    edited 2015-04-02 19:46
    Good effort, you were very close. I'd say subroutines are not needed here. Try the attached program and see if it's any better.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
     'Declare variables
     Counter VAR Word ' Defines variable as word
    
     PAUSE 1000 'Initial 1 second pause
     DEBUG "Program Running!", CR 'Display "Program Running"
    
     '-------------------------------------------------------------------
    
     'Ready set go lights
     main: HIGH 15 'Red LED on
     LOW 14 'Green LED off
     LOW 13 'Yellow LED off
     PAUSE 2500
    
     FOR counter = 1 TO 3 'Flash Yellow LED 3 times
     DEBUG ? counter
     LOW 15 'Red LED off
     LOW 14 'Green LED off
     HIGH 13 'Yellow LED on.
     PAUSE 1500
     LOW 13 'Blink yellow light 3 times
     PAUSE 500
     NEXT
    
     HIGH 14 'Green LED on
     LOW 15 'Red LED off
     LOW 13 'Yellow LED off
     PAUSE 2500 'Green LED on for 2.5 seconds
     'LOW 14 'Green LED off
     'NEXT
     '------------------------------------------------------------
    
     ' Servo Action
     DEBUG "Release cars", CR
     FOR counter = 1 TO 50 ' release cars
     PULSOUT 12, 1200
     PAUSE 20
     NEXT
     PAUSE 1000 ' set car release for 1 second
     LOW 14 ' Turn green LED off
    
     DEBUG "Reset car release", CR
     FOR counter = 1 TO 50 ' set car release back to hold position
     PULSOUT 12, 750
     PAUSE 20
     NEXT
     '--------------------------------------------------------------
    
     DO                     'scan both finish sensors rapidly in loop
     IF IN6=0 THEN Car1win
     IF IN4=0 THEN Car2win
     LOOP
    
     '--------------------------------------------------------------
     Car1win:
    
     DEBUG "Car 1 Wins!", CR
    
     HIGH 9       'Lane 1 LED ON FOR 5 seconds
     PAUSE 5000
     LOW 9        'LED off
     GOTO main
    
    
     '----------------------------------------------------------------
     Car2win:
    
     DEBUG "Car 2 Wins", CR
    
     HIGH 3        'Lane 2 LED ON FOR 5 seconds
     PAUSE 5000
     LOW 3         'LED off
     GOTO main
    
  • mechjo16mechjo16 Posts: 12
    edited 2015-04-03 06:23
    That works! Thanks everyone for working with me.
  • ercoerco Posts: 20,256
    edited 2015-04-03 14:22
    HTH. Be sure to post a video of your finished project with your son racing! I designed Hot Wheels track sets for Mattel for 30 years.
Sign In or Register to comment.