Most efficient (least power) way to loop while reading an input?

abenn

Senior Member
I've got a couple of 08M2 circuits acting as light detectors to switch battery-powered lights on at dusk. I use a simple loop to check the light level, and am just wondering if there's a more efficient code that would reduce the 08M2's current drain while it's waiting for dusk. Would lengthening the PAUSE 5000 help, for instance? Or is it just not worth bothering about?

Code:
main:

    	readadc c.4,b1   			; light detector
	if b1<70 then goto lights_on	    
	pause 5000
	goto main
 

hippy

Technical Support
Staff member
Something like the following would be most efficient...

Code:
main:
    	do
     	  nap 1
	  readadc c.4,b1
	loop until b1<70    
    	goto lights_on
or

Code:
main:
     	nap 1
	readadc c.4,b1
	if b1 >= 70 then goto main    ; Note the comparison is >=    
    	goto lights_on
You can increase the NAP value for greater savings, or use SLEEP if that doesn't make things too unresponsive.
 

StefanST

New Member
(1) Depending on the level of the light, you can use various delays
Code:
main:
SlowLoop:
     	nap 14   ; 4min
QuickLoop:
     	nap 6   ; 1sec
	readadc c.4,b1
	if b1 >= 200 then goto SlowLoop
	if b1 >= 70 then goto QuickLoop
    	goto lights_on
(2) If the light detector circuit (LDR?) has a larger power consumption, you can also control the power supply of this circuit. The detector circuit can be fed through the selected picaxe output pin only during the measurement time.
Code:
high PowerPin
pause 100   ; ms
readadc c.4,b1
low PowerPin
The level of optimization depends on the ratio of the light source consumption to the control electronics current.
 

abenn

Senior Member
Thanks for those suggestions. I'll look at SLEEP and NAP.

The point about the power consumption of the LDR and its associated resistor is a good one, and the idea of powering it up only when needed to make a measurement is worth following up too.
 
Top