Auto power-off (after time elapse) and remembering states?

pjl83

Member
Hi all,

I've had an idea for a relatively simple indicator to be used at home using 2 push buttons and 2 led's.

I'll have a 2 led's. One will be marked "AM" and one "PM". A simple push button will toggle the 2. (If the AM is on then it will go off and PM will light). I am happy with this part as the code is quite simple and I have done it before whilst playing with the 08M.

The clever bit will be for battery saving. After the led lights for x amount of time (lets say 20 seconds) I want the unit to power down to save battery life. Can this be done? If so, can the status be retained e.g. if PM is displayed when the power goes off, PM needs to be displayed when powered back up by pressing another button.

If this isn't possible then does anybody know what power the 08M takes to read constantly 24/7 without outputting anything?

Thanks
Paul
 

hippy

Ex-Staff (retired)
PICAXE can remember things across power-offs; simply use internal Data Eeprom plus WRITE and READ commands; write what the data should be when it changes, read that when it powers on. It's easy enough to have three states; unknown ( for example after download ), AM, or PM.

The big question is 'how powered-off' you really want or need ?

You can build hardware to switch the entire circuit off so it draws mere nA of current, but you should also be able to achieve just uA of current through sleeping and low clock speed operation. That may actually be suitable and should be easier to implement.

I'm afraid I didn't really understand the last sentence of your post.
 

hippy

Ex-Staff (retired)
Thanks cdngunner; that sounds like a reasonable interpretation. If it's just looking for the button pushes then it can probably achieve those uA consumption figures through slowing down and sleeping.

An 08M with no attempt to save power draws just a few mA, so even a small or short term slowing and/or sleeping can greatly reduce that.
 

eclectic

Moderator
snipped
If this isn't possible then does anybody know what power the 08M takes to read constantly 24/7 without outputting anything?

Thanks
Paul
@Paul

"reading", "AM" , "PM", "24/7" ?
I assume it's for a clock of some sort.

Could you provide more info on the project please?

E
 

pjl83

Member
I'm afraid I didn't really understand the last sentence of your post.
My fault, probably not the best worded question!

The small panel (box) would have the 2 led's, a button to toggle the 2 led's and then a button to display the state and start the timer when pressed. With this set-up, there would be a main loop in the programme that is constantly scanning and waiting for a button press, which I assume would still consume power from the batteries? "how powered off" would depend on how much power it takes to scan constantly waiting for the button press.

Thank you for the internal data eeprom hint, I will have a look through the manuals for it now.
 

pjl83

Member
@Paul

"reading", "AM" , "PM", "24/7" ?
I assume it's for a clock of some sort.

Could you provide more info on the project please?

E
Sure, it's pretty simple and may seem pointless to most but believe me the idea works well. My wife and I are in and out of the house at different times everyday depending on what we're up to with work, gym etc.... I got bored of the same twice-daily conversation/phone call asking each other - "Have you fed the dogs?" We currently have a paper dial which we turn to point to either AM or PM to show when the last feed was. I thought it would be fun to make a small box with a couple of led's and a button instead.

It may seem a little "over-the-top" but hey it's fun to make gadgets so why not. When you go to feed the dogs you press the "status" button to find out whether they've been fed or not, if not then feed them and toggle the led with the other button. Seemples! :D

I could just use a push button and an "ON-ON" toggle switch but that would be boring and I wouldn't learn anything :p
 

Minifig666

Senior Member
So will the PICAXE be keeping time? If so it won't be very good at it.
Does an interrupt still work in sleep mode? If it does you could have it come back from a sleep loop if a button is pressed. If the PICAXE isn't keeping time I suppose you could use an SR latch and have the PICAXE turn itself completely off and use the EEPROM to hold the current state.
 

Janne

Senior Member
I think this would be a fun project to implement a supercapacitor on. You would have a ~1F cap that would power the picaxe and the leds. The show button would charge the capacitor from the battery, and enable the picaxe and led run as long as there is charge in the capacitor. When the state was changed, the new state would be stored in EEPROM.
 

pjl83

Member
Not in the slightest, now that I know.
I might make one for Her Ladyship's cat.

But, from bitter experience,
what hapens if/when you forget? :)

Earache awaits. :-(

e
That's the beauty of showing the last feed rather than the next feed. The worst that can happen is that they get fed twice so no harm done.

I'll have a look at all of the suggestions above.

Thanks to all, I'm sure you've not heard the last of this! :)
 

hippy

Ex-Staff (retired)
Will it be more annoying that instead of the conversation, or simply looking at the dial, you have to push the button to have the LED's light ?

I would think about the practical ergonomics before going for low power and a usually off display. You don't want an even longer discussion about how impractical it is and what a waste of time it was :)
 

pjl83

Member
Fair comment. The food is stored in the cupboard with all the other dog stuff so I just think it's a bit of a waste to have it lit all day and only viewed for a few seconds every day.

I've just thought about complicating things even more :rolleyes: How about just one button with a long press to display the status and then a short press on the same button to toggle once powered up.
 

hippy

Ex-Staff (retired)
One button makes sense. I'd go for an instant wake-up though and the same or slightly longer for toggling. You can debounce the button so it doesn't accidentally toggle when you've only pressed to look at status.
 

pjl83

Member
Sounds like fun to me, I have the toggle working now with a de-bounce in the code, I just need to play around and suss out the other bits.
 

Andrew Cowan

Senior Member
If the device is to be kept in the cupboard along with the food, what about triggering from an LDR that shows the cupboard door is open?

Or a PIR detecting someone has walked up to the device?

A
 

pjl83

Member
I'm finding it difficult to interpret the manual again. I use the examples shown but I can never seem to read beyond and adapt the example for my own use.

The eeprom, read and write all have information there but don't show any of them working together so I'm stuck again. After putting my last 08m to sleep :confused: I thought I'd get the toggle and remember part right before I tried the sleep part again but I just can't see where the information is. :(

Here's what I've tried -

Code:
toggle 4 'turn green led on

main:
	read b0, b1
	if pin3 = 1 then change 'goto change when button is pressed
	goto main
	
change:
	pause 300 'for switch bounce
	toggle 4 'toggle green led
	toggle 2 'toggle red led
	write b0, b1
	goto main
I have tried "reading more" after I made the mistake before of jumping in too quick before understanding but I can't find the information.

I need to "write" the state of the led's (pin2 and pin4) and then read and replicate the state on power-up.

Thanks in advance.
 

hippy

Ex-Staff (retired)
Take a step back. Take it one step at a time; throwing code together in the hope it will work rarely does.

Let's start with ...

Eeprom 0,( "ABCD" )
Read 0, b1
SerTxd( b1 )

What does that do ? Why ?

So what if we change the zero in the read ?

And if we make that number a variable, say 'b0' ?

Now let's start again but do a little more ...

Eeprom 0,( "ABCD" )
Read 0, b1
SerTxd( b1 )
Write 0, "X"
Read 0, b1
SerTxd( b1 )

It's all about understanding what is happening and why, then determining what you need to use to achieve something and how that something can be brought about.
 
Last edited:

pjl83

Member
Take a step back. Take it one step at a time; throwing code together in the hope it will work rarely does.
I agree, which is why I was trying to read and use an example that was in the manual, but I couldn't see anything else that helped.

Let's start with ...

Eeprom 0,( "ABCD" )
Read 0, b1
SerTxd( b1 )

What does that do ? Why ?

So what if we change the zero in the read ?

And if we make that number a variable, say 'b0' ?
Eeprom 0,( "ABCD" ) - Names eeprom 0 ABCD?
Read 0, b1 - Reads the data in ABCD and writes it into b1?
SerTxd( b1 ) - I have no idea. The description nor the example make any sense to me at all. :confused:
 

pjl83

Member
No, I couldn't find a mention of "Hard-Reset" anywhere. I tried using the "Clear Hardware Memory" mentioned on page 47 of manual 1 but nothing happened, it was treating it as if the power wasn't connected. In the end I just swapped the chip for a fresh one and all is back to normal now?

Edit - I've just found it on page 45. I didn't try that but will do. Thankyou.
 
Last edited:

hippy

Ex-Staff (retired)
Time to experiment, physical chip or simulator, and time to go to the Manual and read the description of each command.

You don't know - and that's completely fair enough and don't think I'm having a go at you - so you have to get to know. The best way to do that is to try something and see what happens, compare that with how it's described, and then it becomes what if I twiddle that, how does that change things ? Is it still consistent with how it's described ?

Eventually you'll come to an understanding of what the command does and it will tie up with how it's described, and you'll start to get a feel of how the command can be used beyond the simple examples given.

The light comes on, and as you nibble through the entire cornucopia of the PICAXE plater you're slowly becoming an expert. All of us here are either going through or went through that, with the PICAXE or something comparable.

As an aid - actually start with this. It's the same core as before but has a couple of helpers ...

#Picaxe 08M
#Terminal 4800
Pause 2000
Eeprom 0,( "ABCD" )
Read 0, b1
SerTxd( b1 )
Do:Loop
 
Last edited:

eclectic

Moderator
No, I couldn't find a mention of "Hard-Reset" anywhere. I tried using the "Clear Hardware Memory" mentioned on page 47 of manual 1 but nothing happened, it was treating it as if the power wasn't connected. In the end I just swapped the chip for a fresh one and all is back to normal now?

Edit - I've just found it on page 45. I didn't try that but will do. Thankyou.
I think that you need to update to
version 5.3.2

e
 

Attachments

lbenson

Senior Member
> Eeprom 0,( "ABCD" ) - Names eeprom 0 ABCD?
>Read 0, b1 - Reads the data in ABCD and writes it into b1?
>SerTxd( b1 ) - I have no idea. The description nor the example make any sense to me at all. :confused:

Some confusion, yes.

The eeprom area in a picaxe consists of +locations+ which may contain +values+. EEPROM 0 designates the first location in the eeprom area. Each location contains a single byte value--values between 0 and 255, which include the values of ascii charactors, like "A" and "B". The command 'EEPROM 0,("ABCD")' loads 4 byte values into eeprom, starting at location 0, so locaton 0 contains "A", location 1 contains "B", and so on.

When you do 'Read 0,b1', you read the value of location 0 and place it into b1 (actually just another location on the chip which has been given the name of 'b1'). b1 will then contain the value "A". If you do 'Read 1,b1', b1 will contain the value, "B".

Try running hippy's short programs in the simulator. Single step through the commands using the "{" button. Look at the values in the eeprom window. Then try changing them with something like 'write 0,"X".

Try this little program in the simulator. Watch what happens in the "Memory" window. Note that 65 is the ascii value for the letter "A", and 66 is the ascii value for the letter "B" (see http://www.asciitable.com/ if you want to see what the decimal and hex values are for the ascii characters).

Code:
b13 = "A"
b12 = 0
do while b12 < 26
  write b12, b13
  inc b13 ' increment value to next letter in the alphabet
  inc b12 ' increment location in eeprom where letter is to be written
  pause 1000
loop
 

pjl83

Member
Thank you both for the hints and explanations.

From looking at the simulator in both of your examples and playing with some of the values I have managed to figure it out enough to write the first part of the code that toggles the LED's and remembers where it is for power off and back on.

Code:
'"1" = 49 ascii value
'"2" = 50 ascii value
'pin4 = green LED
'pin3 = push button
'pin2 = red LED


read 0, b3 'read value in 0 and put it in b3
if b3 = 49 then green 'if b3 (0) = 49 (1) then green
if b3 = 50 then red 'if b3 (0) = 50 (2) then red

'this section is for first start-up only as 0 will be blank
high 4 'turn green LED on
b1 = "1" 'write 49 into b1
write b12, b1 'write b1 (49) into 0

main:
	read 0,b3 'read value in 0 and put it in b3
	if b3 = 49 and pin3 = 1 then red 'if 0 is 49 and button is pressed then red
	if b3 = 50 and pin3 = 1 then green 'if 0 is 50 and button is in then green
	goto main 'loop
	
green:
	pause 300 'for switch bounce
	high 4 'green LED on
	low 2 'red LED off
	b1 = "1" 'write 49 into b1
	write b12, b1 'write b1 value (49) into 0
	goto main
	
red:
	pause 300 'for switch bounce
	high 2 'red LED on
	low 4 'green LED off
	b2 = "2" 'write 50 into b2
	write b12, b2 'write b2 value (50) into 0
	goto main
I will play around with this more before I go any further with the project to be sure that I understand it fully. The only bit I'm not 100% sure of is where the "write b12" part relates to slot 0 in the memory. I took it from the example above and I can see that it works, but not entirely sure how or why :rolleyes:

Once I'm completely happy with this part I'll move on to the sleep function and the 2 stage button presses - short press for "wake-up" and long press for "toggle"

Thanks again for the help and patience.
Paul ;)
 

lbenson

Senior Member
In the example code I gave, b12 contained the address in eeprom to which the value was written--in the do loop, it had values from 0 to 25.

In your code, writing your initial state to b12 works as you expect only because the initial value of b12 is 0 when the program starts to run, so you wrote to eeprom location 0. If you move this code to some other location after b12 had been used for another value, you would have to initialize b12 to the location where you wanted to write the value.

You can omit your initialization code, and set the value of eeprom location 0 to "1" by using the command, "EEPROM 0,("1"). The EEPROM command is not one which is executed as your program is running--the program editor uses it to initialize the values in the eeprom before any code is executed.

Note that except for particular cases, it makes more sense in a command to use an alphabetical character rather than the numeric value of the ascii character--that is, use "if b3 = '1'" rather than "if b3 = 49".

Even more clear would be to use symbols:

#symbol switch-to-green = "2"
...

if b3 = switch-to-green then ...

Glad you're making progress in figuring this stuff out.
 
Last edited:
Top