rem "BedroomFan.BAS" 9/13/2007 rem The purpose is to cool my bedroom in summer when the outside temperature is rem lower than the room temperature. The fan speed is reduced after we have rem retired and put out the light, to be practically noiseless. rem The fan assembly consists of 6 120mm, 12V computer server fans in a frame, rem which fits snugly into the sliding window groove. rem The fan assembly plugs into the control box, which includes the power supply. rem The programming connector doubles for optionally logging the data onto a Secure Digital module. #picaxe 08M init: symbol Temp = b1 symbol Light = b2 symbol Mode = b3 'Off = 0, FullSpeed = 2, Quiet Mode = 1 symbol FileNo = b4 symbol Phase = b5 symbol DarkTime = b6 symbol OldMode = b7 symbol Cycles = w5 symbol PWMDutyCycle = w6 symbol PWMPeriod = 250 symbol DCFast = 5 symbol DCSlow = 845 symbol DCOff = 995 symbol FanDrive = 2 symbol LightLimit = 50 symbol UpperLimit = 90 symbol LowerLimit = 80 Data 0,(174) 'File number - Set to one higher than last re-load main: let Cycles = 0 let Mode = 0 let OldMode = 5 let Phase = 251 let PWMDutyCycle = DCOff high 2 looping: pause 337 'ms: 1000 recordings per day (each 256 loops) if pin3 = 1 then lightest 'Pin 3 is low if the manual switch is override ON let Mode = 4 'This is a "State Machine" with Mode as node values let PWMDutyCycle = DCFast lightest: 'Auto operation readadc 4,Light 'Read Light (photocell on Control) if Light > LightLimit then notdark if Mode = 0 then setspeed 'don't turn on fan if off let PWMDutyCycle = DCSlow 'reduce fan noise if on let Mode = 1 goto setspeed notdark: let DarkTime = 0 'don't countup to new file setspeed: if OldMode = Mode then branching 'only set speed if it changes pwmout FanDrive,PWMPeriod,PWMDutyCycle OldMode = Mode branching: Phase = Phase + 1 'States of making an entry on the SD Module Branch Phase,(looping,SDTimeout,looping,OpenFile,looping,WriteFile,looping,CloseFile) goto looping StartFan: let Mode = 2 let PWMDutyCycle = DCFast goto looping SDTimeout: setfreq m8 sertxd ("S 1 5",13) setfreq m4 goto looping Openfile: 'Opens the file Read 0,FileNo setfreq m8 sertxd ("O 1 A /F",#FileNo,".CSV",13) setfreq m4 goto looping WriteFile: 'Writes the data or heading Cycles = Cycles + 1 'Keeps track of loops setfreq m8 sertxd ("W 1 17",13,#Cycles,",",#Mode,",",#Light,",",#Temp,13,10) setfreq m4 readadc 1,Temp 'Temp if Temp > UpperLimit then StartFan if Temp > LowerLimit then looping let PWMDutyCycle = DCOff let Mode = 0 goto looping CloseFile: 'Closes the file setfreq m8 sertxd ("C 1",13) setfreq m4 if DarkTime > 170 then looping DarkTime = DarkTime + 1 if DarkTime <> 166 then looping 'New file # after 4 hours of continuous darkness FileNo = FileNo + 1 Write 0,FileNo 'Store File # between power cycles Cycles = 0 goto looping