LED battery level indicator

LED Maestro

Senior Member
Hi,
Quick question. Is it possible for a picaxe to display the power level of 2 AA batteries (3V) on a set of LED's. I have between 1 and 16 LED's to use for the display but up to 8 would, I am sure, be plenty for the display. If I need any sort of external circuit then that would be fine.
If it is possible then could I have some pointers please,
If it is NOT possible then just say no and I will be on my merry way.
Many thanks
 

nick12ab

Senior Member
Yes it's possible.

You need to use the adcconfig and fvrsetup commands to configure the ADC for proper measurement though. You can then use if or select : case to show the level on the LED bargraph (I think there's a unary operator for this last part but the PICAXE won't work below 1.8V so that has to be considered).
 

LED Maestro

Senior Member
Hi, thanks for the reply. Sorry but could you go into a bit more detail on the adcconfig and fvrsetup as I am fairly new to programming. I mainly use picaxe for various LED applications and so this is the first time I have come across these commands.
Thanks
 

nick12ab

Senior Member
Hi, thanks for the reply. Sorry but could you go into a bit more detail on the adcconfig and fvrsetup as I am fairly new to programming. I mainly use picaxe for various LED applications and so this is the first time I have come across these commands.
Thanks
The adcconfig command only needs to be used once in a program - see this which will generate the code for you.
You use the readadc command to read the ADC value from an external potential divider (two 10ks or two 22ks would be fine) on an input pin. Before each readadc command, you must use the fvrsetup command - simply use fvrsetup FVR2048.

I believe there is a command like readadc where the internal fvr is measured in relation to the supply pins but I can't find it. It would eliminate the need for any external circuitry.
 

Technical

Technical Support
Staff member
I believe there is a command like readadc where the internal fvr is measured in relation to the supply pins but I can't find it. It would eliminate the need for any external circuitry.
calibadc.

But display of 8 LEDs on a 3V supply needs consideration - the LEDs themselves will draw a current and hence affect the readings
 

MartinM57

Moderator
Before we get carried away on measuring voltage, the request was for measuring power (or maybe 'battery level', whatever that means)

So, LEDM, voltage or power (remaining?) or something else? One is a lot easier than the others..but may not be as useful if we're talking rechargeable batteries
 

nick12ab

Senior Member
But display of 8 LEDs on a 3V supply needs consideration - the LEDs themselves will draw a current and hence affect the readings
One way to counter this is to only light one LED with the position of the lit LED indicating what the level is.
 

manuka

Senior Member
Is it possible for a picaxe to display the power level of 2 AA batteries (3V) on a set of LED's.
It's essential to first know more about the nature of the electrical load. What do you actually mean by 'power" - supply voltage or Ah. capacity? Cell internal resistance may drag down the supply value under heavy loads, yet under light intermittent loading battery shelf life may become an issue.

It's important to measure battery supply voltage under a representative load of course. DMMs which offer settings for this will switch in suitable resistors to provide load. In fact dirt cheap (US$5 range) DMMs can even be sacrificed for just this application. If space allows just build them into the project,although this may be an overkill when you just need "good-bad-ugly" supply insights!
 

Attachments

boriz

Senior Member
Are you building a stand-alone battery tester or is this intended to form part of another project and warn when the batteries are getting low?

If the latter, only one LED would do. Make it a low power type and under-drive it and flash it. You could get away with maybe half a milliamp or less (average).

*edit*

Actually, now that I think about it, the single LED could perform three duties. Firstly as a power-on indicator, secondly as a voltage reference for the ADC measurements and thirdly as the battery low indicator (Low duty flash).
 
Last edited:

AllyCat

Senior Member
Hi,

Yes, the PICaxe can only measure the Voltage of the cells and their internal impedance (by applying a load and seing how much the voltage drops). Both these will give some indication of the condition (state of discharge) of the cells, but it depends very much on the type of battery.

Two Alkaline cells may start at 3.0 volts (probably 3.2) but fall continuously, and the manufacturers don't consider them exhausted (for the purpose of their specification/ratings) until they're down to about 0.9 volt per cell (i.e. 1.8 volts). Conversely, two fully charged NiMH rechargeables won't start much above 2.5 volts, and only fall below 2.4 volts near exhaustion (which makes measuring their state of discharge quite difficult).

Also, many LEDS require a significant proportion of that 3.0 volts. Don't expect white or blue to light much below 3 volts, but fortunately red LEDS may work down to say 1.5 volts. However, they will change their brightness considerably over the range of battery voltage. For a more advanced design you could use the PICaxe PWM, and monitor the current flow through the LED(s) with another A/D converter pin. You'd need very few peripheral components, just a few resistors to feed the LEDs (and potentially monitor/create current flow from the battery) and a capacitor to filter the PWM to a more constant analogue level. And a transistor if you want to switch in a load of more than say 10mA

My first PICaxe project was actually an AA battery tester which drains a constant 100mA, logging the voltage over time, and then reports the mAhr and voltage profile when a "low" limit is reached.

Cheers, Alan.
 
Last edited:

LED Maestro

Senior Member
Hi, thanks for all the replies. The battries in quesiton are standard 1.5V alkalines. It's part of an ongoing project and it doesn't have to be very accurate, it's more of a 'show-off' thing really. Nevertheless I have got it working very well. Thank you to all for your help.
Thanks again
 

LED Maestro

Senior Member
OK, quick update/question. When I said it was working, I meant it was working in the sim. Now I have downloaded my code into my 28X2, it's not working like it does in the sim. My question is, I use 2 button switches to cycle up and down through 16 LED's. I am also using one of these switches as my adc input. This switch works fine until I activate the adc part of my code, all goes well until it goes back to main, then the adc input switch does not work even though it works in the sim. I am at a loss to explain. Could someone please advise.
Many thanks

Is there a way to deactivate adc at the end of my code and reactivate when it goes back to main?
 
Last edited:

LED Maestro

Senior Member
oh dear. I hope you are ready for this. Any comments for improvement and shortening would be appreciated. I don't have a circuit schematic as it is still on the breadboard. Needless to say buttons are on pina.1 and 0 and LED's are C.7-A.3 and B.7-B.0
thanks

code is in two post due to length

Code:
Main: symbol counter = b1
adcconfig %0000
readadc a.0,b1 
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0

led1on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.7  goto led2on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led2on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.6 goto led3on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.7 goto led1on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led3on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.5 goto led4on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.6 goto led2on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led4on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.4 goto led5on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.5 goto led3on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led5on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.3 goto led6on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.4 goto led4on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led6on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.2 goto led7on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.3 goto led5on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led7on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.1 goto led8on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.2 goto led6on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led8on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high a.3 goto led9on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.1 goto led7on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led9on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.7 goto led10on
endif 
if pina.0 = 1 then sound a.4, (124, 10) 
low a.3 goto led8on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led10on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.6 goto led11on
endif if pinc.0 = 1 then goto emitter
if pina.0 = 1 then sound a.4, (124, 10)
low b.7 goto led9on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led11on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.5 goto led12on
endif if pinc.0 = 1 then goto emitter1
if pina.0 = 1 then sound a.4, (124, 10)
low b.6 goto led10on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led12on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.4 goto led13on
endif if pinc.0 = 1 then goto emitter2
if pina.0 = 1 then sound a.4, (124, 10)
low b.5 goto led11on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led13on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.3 goto led14on
endif if pinc.0 = 1 then goto emitter3
if pina.0 = 1 then sound a.4, (124, 10)
low b.4 goto led12on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led14on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.2 goto led15on
endif if pinc.0 = 1 then goto emitter4
if pina.0 = 1 then sound a.4, (124, 10)
low b.3 goto led13on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led15on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.1 goto led16on
endif if pinc.0 = 1 then goto emitter5
if pina.0 = 1 then sound a.4, (124, 10)
low b.2 goto led14on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led16on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high b.0 goto led16off
endif if pinc.0 = 1 then goto emitter6
if pina.0 = 1 then sound a.4, (124, 10)
low b.1 goto led15on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led16off: pause 80
 do if pina.0 = 1 then sound a.4, (124, 10)
low b.0 goto led16on
endif if pinc.0 = 1 then goto emitter7
if pina.1 = 1 then sound a.4, (124, 10)
goto overload
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

overload:  
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3 
low b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0

overload1:
high c.4, c.3 pause 300
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 300
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 300
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 300
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 250
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 250
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 250
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 250
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3 pause 200
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 200
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 200
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 200
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 150
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 150
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 150
if pina.0 = 1 then overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 150
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3 pause 150
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 150
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 150
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 150
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 100
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 100
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 100
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 100
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3 pause 100
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 100
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 100
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 100
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 50
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 50
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 50
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 50
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff
 
Last edited:

LED Maestro

Senior Member
the second part of the code.
Code:
high c.4, c.3 pause 50
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 50
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 50
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 50
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 10
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 10
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 10
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 10
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3 pause 10
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2 pause 10
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1 pause 10
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3 pause 10
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0 pause 10
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1 pause 10
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2 pause 10
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3 pause 10
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3  
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2  
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1  
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3  
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0  
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1  
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2  
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3  
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3  
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2  
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1  
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3  
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0  
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1  
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2  
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3  
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff

high c.4, c.3  
if pina.0 = 1 then goto overloadoff
low c.4, c.3
if pina.0 = 1 then goto overloadoff
high c.5, c.2  
if pina.0 = 1 then goto overloadoff
low c.5, c.2 
if pina.0 = 1 then goto overloadoff
high c.6, c.1  
if pina.0 = 1 then goto overloadoff
low c.6, c.1 
if pina.0 = 1 then goto overloadoff
high c.7, a.3  
if pina.0 = 1 then goto overloadoff
low c.7, a.3 
if pina.0 = 1 then goto overloadoff
high b.7, b.0  
if pina.0 = 1 then goto overloadoff
low b.7, b.0 
if pina.0 = 1 then goto overloadoff
high b.6, b.1  
if pina.0 = 1 then goto overloadoff
low b.6, b.1 
if pina.0 = 1 then goto overloadoff
high b.5, b.2  
if pina.0 = 1 then goto overloadoff
low b.5, b.2 
if pina.0 = 1 then goto overloadoff
high b.4, b.3  
if pina.0 = 1 then goto overloadoff
low b.4, b.3
if pina.0 = 1 then goto overloadoff
goto led1on

overloadoff: 
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3 
low b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0
high c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3 
high b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0
goto led16off

emitter:
do high a.2
if pinc.0 = 0 then
low a.2 goto led10on endif
loop

emitter1:
do high a.2
if pinc.0 = 0 then
low a.2 goto led11on endif
loop

emitter2:
do high a.2
if pinc.0 = 0 then
low a.2 goto led12on endif
loop

emitter3:
do high a.2
if pinc.0 = 0 then
low a.2 goto led13on endif
loop

emitter4:
do high a.2
if pinc.0 = 0 then
low a.2 goto led14on endif
loop

emitter5:
do high a.2
if pinc.0 = 0 then
low a.2 goto led15on endif
loop

emitter6:
do high a.2
if pinc.0 = 0 then
low a.2 goto led16on endif
loop

emitter7:
do high a.2
if pinc.0 = 0 then
low a.2 goto led16off endif
loop

diagnostic:
if b1 > 250 and pina.1 = 1 then flash 
if b1 > 208 and pina.1 = 1 then flash1
if b1 > 166 and pina.1 = 1 then flash2
if b1 > 124 and pina.1 = 1 then flash3
if b1 > 82 and pina.1 = 1 then flash4
goto diagnostic 
flash:
gosub diag
high c.7 pause 100
high c.6 pause 100
high c.5 pause 100
high c.4 pause 100
high c.3 pause 100
high c.2 pause 100
high c.1 pause 100
high a.3 pause 100
high b.7 pause 100
high b.6 pause 100
high b.5 pause 100
high b.4 pause 100
high b.3 pause 100
high b.2 pause 100
high b.1 pause 100
high b.0 pause 100
pause 3000
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0
goto main
flash1:
gosub diag
high c.7 pause 100
high c.6 pause 100
high c.5 pause 100
high c.4 pause 100
high c.3 pause 100
high c.2 pause 100
high c.1 pause 100
high a.3 pause 100
high b.7 pause 100
high b.6 pause 100
high b.5 pause 100
high b.4 pause 100
pause 3000
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7, b.6, b.5, b.4
goto main
flash2:
gosub diag
high c.7 pause 100
high c.6 pause 100
high c.5 pause 100
high c.4 pause 100
high c.3 pause 100
high c.2 pause 100
high c.1 pause 100
high a.3 pause 100
high b.7 pause 100
pause 3000
low  c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7
goto main
flash3:
gosub diag
high c.7 pause 100
high c.6 pause 100
high c.5 pause 100
high c.4 pause 100
high c.3 pause 100
high c.2 pause 100
pause 3000
low c.7, c.6, c.5, c.4, c.3, c.2
goto main
flash4:
gosub diag
high c.7 pause 100
high c.6 pause 100
high c.5 pause 100
pause 3000
low c.7, c.6, c.5
goto main

diag:
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0
for counter = 1 to 25
low a.3, b.7 if pina.1 = 1 and pina.0 = 1 then goto main  high c.7, b.0 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.7, b.0 if pina.1 = 1 and pina.0 = 1 then goto main  high c.6, b.1 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.6, b.1 if pina.1 = 1 and pina.0 = 1 then goto main  high c.5, b.2 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.5, b.2 if pina.1 = 1 and pina.0 = 1 then goto main  high c.4, b.3 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180 
low c.4, b.3 if pina.1 = 1 and pina.0 = 1 then goto main  high c.3, b.4 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.3, b.4 if pina.1 = 1 and pina.0 = 1 then goto main  high c.2, b.5 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.2, b.5 if pina.1 = 1 and pina.0 = 1 then goto main  high c.1, b.6 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
low c.1, b.6 if pina.1 = 1 and pina.0 = 1 then goto main  high a.3, b.7 if pina.1 = 1 and pina.0 = 1 then goto main  pause 180
next counter
for counter = 1 to 8
high c.1, b.6 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.2, b.5 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.3, b.4 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.4, b.3 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.5, b.2 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.6, b.1 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
high c.7, b.0 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.7, b.0 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.6, b.1 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.5, b.2 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.4, b.3 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.3, b.4 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.2, b.5 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
low c.1, b.6 if pina.1 = 1 and pina.0 = 1 then goto main  pause 130
next counter
low a.3, b.7
return
 

HertzHog

Member
I have a circuit that needs a battery monitor, as the non Picaxe bits are unreliable below an arbitrary voltage. It uses an 18m2 or 20m2, I have a single 7 segment LED plus decimal point for the numerical display and the program loops. Each loop it checks the battery voltage, no resistor network required just software commands. When the program is not using the display to show a figure, on the LED, the battery monitor part, chooses which of the segments to pulse very very briefly just to show the circuit is alive and to display the battery 'power'. Seeing which segment is lit gives feedback on the battery state. I have found even with NiMiH batteries the information is useful, stable and the command used do not seem to drain the battery too much. The whole circuit when on continuously, runs for more than a month on 3 AA cells. One surprise for me was that the voltage of the batteries seemed very stable with temperature even below zero. I could post the code if you want but of course the relevant bits are buried at the moment it the program which is doing other stuff.
I would use the minimum LED number. How about a brief pair of flashes with the time between them relating to the voltage? That way you get information with minimum pin requirement to drive. HertzHog
 

nick12ab

Senior Member
First of all, where's the #picaxe directive? I had to hunt through your posts to find that it was a 28X2, so stick #picaxe 28X2 at the beginning of your code please.

Code:
Main: symbol counter = b1
Symbols should be defined before the code. If you decide to use the symbol counter in an expression etc. before the main: label then the compiler will throw an error. This is more readable too:
Code:
symbol counter = b1
main:
Code:
low c.7, c.6, c.5, c.4, c.3, c.2, c.1, a.3, b.7, b.6, b.5, b.4, b.3, b.2, b.1, b.0
Use the data direction registers! This horrid long line could be replaced with:
Code:
dirsA = %00001000
dirsB = %11111111
dirsC = %11111110
Code:
led1on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.7  goto led2on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led2on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.6 goto led3on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.7 goto led1on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif

led3on: pause 80
 do if pina.1 = 1 then sound a.4, (124, 10)
high c.5 goto led4on
endif
if pina.0 = 1 then sound a.4, (124, 10)
low c.6 goto led2on
endif pause 50
if pina.1 = 1 and pina.0 = 1 then sound a.4, (124, 10) sound a.4, (124, 10)
goto diagnostic
else loop endif
Why is there no white spacing? And also your code is a program memory hog. It looks like you could just use a variable to count what level you're currently on and go from there.

It would be really easy if you could also use C.0 as well. This is a quick version I just did which should work well on a real PICAXE but is slow in the simulator due to the nature of the simulator.
Code:
#picaxe 28x2
#no_table
#no_end
symbol upbutton = pinA.1
symbol downbutton = pinA.0
symbol counter = b4
symbol loopcounter = b5
	dirsB = $FF
	dirsC = $FF
main:
	if upbutton = 1 and counter < 15 then
		inc counter
		gosub updatedisplay
		do : loop until upbutton = 0
	end if
	if downbutton = 1 and counter <> 0 then
		dec counter
		gosub  updatedisplay
		do : loop until downbutton = 0
	end if
	goto main

updatedisplay:
	for loopcounter = counter to 15
		low loopcounter
	next loopcounter
	for loopcounter = 0 to counter
		high loopcounter
	next loopcounter
	return
 

LED Maestro

Senior Member
Really, really sorry about my code. Thanks for all the advice. The code snippets work well in the sim so will download them shortly. I didn't realise symbols could be put before main. One thing, can readadc be 'switched off' so to speak?
many thanks again
 

srnet

Senior Member
Why would you need to anyway?
In cases where you might want to reduce current, particularly in sleep mode, to an absolute minimum, the 28X2s PIC does have registers to turn off the ADC part.

I doubt this is the requirement here, and I dont know if the PICAXE firmware does this for you when its finished an AD read.
 

nick12ab

Senior Member
This is another version which looks nicer but takes up more program memory:
Code:
#picaxe 28x2
#no_table
#no_end
symbol upbutton = pinA.1
symbol downbutton = pinA.0
symbol counter = b4
symbol loopcounter = b5
symbol someword = w3
    dirsB = $FF
    dirsC = $FF
    gosub updatedisplay
main:
    if upbutton = 1 and counter < 15 then
        inc counter
        gosub updatedisplay
        do : loop until upbutton = 0
    end if
    if downbutton = 1 and counter <> 0 then
        dec counter
        gosub  updatedisplay
        do : loop until downbutton = 0
    end if
    goto main

updatedisplay:
    lookup counter,($0001,$0003,$0007,$000F,$001F,$003F,$007F,$00FF,$01FF,$03FF,$07FF,$0FFF,$1FFF,$3FFF,$7FFF,$FFFF),someword
    pinsC = b7
    pinsB = b6
    return
 

westaust55

Moderator
No, you can only disconnect the normal input sensor from a pin, using the adcsetup command.

Why would you need to anyway?

Of course, you can still use the pin as a normal input.
@nick12ab,
can I ressectfully suggget that you don't use alternative fonts which are very small to highlight PICAXE BASIC keywords.
The combination of small text size and the light blue colour (from the link) make the text far less readable.
On an iphone for example at a size where I can read the usual black text, I must zoom in to about 3x to read these small linked words
and even on a desktop LCD screen it is slightly more obscure than the regular text.
maybe try standard font or make the text bold instead.

by way of example:

No, you can only disconnect the normal input sensor from a pin, using the adcsetup command.

No, you can only disconnect the normal input sensor from a pin, using the adcsetup command.
 

LED Maestro

Senior Member
Thanks for the code suggestions. They work very well. I have managed to correct the problem I was having. My thanks again to all on the forum.
Many thanks.
 
Top