;------------------------------------------------------------------------------------------------ ; ; PROGRAM NAME BEDSIDE LAMP CONTROLLER Mk4 ; ; PROGRAM DATE 9 May 2008 ; ; AUTHOR Jeff MONEGAL ; ; PROCESSOR PICAXE 08M ; ;------------------------------------------------------------------------------------------------ ; ; This version uses both buttons, UP/ON and DOWN/OFF, as dim up and dim down functions. ; Pressing and releasing either will turn the lamp on or off. Pressing and holding either will ; activate the dim up and dim down functions. ; ; When ARMED the system is ready to turn on the light when the alarm clock sounds. When the ; system is disarmed it will ignor any alarm clock sound but will still respond to the ; DIMMING functions of both buttons ; ;------------------------------------------------------------------------------------------------ ; Flags register (b0) has the following bit functions ;------------------------------------------------------------------------------------------------ ; ; bit0 dimming direction flag. If set dimming is up. ; ;------------------------------------------------------------------------------------------------ symbol time = w6 symbol brightness = w5 rem holds actual lamp brill level (0 to 65536) symbol flags = b0 symbol counter = b5 symbol bright = b6 rem contains current brightness level (0 to 255) symbol delay = b7 ;------------------------------------------------------------------------------------------------ symbol lamp = pin2 symbol up = pin4 symbol down = pin1 symbol alarm = pin3 ;------------------------------------------------------------------------------------------------ bright = 0 rem clear brightness level delay = 25 pwmout 2,0,0 rem make sure the system starts with the lamp off low 2 ;------------------------------------------------------------------------------------------------ main: if up = 0 then decode_up if down = 0 then decode_down rem look to see if either button pressed if alarm = 0 then alarm_activated rem alarm triggered so go do its thing goto main ;--------------------------------------------------------------------------------------------------- decode_up: bit0 = 1 counter = 20 goto timing decode_down: bit0 = 0 counter = 20 timing: pause 25 if up = 1 and down = 1 then but_released rem button released before 1 second so toggle lamp counter = counter - 1 if counter = 0 then but_held rem after 1/2 second button still pressed so dim lamp goto timing but_released: if bit0 = 1 then lamp_on goto lamp_off but_held: if up = 0 then dim_up goto dim_down ;--------------------------------------------------------------------------------------------------- lamp_on: if bright > 245 then main rem lamp is at max brightness bright = bright + 2 rem increase the lamp brightness by 1 gosub update rem update the brightness level pause 1 goto lamp_on ;--------------------------------------------------------------------------------------------------- lamp_off: bright = 0 rem turn the lamp off gosub update goto main ;--------------------------------------------------------------------------------------------------- dim_up: pause 250 dim_up_lp: if up = 1 then main rem button has been released if bright > 245 then dim_down_wait rem max brightness bright = bright + 2 rem increase the brightness by 2 at a time gosub update rem transfer the brightness level to the lamp pause 50 goto dim_up_lp ;--------------------------------------------------------------------------------------------------- dim_down: pause 250 dim_down_lp: if down = 1 then main rem button released if bright < 5 then dim_down_wait bright = bright - 2 gosub update pause 50 goto dim_down_lp dim_down_wait: gosub release goto main ;--------------------------------------------------------------------------------------------------- update: brightness = bright * 4 rem multiply brightness by 4 as pwmout uses 10 bits pwmout 2,255,brightness return ;--------------------------------------------------------------------------------------------------- alarm_activated: pause 500 auto_up: if alarm = 1 then main if up = 0 then hold_dim_up if alarm = 1 then stop_dim_up if bright > 250 then hold_dim_up bright = bright + 1 brightness = bright * 4 pwmout 2,255,brightness pause 100 rem this number sets the dim up speed. Change to suit goto auto_up stop_dim_up: bit0 = 0 rem swap dim direction flag to down goto main hold_dim_up: gosub release rem wait for any pressed button to be released hdu_lp1: pause 50 if up = 0 then stop_dim_up if alarm = 0 then hdu_lp1 goto stop_dim_up ;--------------------------------------------------------------------------------------------------- release: pause 50 if up = 0 then release if down = 0 then release return ;-----------------------------------------------------------------------------------------------------