EOS

Instruments

Light Sensor Solution

Programming Exercise in PicAxe

Solution

A.  Linear Scales

Text Box: 001	'BASIC converted from flowchart:
002	'     	Note: this program has reached the limit of memory in the PicAxe 08M2 chip.
003			'Routines which test for off-scale and which supress the first zeros on all scales 
004			'are not available beacuse of lack of program memory.
005			'Log scale not available for same reason.
006				'More efficient programming may free up some space.
007				
008				
009	{ ;Symbols
010	
011	symbol RotarySwitch = w0
012	symbol BlueBoxVoltage = w1
013	symbol LightIntensity = w2
014	symbol digit1 = w3
015	symbol digit2 = w4
016	symbol digit3 = w5
017	symbol digit4 = w6
018	symbol digit5 = w7
019	}
020	
021	main:
022		let dirsB = 0
023	Cell_7_4:
024							'read the Switch relative to = +5.0V
025		fvrsetup OFF			
026		adcconfig %000		    ; set power supply as ADC Vref+, 0V Vref-
027		readadc10 C.4, RotarySwitch 	
028		
029		
030		fvrsetup FVR2048  		 ; set FVR as 2.048V
031		adcconfig %011    		 ; set FVR as ADC Vref+, 0V Vref-
032		readadc10 C.2, BlueBoxVoltage       
033		
034				
035			
036		if RotarySwitch >= 978 and RotarySwitch <= 1024 then  	'log scale
037			goto Cell_10_15
038		end if
039	
040		if RotarySwitch >= 885 and RotarySwitch <= 977 then   	'100k scale
041			goto Cell_10_14
042		end if
043	
044		if RotarySwitch >= 792 and RotarySwitch <= 884 then   	'25k scale
045			goto Cell_10_13
046		end if
047	
048		if RotarySwitch >= 699 and RotarySwitch <= 791 then   	'10k scale
049			goto Cell_10_12
050		end if
051	
052		if RotarySwitch >= 606 and RotarySwitch <= 698 then   	'2500 scale
053			goto Cell_10_11
054		end if
055	
056		if RotarySwitch >= 513 and RotarySwitch <= 605 then   	'1000 scale
057			goto Cell_10_10
058		end if
059	
060		if RotarySwitch >= 420 and RotarySwitch <= 512 then   	'250 scale
061			goto Cell_10_9
062		end if
063	
064		if RotarySwitch >= 327 and RotarySwitch <= 419 then   	'100 scale
065			goto Cell_10_8
066		end if
067	
068		if RotarySwitch >= 234 and RotarySwitch <= 326 then   	'25 scale
069			goto Cell_10_7
070		end if
071	
072		if RotarySwitch >= 141 and RotarySwitch <= 233 then   	'10 scale
073			goto Cell_10_6
074		end if
075	
076		if RotarySwitch >= 47 and RotarySwitch <= 140 then    	'2.5 scale
077			goto Cell_10_5
078		end if
079	
080		if RotarySwitch >= 0 and RotarySwitch <= 46 then		'1 scale
081			goto Cell_10_4
082		end if
083		goto Cell_7_4
084	
085	Cell_10_15: 					
086					'1 scale
087		let LightIntensity = BlueBoxVoltage * 20  
088		serout C.1, N2400, (254, 128, "Light Intensity ")
089		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
090		serout C.1, N2400, (254, 192, "   ", digit1,".",digit2,digit3,digit4," lux    ")
091	
092		goto Cell_7_4
093	
094	Cell_10_14:
095					'2.5 scale       				   
096		let LightIntensity = BlueBoxVoltage * 50 
097		serout C.1, N2400, (254, 128, "Light Intensity ")
098		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
099		serout C.1, N2400, (254, 192, "   ", digit1,".",digit2,digit3,digit3," lux    ")
100	
101		goto Cell_7_4
102	
103	Cell_10_13:
104					'10 scale
105		let LightIntensity = BlueBoxVoltage * 20 
106		serout C.1, N2400, (254, 128, "Light Intensity ")
107		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
108		serout C.1, N2400, (254, 192, "   ", digit1,digit2,".",digit3,digit4," lux    ")
109	
110		goto Cell_7_4
111	
112	Cell_10_12:
113	       		      '25 scale
114		let LightIntensity = BlueBoxVoltage * 50  
115		serout C.1, N2400, (254, 128, "Light Intensity ")
116		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
117		serout C.1, N2400, (254, 192, "   ", digit1,digit2,".",digit3,digit4," lux    ")
118	
119		goto Cell_7_4
120	
121	Cell_10_11:
122	     			'100 scale
123		let LightIntensity = BlueBoxVoltage * 20   
124		serout C.1, N2400, (254, 128, "Light Intensity ")
125		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
126		serout C.1, N2400, (254, 192, "    ", digit1,digit2,digit3,".",digit4," lux   ")
127	
128		goto Cell_7_4
129	
130	Cell_10_10:
131	      			'250 scale
132		let LightIntensity = BlueBoxVoltage * 50  
133		serout C.1, N2400, (254, 128, "Light Intensity ")
134		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
135		serout C.1, N2400, (254, 192, "    ", digit1,digit2,digit3,".",digit4," lux   ")
136	
137		goto Cell_7_4
138	
139	Cell_10_9:
140	      			 '1000 scale
141		let LightIntensity = BlueBoxVoltage * 20  
142		serout C.1, N2400, (254, 128, "Light Intensity ")
143		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
144			if digit1 = $30 and digit2 = $30 then     				'$30 means the digit is zero
145				if digit3 = $30 then    						'supress first, second and third zero
146					serout C.1,N2400,(254,192,"       ",digit4,". lux   ")
147					goto Cell_7_4
148				endif
149				serout C.1,N2400,(254,192,"      ",digit3,digit4,". lux   ") 'supress first, second zero
150				goto Cell_7_4
151			endif
152			if digit1 = $30 then    							'supress first and second zero
153				if digit2 = $30 then
154					serout C.1,N2400,(254,192,"      ",digit3,digit4,". lux   ")
155					goto Cell_7_4     
156				endif 									'supress first zero
157				serout C.1,N2400,(254,192,"     ",digit2,digit3,digit4,". lux   ")
158				goto Cell_7_4
159			endif
160			serout C.1, N2400, (254, 192, "    ", digit1,digit2,digit3,digit4,". lux   ")  'first three digits non-zero
161			goto Cell_7_4
162	
163	Cell_10_8:
164	     			 '2500 scale
165		let LightIntensity = BlueBoxVoltage * 50 
166		serout C.1, N2400, (254, 128, "Light Intensity ")
167		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
168			if digit1 = $30 and digit2 = $30 then     				'$30 means the digit is zero
169				if digit3 = $30 then    						'supress first, second and third zero
170					serout C.1,N2400,(254,192,"      .",digit4," kilolux")
171					goto Cell_7_4
172				endif
173				serout C.1,N2400,(254,192,"     ",digit3,digit4,". kilolux") 'supress first, second zero
174				goto Cell_7_4
175			endif
176			if digit1 = $30 then    							'supress first and second zero
177				if digit2 = $30 then
178					serout C.1,N2400,(254,192,"     ",digit3,digit4,". kilolux")
179					goto Cell_7_4     
180				endif 									'supress first zero
181				serout C.1,N2400,(254,192,"    ",digit2,digit3,digit4,". kilolux")
182				goto Cell_7_4
183			endif
184			serout C.1, N2400, (254, 192, "   ", digit1,digit2,digit3,digit4,". kilolux")  'first three digits non-zero
185			goto Cell_7_4
186	
187	Cell_10_7:
188	     		 	 '10k scale
189		let LightIntensity = BlueBoxVoltage * 20  
190		serout C.1, N2400, (254, 128, "Light Intensity ")
191		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
192			if digit1 = $30 then    							'supress first zero
193				serout C.1,N2400,(254,192,"    ",digit2,".",digit3,digit4," kilolux")
194				goto Cell_7_4
195			endif
196		serout C.1, N2400, (254, 192, "   ",digit1,digit2,".",digit3,digit4," kilolux")
197	
198	
199		goto Cell_7_4
200	
201	Cell_10_6:
202	     			'25k scale
203		let LightIntensity = BlueBoxVoltage * 50 
204		serout C.1, N2400, (254, 128, "Light Intensity ")
205		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
206			if digit1 = $30 then    							'$30 means the digit is zero
207				if digit2 = $30 then    						'supress first and second zero
208					serout C.1,N2400,(254,192,"     .",digit3,digit4," kilolux")
209					goto Cell_7_4
210				endif
211				serout C.1,N2400,(254,192,"    ",digit2,".",digit3,digit4," kilolux")
212				goto Cell_7_4
213			endif
214			serout C.1, N2400, (254, 192, "   ",digit1,digit2,".",digit3,digit4," kilolux")
215			goto Cell_7_4
216	
217	Cell_10_5:
218	      			'100k scale
219		let LightIntensity = BlueBoxVoltage * 20
220		serout C.1, N2400, (254, 128, "Light Intensity ")
221		bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
222			if digit1 = $30 and digit2 = $30 then     				'$30 means the digit is zero
223				if digit3 = $30 then    						'supress first, second and third zero
224					serout C.1,N2400,(254,192,"      .",digit4," kilolux")
225					goto Cell_7_4
226				endif
227				serout C.1,N2400,(254,192,"     ",digit3,".",digit4," kilolux") 'supress first, second zero
228				goto Cell_7_4
229			endif
230			if digit1 = $30 then    							'supress first and second zero
231				if digit2 = $30 then
232					serout C.1,N2400,(254,192,"     ",digit3,".",digit4," kilolux")
233					goto Cell_7_4	
234				endif 									'supress first zero
235				serout C.1,N2400,(254,192,"    ",digit2,digit3,".",digit4," kilolux")
236				goto Cell_7_4
237			endif
238			serout C.1, N2400, (254, 192, "   ", digit1,digit2,digit3,".",digit4," kilolux")  'first three digits non-zero
239			goto Cell_7_4		
240					
241					
242	
243	
244	Cell_10_4:
245	       		      'log scale
246	'	serout C.1, N2400, (254, 128, "Light Intensity ")
247		serout C.1, N2400, (254, 192, "  log scale lux ")
248	
249		goto Cell_7_4
250	
251	
252	#no_data    'reduce download time

B.  Log Scales

Text Box: 001	'BASIC converted from flowchart:
002				
003				
004	{ ;Symbols
005	
006	symbol RotarySwitch = w0
007	symbol BlueBoxVoltage = w1
008	symbol LightIntensity = w2
009	symbol digit1 = w3
010	symbol digit2 = w4
011	symbol digit3 = w5
012	symbol digit4 = w6
013	symbol digit5 = w7
014	}
015	
016	main:
017		let dirsB = 0
018	Cell_7_4:
019							'read the Switch relative to = +5.0V
020		fvrsetup OFF			
021		adcconfig %000		    ; set power supply as ADC Vref+, 0V Vref-
022		readadc10 C.4, RotarySwitch 	
023		
024	debug	 
025		
026		if RotarySwitch >= 0 and RotarySwitch <= 46 then  	'log scale
027			goto Cell_10_15
028		end if
029	
030		if RotarySwitch >= 47 and RotarySwitch <= 1024 then   	  'off scale
031			goto Cell_10_14
032		end if
033	
034		goto Cell_7_4
035	
036	Cell_10_15: 					
037					'log scale
038					
039			fvrsetup FVR2048  		 ; set FVR as 2.048V
040			adcconfig %011    		 ; set FVR as ADC Vref+, 0V Vref-
041			readadc10 C.2, BlueBoxVoltage      			
042			
043		if BlueBoxVoltage >= 0 and BlueBoxVoltage < 49 then
044	'scale 1 - 3.5 lux	;never below 1 lux on this log scale
045			LightIntensity = BlueBoxVoltage * 500 + 10000
046			serout C.1, N2400, (254, 128, "Light Intensity ")	
047			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
048			serout C.1, N2400, (254, 192, "  ", digit1,".",digit2,digit3," lux     A")
049				
050		endif
051					
052	
053	
054		if BlueBoxVoltage >= 50 and BlueBoxVoltage < 99 then
055	'scale 3.5 lux - 10 lux
056			LightIntensity = BlueBoxVoltage * 130 - 3000
057			serout C.1, N2400, (254, 128, "Light Intensity ")
058			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
059				if digit1 = $30 then    						'supress the first zero 	$30 means digit1 is zero
060					serout C.1,N2400,(254,192,"  ",digit2,".",digit3,digit4," lux     B")
061					goto line200:
062				endif
063			serout C.1, N2400, (254, 192, " ", digit1,digit2,".",digit3,"  lux     B")		
064		endif
065	line200:
066	
067	
068		if BlueBoxVoltage >= 100 and BlueBoxVoltage < 149 then
069	'scale 10 lux - 35 lux
070			LightIntensity = BlueBoxVoltage * 50 - 4000
071			serout C.1, N2400, (254, 128, "Light Intensity ")
072			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
073			serout C.1, N2400, (254, 192, " ", digit2,digit3,".",digit4,digit5," lux     C")		
074		endif
075	
076	
077	
078		if BlueBoxVoltage >= 150 and BlueBoxVoltage < 199 then
079	'scale 35 lux - 100 lux
080			LightIntensity = BlueBoxVoltage * 130 - 16000
081			serout C.1, N2400, (254, 128, "Light Intensity ")
082			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
083			serout C.1, N2400, (254, 192, " ", digit2,digit3,".",digit4,digit5," lux     D")	
084		endif
085	
086		
087		
088		if BlueBoxVoltage >= 200 and BlueBoxVoltage < 249 then
089	'scale 100 lux - 350 lux
090			LightIntensity = BlueBoxVoltage * 50 - 9000
091			serout C.1, N2400, (254, 128, "Light Intensity ")
092			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
093			serout C.1, N2400, (254, 192,digit2,digit3,digit4,".",digit5,"  lux     E")		
094		endif
095	
096	
097	
098		if BlueBoxVoltage >= 250 and BlueBoxVoltage < 299 then
099	'scale 350 lux - 1000 lux
100			LightIntensity = BlueBoxVoltage * 130 - 29000
101			serout C.1, N2400, (254, 128, "Light Intensity ")
102			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
103				if digit1 = $30 then
104					serout C.1,N2400, (254,192,"   .",digit2,digit3,digit4,"kilolux F")
105					goto line300
106				endif
107			serout C.1, N2400, (254, 192, "  ", digit1,".",digit2,digit3,digit4,"kilolux F")		
108		endif
109	line300:
110		
111	
112	
113		if BlueBoxVoltage >= 300 and BlueBoxVoltage < 349 then
114	'scale 1000 lux - 3500 lux
115			LightIntensity = BlueBoxVoltage * 50 - 14000
116			serout C.1, N2400, (254, 128, "Light Intensity ")
117			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
118			serout C.1, N2400, (254, 192, "  ",digit2,".",digit3,digit4," kilolux G")		
119		endif
120	
121		
122		
123		if BlueBoxVoltage >= 350 and BlueBoxVoltage < 399 then
124	'scale 3500 lux - 10,000 lux
125			LightIntensity = BlueBoxVoltage * 130 - 42000
126			serout C.1, N2400, (254, 128, "Light Intensity ")
127			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
128				if digit1 = $30 then
129					serout C.1, N2400,(254,192,"  ",digit2,".",digit3,digit4," kilolux H")
130					goto line400
131				endif
132			serout C.1, N2400, (254, 192, " ",digit1,digit2,".",digit3,"  kilolux H")    		
133		endif
134	line400:
135		
136		
137		if BlueBoxVoltage >= 400 and BlueBoxVoltage < 449 then
138	'scale 10,000 lux - 35,000 lux
139			LightIntensity = BlueBoxVoltage * 50 - 19000
140			serout C.1, N2400, (254, 128, "Light Intensity ")
141			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
142			serout C.1, N2400, (254, 192," ",digit2,digit3,".",digit4,"  kilolux I")    		
143		endif
144	
145		
146		
147		
148		if BlueBoxVoltage >= 450 and BlueBoxVoltage <= 500 then
149	'scale 35,000 lux - 100,000 lux
150			LightIntensity = BlueBoxVoltage * 130 - 55000
151				if LightIntensity = 10000 then
152					serout C.1, N2400, (254, 128, "Light Intensity ")
153					serout C.1,N2400,(254,192,"100.0  kilolux J")
154					goto line500
155				endif
156			serout C.1, N2400, (254, 128, "Light Intensity ")
157			bintoascii LightIntensity, digit1, digit2, digit3, digit4, digit5
158			serout C.1, N2400, (254, 192," ",digit2,digit3,".",digit4,"  kilolux J") 		
159		endif
160	line500:
161	
162	
163				
164	'off scale
165		if BlueBoxVoltage > 500 then				
166			serout C.1, N2400, (254, 128, "Light Intensity ")
167			serout C.1, N2400, (254, 192, " volt. off scale")
168		endif
169		goto Cell_7_4	
170		
171	
172	
173	Cell_10_14:
174	'not on log scale
175		serout C.1, N2400, (254, 128, "Light Intensity ")
176		serout C.1, N2400, (254, 192, "Put on log scale")
177		goto Cell_7_4
178	
179	
180	
181	#no_data    'reduce download time