Another code snippet (light detection)

Post Reply
gkowen
Posts: 114
Joined: Sun Jan 31, 2010 9:02 pm

Another code snippet (light detection)

Post by gkowen »

Image

Code: Select all

'This routine is how I calibrate my light detector circuit (shown above). I put the circuit outside and count
'the LED flashes when its just too dark for pics. I use this for movie mode. Here's what
'you will see. At startup, there will be 10 quick flashes. This lets you know you are 
'beginning. Then there is a pause for 2.3 seconds. Then the number of flashes for the 
'hundreds digit take place. Then 3 quick flashes tell you the next is tens digit. Then 
'3 quick flashes and the ones digit.
'EXAMPLE #1: Light level measures 214
'		10 quick flashes, 2.3 second delay,2 longer flashes (100s),3 quick flashes, 1 longer flash (10s)
'		3 quick flashes, 4 longer flashes, 3 quick flashes, then 5 second pause.  REPEAT
'
'EXAMPLE #2: Light level measures 072
'		10 quick flashes, 2.3 second delay,0 longer flashes (100s),3 quick flashes, 7 longer flash (10s)
'		3 quick flashes, 2 longer flashes, 3 quick flashes, then 5 second pause.  REPEAT


low 4
input 2      				

b0 = 0
b1 = 0

Do 
	for b5 = 1 to 10		'this routine gives 10 quick flashes
		high 4
		pause 100
		low 4
		pause 100
	next b5
	sleep 1			'2.3 second wait
	readadc 2,b1		'read light level on pin2
	'b1 = 154                               'change the value to test
	
 	if b1 > 99 then		'hundreds digit flash
		b1 = b1 - 100
		high 4
		pause 300
		low 4
		pause 300
	end if
	if b1 > 99 then		'hundreds digit flash
		b1 = b1 - 100
		high 4
		pause 300
		low 4
		pause 300
	end if
	
	gosub quickflash		'3 quick flashes to indicate hundreds digit done
	
	if b1 > 9 then		'tens digit flash
		do until b1 < 10
			b1 = b1 - 10
			high 4
			pause 300
			low 4
			pause 300
		loop
	end if
	
	gosub quickflash		'3 quick flashes to indicate tens digit done
	
	if b1 > 0 then		'ones digit flash
		do until b1 < 1
			b1 = b1 - 1
			high 4
			pause 300
			low 4
			pause 300
		loop
	end if
	
	gosub quickflash		''3 quick flashes to indicate ones digit done
	
	sleep 2
loop
quickflash:
	pause 1000			'1 second pause
	for b5 = 1 to 3		'3 quick flashes
		high 4
		pause 100
		low 4
		pause 100
	next b5
	
	pause 1000			'1 second pause
return
Last edited by gkowen on Tue Jun 29, 2010 7:56 pm, edited 1 time in total.
tbass
Posts: 3251
Joined: Sun Jan 31, 2010 9:07 pm
Location: Central PA near PSU
Contact:

Re: Another code snippet (light detection)

Post by tbass »

That's interesting. Why the 100's digit twice?
gkowen
Posts: 114
Joined: Sun Jan 31, 2010 9:02 pm

Re: Another code snippet (light detection)

Post by gkowen »

The possibilities for 100s is 0,1, or 2. Rather than use a loop like I did for the others, I just did it with straight code. It could be done in a loop too. I just have never changed it. I use this to keep from taking movies in the dark.
tbass
Posts: 3251
Joined: Sun Jan 31, 2010 9:07 pm
Location: Central PA near PSU
Contact:

Re: Another code snippet (light detection)

Post by tbass »

Good thinking.
Post Reply