Saturday, February 5, 2011

tuesday week 4

Today we worked on building a color sensor for our robot. Using a 14 pin picaxe, I built the circuit on a breadboard and did some test code. The test circuit I built:
When you shine a red, blue, and green light on a red paper, the red light reflects back most brightly, on a blue paper, the blue reflects back most, etc. How this circuit works is that we have a 3 color LED and a CdS cell connected to a pic microcontroller. The 3 colors take turns lighting in sequence and the pic reads the brightness reflected for each one. It then does a comparison of the 3 reflected values and determines what color the object being illuminated is.
The "screenshot" is the serial message i got when holding a green card in front of the sensor, so it works!

I put electrical tape around the CdS cell and LED to reduce error and noise caused by ambient light.
My code:
high 0,1,2,3,4,5
pause 1000
main:
 debug
 w3 = 0
 low 1
 high 2,3
 pause 50
 readadc 0,b0
 low 2
 high 1,3
 pause 50
 readadc 0,b1
 b1=b1+20
 low 3
 high 1,2
 pause 50
 readadc 0,b2
 w3 = b0 + b1 +b2
 if w3 > 450 then nocolor
 if w3 < 220 then nocolor
 if b0<b1 and b0<b2 then red
 if b1<b0 and b1<b2 then green
 if b2<b0 and b2<b1 then blue
goto main
nocolor:
 serout 0,N2400,("nocolor",13,10)
 high 4,5
goto main

red:
 serout 0,N2400,("red",13,10)
 low 4
 high 5
goto main

blue:
 serout 0,N2400,("blue",13,10)
 high 4
 low 5
goto main

green:
 serout 0,N2400,("green",13,10)
 low 4,5
goto main

For this test version, I had a pair of LED's that would light in different pattern to indicate color.

No comments:

Post a Comment