Thursday, February 3, 2011

Color Sensor

Here is the final version of the code which was uploaded to the Pic 14M to get an analogue signal for the color sensor.
CODE BEGINS:
-------------------------------------------------------------------
symbol redgreen = b3
symbol redblue = b4
symbol greenblue = b5

init:
pwmout 2,99,0
main:
low 1 ;red on
pause 125
readadc 0, b0 ;how much red?
pause 125
high 1 ;red off
low 2 ;green on
pause 125
readadc 0, b1 ;how much green?
pause 125
high 2 ;green off
low 3 ;blue on
pause 125
readadc 0, b2 ;how much blue?
pause 125
high 3 ;blue off
redgreen = b1-b0
redblue = b2-b0
greenblue = b2-b1
;readadc 4, b6
serout 0, N2400, (10,13)
serout 0, N2400, (#b0, " ",#b1, " ", #b2 )
if redgreen > 10 AND redgreen <> 10 AND redblue <150 then red
if redgreen <> 150 AND greenblue > 10 AND greenblue < 150then green
if redblue <> 150 AND greenblue <>150 then blue
low 4
low 5
goto main

red:
pwmout 2, 99, 0
high 4
low 5
goto main

green:
pwmout 2, 99, 150
high 4
high 5

goto main
blue:
high 5
low 4
pwmout 2, 99, 250
goto main
------------------------------------------------------------------------
CODE ENDS:

The sensor activates a 3 state LED which can emit a red green or blue light, and then measures the voltage level on an input pin which is connected to a CDS cell. the theory is that blue objects will reflect more light when a blue object is in front of it, and the resistance of the CDS cell will increase, thereby causing a corresponding voltage change on the input pin. The sensor measures voltage values for each of the color states and makes a decision based on the resultant data. The most difficult part of this project was getting the analogue signal to function properly. I spent at least an hour trying to get a coherent signal from the sensor, only to be informed that I forgot to comment out some of my debugging code.

It works now, and can interface with the vex brain! On to other projects!

No comments:

Post a Comment