Input pin 0 for LDR sensor, ie readadc or read the value from the LDR into the Pic14M. Input 1,2,3 are the Red, Green, and Blue. b0, b1, and b2 respectively, are the possible stored read-value from the LDR. w3 is the combined value of b0, b1, and b2. Color determinations are done comparetively, i.e. if b0>b1 and b0>b2 then red--this means the largest reflective color is red; thought green or blue reflection may store some values, but red would be the predominant color reflected into the LDR sensor as compare with the blue and green colors, which may still exist but comparatively is not the dominant valued color. To account for range of colors beyond red, blue, and green, w3 is created to deal with color values beyond the RGBs, i.e. any color value intensity lower than 250 or greater than 510. Please see below for the actual program for reference.
'Color Sensor LEDs RGB on outputs 1,2,3 USING PWM
'Read the CDs cell on ADC 4
Low 0,1,2,3,4,5 'Set all outputs to low to turn off RGB
pause 1000
main:
w3 = 0 'This is the sum of all channels
high 1 'Set the RED channel high turns it on
low 2,3
pause 50 'Give time for the LED to turn on
readadc 0,b0 'Read the value from the LDR
high 3 'Set the BLUE Channel ON
low 1,2
pause 50
readadc 0,b2 'Read the value from the LDR into a different variable
'Apply any necessary offset for the sensitivity of the LDR
high 2 'Repeat for the GREEN Channel
low 1,3
pause 50
readadc 0,b1
b1 = b1 - 30
debug
w3 = b0 + b1 + b2 'Find the sum of the 3 color channels
'sertxd ("w3 reading ",#w3,13,10)
if w3 > 510 then nocolor 'Cut on total intensity
if w3 < class="Apple-tab-span" style="white-space: pre;"> 'Cut on total intensity
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:
sertxd ("nocolor",13,10)
pwmout 2,99,400 ;SET THE OUTPUT TO 5V
low 4,5
goto main
red:
sertxd ("red",13,10)
pwmout 2,99,0 ;SET THE OUTPUT TO 0V
high 4
low 5
goto main
green:
sertxd ("green",13,10)
pwmout 2,99,125 ;SET THE OUTPUT TO 1.5V
low 4
high 5
goto main
blue:
sertxd ("blue",13,10)
pwmout 2,99,250 ;SET THE OUTPUT TO 3V
high 4,5
goto main
No comments:
Post a Comment