' An extension of my first picaxe project,.. ' This is a combination of the ultimate flashlight and a small toy for ' entertaining children and engineers. Beeps and flashing lights, neat. ' ' 7/4/06 - changed pulse brightness in off mode to quiter value ' 7/4/06 - added routines for LDR, switched hardware hookups ' 7/4/06 - rid of strobe2 mode, added the ever important molest mode ' 7/5/06 - code optimization, LDR more responsive ' 'hardware hookups for this project symbol LED=1 'LED symbol PIEZO=2 'piezo symbol BUTN=pin3 'N.O. pushbutton symbol LDR=4 'CDS cell for sensing light level 'variable space symbol mode=b0 'mode symbol strobeval=b1 'strobe value symbol LEDpulseval=b2 'brightness of LED during pulse mode symbol var1=w2 'local variable symbol var1b=b4 'byte1 of var1 symbol var2=w3 'local variable symbol devcnt=w4 'count how many cycles for timeout feature symbol randw=w6 'var for random number symbol randb=b12 'byte of randw 'parameters symbol napval=1 'nap for 36 ms symbol devoffcnt=832 'how often to flash when off? 30/.036, approx 60 seconds symbol devoncnt=16666 'timeout for left on, 60*10/.036, approx 10 minutes symbol devbreathcnt=156 'how often for breaths?, 6/.036, approx 10 breaths/min symbol devheartbtcnt=26 'how often to beat heart? 1/.036, approx 1 bps symbol maxmodes=5 'maximum number of modes available '0=off, 1=on, 2=strobe, 3=alive, 4=ldrmusic, 5=molest 'initialization init: mode=0 strobeval=0 devcnt=0 gosub tellmode 'tell user device is alive 'main loop modeloop: devcnt=devcnt+1 'increment loop counter if BUTN = 0 then nomodechange pause 10 'debounce delay mode=mode+1 'change mode if btn pressed if mode <= maxmodes then cont mode=0 'start over when at maximum mode cont: devcnt=0 gosub tellmode nomodechange: if devcnt < devoncnt or mode = 5 then noautooff mode=0 'switch off if forgotten and left on devcnt=0 noautooff: branch mode,(devoff, devon, strobe1, alive, ldrmusic, molest) 'execute current mode mainret: if mode = 4 then nosleep nap napval 'powerdown for a few milliseconds nosleep: goto modeloop 'and loop again 'device is off, pulses occasionally to let use know where it is devoff: low LED if devcnt < devoffcnt then devoffend LEDpulseval=100 'keep brightness down when flashlight is off gosub pulseled devcnt=0 devoffend: goto mainret 'device is on, has auto off feature devon: high LED goto mainret 'device is in strobe mode for fun strobe1: branch strobeval,(strobeon,strobeoff) strobeon: high LED strobeval=1 goto strobecont strobeoff: low LED strobeval=0 strobecont: goto mainret 'device is in alive breathing mode for fun alive: LEDpulseval=255 'full brightness in this fun mode low LED var1=devcnt % devheartbtcnt 'get modulus (from loop counter) for heartbeat if var1 !=0 then alivecont1 ;if not time, skip heart beat pulse gosub pulseled 'beat heart alivecont1: var1=devcnt % devbreathcnt 'get modulus (from heartbeat counter) for breathes if var1 !=0 then alivecont2 'every so often take a breath sound PIEZO,(250,40) 'inhale = breathe in "hiss" pause 600 'hold breath sound PIEZO,(230,80) 'exhale = breathe out "hiss" alivecont2: goto mainret 'play tones proportional to amount of light on LDR ldrmusic: readadc LDR,var1 'read LDR value var1=255-var1-128 'LDR inpit is inverted! next time hook LDR to +5v instead of GND 'scale var1 to 0-255 range via offset and multiplier sound PIEZO,(var1,3) goto mainret 'pulse LED pulseled: for var1= 0 to LEDpulseval step 4 ' counter loop so LED has multiple PWM cycles pwm LED,var1,1 ' PWM pin 2 LED one cycle increasing pulse width next ' effect is a pleasing surging brightness increase for var1= LEDpulseval to 0 step -4 ' When warm b2 decreases so step less/beat faster pwm LED,var1,1 ' PWM pin 2 LED one cycle decreasing pulse width next ' gives a fading brightness instead of sudden off return 'beep at random intervals only when its dark molest: if devcnt < randb then molestend 'skip out if delay hasn't passed devcnt=0 'reset loop counter readadc LDR,var1 'read LDR value if var1<100 then molestend 'skip out if box is in the light random randw 'get new random interval sound piezo, (125,100) 'annoy user molestend: goto mainret 'beep piezo to tell user what mode device is in tellmode: for var1=0 to mode sound PIEZO, (120,20,0,20) next return