' ' Simple color blending for Bi-color LED with a bit of power saving thrown in. ' ' picaxe08m connected drive 2 leds (in one package) via approx 330 ohm resistor ' ' use with BlendLight v1 schematic, note that outputs are inverted because of ' LED diode direction (tied to +5v) ' ' Dec 2006 A. Stein, MedCosm, LLC. ' ' Mar 2007, added light sensitivity behavior. Starts activity if change in ambient light value ' ' ' v4 - blend light ' v4a - forked for bidirectional LED (2 leads) ' v5 - try to sense light via LEDs and respond ' v6 - added candle modes and LED sensitivity is now adjustable (set at powerup) ' light turns on when it gets darker than this level. To readjust sensitivity, just disconnect and ' connect batteries again! 'pinouts symbol LEDred=1 'LED1 (red) pinout symbol LEDgreen=2 'LED2 (green) pinout 'variables symbol redValue=b0 'red value symbol greenValue=b1 'green value symbol randomw=w1 'allocate storage for random variable symbol randomb1=b2 symbol randomb2=b3 symbol tmpB1=b4 symbol tmpB2=b5 symbol tmpW1=w3 symbol tmpB3=b6 symbol tmpB4=b7 symbol intensity=b8 symbol blendStep=b9 symbol lightThreshold=b10 'threshold for light sensitivity symbol blendValue=b11 'current blend value (0-255) symbol newBlendValue=b12 symbol oldBlendValue=b13 'constants symbol numberColorChanges=8 symbol napLen=6 'length of sleep between light samples symbol napCnt=15 'number of times to loop on nap symbol candleDly=4 'length of pwm for candle effect symbol candleCnt=800 'num of flickers in candle effect 'init variables init: blendstep=5 '1 for smooth blend, 10 for stepped blend randomw=1234 blendValue=128 readadc LEDred, tmpB1 pause 200 readadc LEDred, lightThreshold 'read the initial light threshold value 'start of execution loop startexec: readadc LEDred, tmpB1 'seems to work better with fake 1st read pause 200 readadc LEDred, tmpB1 ' serTxD(#tmpB1," ") if tmpB1 > lightThreshold then random randomw tmpB1=randomB1%3 on tmpB1 gosub redCandle, greenCandle, startColorChange endif for tmpB1=0 to napCnt 'nap napLen*napCnt but remain responsive to serial nap napLen next goto startexec 'red candle flicker effect redCandle: for tmpW1=0 to candleCnt random randomw pwm LEDred, randomB1, candleDly next tmpW1 return 'green candle flicker effect greenCandle: for tmpW1=0 to candleCnt random randomw pwm LEDgreen, randomB1, candleDly next tmpW1 return 'blend between a few colors startColorChange: setfreq m8 'double clock freq to mitigate flickering gosub rampup random randomw blendStep=randomb2 % 10 + 1 'pick step to get there for tmpB1 = 1 to numberColorChanges random randomw 'get new random values newBlendValue=randomb1 'for new blend ' debug 'and step to new value if newBlendValue > oldBlendValue then for blendValue=oldBlendValue to newBlendValue step blendStep gosub glowBlend next blendValue else for blendValue=oldBlendValue to newBlendValue step -blendStep gosub glowBlend next blendValue endif oldBlendValue=newBlendValue 'set blend value for next time next tmpB1 gosub rampdown setfreq m4 'set clock to normal in case we want to download new code gosub LEDsoff 'turn off intensity=0 return rampup: for intensity=1 to 100 step 1 gosub glowBlend next intensity return rampdown: for intensity=100 to 1 step -1 gosub glowBlend next intensity return 'display blended colors on LED '(blendValue, intensity) glowBlend: redValue=blendValue * intensity / 100 redValue=255 - redValue greenValue=255-blendvalue * intensity / 100 greenValue=255-greenValue pwm LEDgreen, greenValue, 1 pwm LEDred, redValue, 1 return LEDsoff: high LEDgreen high LEDred return