fl { PropForth4.5 Using LED as sensor Recommended High bright LED type. Usage: Executing WORD"LED" when bright around LED. LED blink after 2seconds. When it's dark abobe LED, LED is blinking. 14/07/2011 11:09:48 } hex 1F8 wconstant ctra 1FA wconstant frqa 1FC wconstant phsa clkfreq 1 lshift constant 2sec c wconstant _ledp \ LED anode d wconstant _ledn \ LED cathode variable dark \ ( -- ) measurement time while _ledn is high : led_read \ reverse bias for LED 3 _ledp lshift invert outa COG@ and 2 _ledp lshift or outa COG! 3 _ledp lshift dira COG@ or dira COG! cnt COG@ 3e8 + 3e8 waitcnt drop \ wait for LED-Capacitor to charge 1 frqa COG! 0 phsa COG! \ start counter 1 _ledn lshift invert dira COG@ and dira COG! \ set _ledn to input begin ina COG@ 1 _ledn lshift and 0= if 1 else 0 then until \ wait until _ledn is low 0 frqa COG! \ stop counter ; : get_value led_read phsa COG@ ; \ ( -- n1 ) n1:phsa value : >2sec? swap dup cnt COG@ swap - 2sec > if drop 1 else swap 0 then ; \ ( n1 n2 -- n1 n2 t/f )check if 2sec passed \ n1:system counter n2:phsa value : LED_On 3 _ledp lshift invert outa COG@ and 1 _ledp lshift or outa COG! 3 _ledp lshift dira COG@ or dira COG! ; : LED 20000000 _ledn or ctra COG! \ get value when bright cnt COG@ get_value begin get_value max >2sec? until \ finished sense-data LED_On 100 delms dup a u/ + dark L! 500 0 do led_read phsa COG@ dark L@ > if LED_On \ 3 _ledp lshift invert outa COG@ and 1 _ledp lshift or outa COG! \ 3 _ledp lshift dira COG@ or dira COG! 20 delms then loop ; : test 20000000 _ledn or ctra COG! 200 0 do get_value . cr 64 delms loop ; decimal