Using Photocell to detect winner of race
mechjo16
Posts: 12
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
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
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.
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.
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
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.
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).
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.
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.
' {$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
http://bscircuitdesigns.com/Parallax/GAME.JPG
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?
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.
Using DC light level sensors outdoors reliably is very difficult. Sunlight gets everywhere.