a hand with some code please.. Power consumption

I posted a while ago about making a foaling alarm for a horse. Below is the code im using. I read about clock the speed of the 08m down to save on power, how would i go about doing this? Also any one got any ideas on how to compress the program some what, ive use the whole memory of the chip and still need to add a few lines of code!! Also is the disablebod / enablebod used correctly, will it have any significant effect?

thanks for any help.


Code:
'Foaling alarm.
'when a horse gives birth, it lies on its side.  This code detects this by using 
'mercury switches. after about 30 seconds the picaxe will control three small 
'relays which simulates pressing the 'attached nokia phones buttons.  The 
'Phone will ring and hang up the last two missed calls, This 'makes for easy 
'set up.  A reed switch is fitted, which when activated by a magnet will run a 
'reset routine that will clear the last missed calls.


'pin 1 = controls the down button on the phone
'pin 2 = controls the select button on the phone
'pin 0 = controls the power button on the phone
'pin 4 = reed switch input
'pin 3 = mercury switch input
main:

sleep 3         'held in a sleep state for power conservation. Checks the
                   'reed switch and the mercury switch every 6 seconds.



for B1 = 1 to 10                       ' if mercury switch is constant for 23 seconds then                                                               'start   ring sequence.
if Pin4 = 1 then goto reset          'reed switch for resetting the alarm.
if Pin3 = 0 then goto main           go back to main if mercury switch is not active.
sleep 1
Next B1
goto Ring

Reset:

'first check to make sure the reed switch is still active then tun the 
'phone on and cycle  through the menu to reset the last missed 
'calls,and leave the phone on to allow new "missed calls" to be set up

if pin4 = 0 then goto main
high 0
sleep 1
low 0
sleep 5
gosub select1
gosub down
gosub select1 

for B3 = 1 to 3
gosub down
next B3

for B4 = 1 to 2
gosub select1
next b4

pause 1000
gosub powershort
sleep 100
gosub powerlong
goto main

ring:

'goes through the process of ringing the last missed call
'and hanging up, it then goes to the next missed call.
'a delay of ten minutes is added before it starts check 
'the mercury switches again.

gosub powerlong                                
gosub select1                                     
gosub down                                       
for B4 = 1 to 3
gosub select1
next B4
for B5 = 1 to 7
gosub down
next B5
gosub select1
sleep 3
for B6 = 1 to 2
gosub powershort
sleep 1
next B6

'ring the next number in the missed calls list.

gosub select1
'down
high 1
pause 100
low 1
pause 100

'select1:                    'have run out of subroutines (16 max),  
for B6 = 1 to 2           'have to do it manually 
high 2
pause 100
low 2
pause 100
next B6

'down:
high 1
pause 100
low 1
pause 100

'select1:
high 2
pause 100
low 2
pause 100
 
'down:
for B7 = 1 to 7
high 1
pause 100
low 1
pause 20
Next B7

'select1:
high 2
pause 100
low 2
pause 100
sleep 3

high 0
sleep 1
low 0

sleep 240

goto main

'sub routines - these simulate pressing the buttons.  Power
'long is a long hold to turn the phone 'on and off.  
'Power short just takes it back to the home page.

down:
high 1
pause 100
low 1
pause 100
return


select1:
high 2
pause 100
low 2
pause 100
return

powershort:
high 0
pause 100
low 0
pause 100
return

powerlong:
high 0
sleep 1
low 0
sleep 5
return
-------------------------

The reset sequence clears the last missed calls from the phone which the pic is connected too. The ring sequence dials and hangs up the last two missed calls.
 
Last edited:

eclectic

Moderator
Mark.
Just a few comments.

1. You'll find info. on underclocking here, amongst others

http://www.picaxeforum.co.uk/showthread.php?t=2233&highlight=under+clocking+poke

2. Have a look at "interrupt" / "setint" in manual 2

3. Considering the "criticality" and cost of the "output", would it be safer
to pay an extra couple of $ for the 18X?

4. Could you post a circuit/block diagram, so that folks can get a better overall
"picture" of what's required?
e.g,
'pin 1 = down button
'pin 2 = select button
'pin 3 = power button
What is connected to PIN4?

5. I haven't read the listing fully,
but there appear to be some GOTO and RETURN conflicts.
Has this been simulated?

e
 
Last edited:

Dippy

Moderator
I agree. People may be able to help more with code and circuit suggestions if all the information is provided.

And also commenting the code helps others to help you.
Some people may have a lot of spare time, but.....

Also, can you bracket your code in the code window too please?
 

ValueAdd

Senior Member
I agree. People may be able to help more with code and circuit suggestions if all the information is provided.

And also commenting the code helps others to help you.
Some people may have a lot of spare time, but.....

Also, can you bracket your code in the code window too please?
Although more into reading than publishing at the moment, I agree with Hippy about commenting. I like to look at the code others develop for ideas while I build up my own experience .

While your code looks relatively simple, it is hard to know what is going on.

For a good recent example (commented upon by others) have a look at example code in the first post on this thread.
http://www.picaxeforum.co.uk/showthread.php?t=9904

As I understand it, comments are only in your source file in the programm editor and do not take up space in the actual picaxe chip.
 

crossthreaded

New Member
First I have to say that I agree with comments about the code appearance. The three comments at the head which establish the connection between the pins and some names should be symbol statements, that would tidy up a lot.

As for code length. you have many sequences of the form:
Code:
pin control
sleep (or pause)
pin control
sleep (or pause)
These would be an ideal candidate for a subroutine with register parameters which could specify which pin, and the duration of the pauses.

e.g. (and forgive me if this doesn't compile since I'm not going to check it)
Code:
symbol pin_id = b3
symbol high_pause = b4
symbol low_pause = b5

let pin_id = 1 ; should be a meaningful name, not just a number!
let high_pause = 100
let low_pause = 50
gosub pause_with_parameters
..

pause_with_parameters:
high pin_id
pause high_pause
low pin_id
pause low_pause
return
 

hippy

Technical Support
Staff member
Commenting is also useful for getting people to 'buy into' what you're doing even if not essential, if they can understand the code they are likely more enthused about it and more willing to look at it. It doesn't matter what the code is just that they feel they understand what's going on, more empathy than anything else.

The finest example of useful documentation must be that which accompanies Michael Craig-Martin's An oak tree which is my personal favourite piece of art. It may look like a glass of water on a shelf but it is an oak tree ...

http://www.cl.cam.ac.uk/~ig206/oak_tree.html

In the particular code here "select1", "powerlong", "powershort"; what do they mean, what do they do, why are they there, how are they used, and when, what do they achieve ? It's passing on the knowledge which makes those names something meaningful which only exists inside the head of the author.

It will also help undertanding when names are self explanatory; does "ring" mean comes here when the phone rings, or go there to initiate a call ? For a label like "DialTheNumberToCall" it's both clear what that routine does in its own right, and when used elsewhere it's clear what that code is going off to do.

Code optimisation don't need any documentation as a prerequisite but it helps in getting people stepping up to the table in the first place. Much like non-commercial translating of something in a foreign language is more likely to happen if it's writing on a subject one has an interest in.

To somewhat contradict what I do say about meaningful names and labels, crossthreaded's idea of parameters is a good one, but to simplify that I'd have gone for something like ...

which = 1 : length = 100 : delay = 50 : Gosub PulseThePin

Short sweet names for the parameters and all on one line. Ultimately it's how quickly and easily can a reader get an understanding of what something is likely to mean without looking anywhere else.

The only question here is do such lines and subroutine calls take more code than writing it out long hand ? Always try it with test code before make a huge change which makes code bigger !
 
Last edited:
point taken

ill have a play round with the code and add comments/documentation.

thanks for the help so far.

does underclocking affect power draw in sleep mode?

Will be back soon...
 
Last edited:

eclectic

Moderator
Re the "Art" in post #6

http://www.cl.cam.ac.uk/~ig206/oak_tree.html

Am I off topic or OTT? Or am I iconoclastic?

I can think of several comments.

1.Transmogrification.
2.Russell's teapot.
3.The Abraham Lincoln quote starting “You can.....
4. “The Comedians” star Charlie Williams's “Ngombe!”

I really don't mind paying Tax. Honestly. If it benefits society.

But in this case, I don't know whether to cry or laugh.

e.

Sorry Mark.D. Back to the plot.
 

moxhamj

New Member
Hippy, that Oak Tree is an absolute crackup! It has brought a smile to my face and made my day. It is an almost Monty Python sketch - one can even read it with their silly accents - the rather posh and earnest interviewer and the eccentric artist answering. Like the Two Sheds sketch.

I'm off to redocument all my code to say something completely different to the actual code.

high 1' this line reads an analog voltage
 

hippy

Technical Support
Staff member
high 1' this line reads an analog voltage

That's just brilliant.

I really do find there's something deeply brilliant about The Oak Tree. I think you have to see it, read the 'interview' look again, maybe a few times, and just clicks into place, it is An Oak Tree. Or maybe not if that's not your cup of tea / mug of coffee / glass of water.
 

lahaye

Member
Hello Mark Pigeonstone (you have a Dutch name; I have allowed myself to translate it into English, just in case you didn't know yet ;) )

I understood that you also need to reduce the size of your code, well here's my two cents:
When I translated some code that consisted of only gotos to a code that only cosited of gosubs it grew from 69 bytes to 129 bytes (7 gosubs/gotos), nothing else was changed. So if you can replace some gosub/return statements with gotos that might help you squeeze in that extra bit of code. Not sure whether this is possible/feasible with your code.
Also, as I understand it, gotos are bad programming practice (spaghetti code...?), but it might do the trick.

Nice project.


Greetings

Florian
 
Hey

I was under the impression that a goto is a permanent jump in the code? how do you get it back to the position wheres its supposed to be?

Any hints?

Cheers

Mark Pigeonstone - BTW was in Holland 2 weeks ago, beautiful country, now im back in cold New Zealand - wind rain and snow, just about winter..
 
Last edited:

Andrew Cowan

Senior Member
You can't get back to where you were - so this only works unless the subroutine is only used once (or you use variables, but that adds more code).
Code:
main:
high 2
if pin6=1 then alarm
main2:
high 4
goto main

alarm:
high 5
goto main2
If you wanted to use variables (but that is more code), you would set b1 as 1 the first time you got to the routine, set it as 2 the second time you go, then you can put:
Code:
if b1=1 then first
if b1=2 then second
Andrew
 

lahaye

Member
some code

Hello Mark,

Yes indeed, as Andrew already pointed out gotos can't return like gosubs can, he has also already indicated a way to work around this.
I have used this previously one some code, so I allowed myself to rewrite your code using this method.
My editor (5.2.0, Picaxe 08M setting) states your code size as 238 bytes,
my "hybrid" code (contains gosubs as well as gotos) uses 231 bytes, the fully "gotoed" code 224, so only marginal savings.
As I don't know what kind and how much code you want to add I am not sure whether this helps you or not.
If your added code requires your previous structure the hybrid code might be the way to go, but only a handful of bytes were saved.
I added some comments and indicated them with ' # #

I also noticed that there seems to be no powerlong routine at the end of the second phone call. So if the next call routine is implemented the phone will turn off. Maybe this is intended. Maybe there is some code I missed which deals with this issue. If not the delay (sleep 240) could be simply reduced to 120.

Please note that I am an ABLOLUTE (no shouting just emphasising) beginner so I may have rendered the code totally useless so should you choose to implement it please keep this in mind.

As suggested before a bigger PICAXE would hold more code and also has more gosub routines, I thing manual 1 has a listing of the number of gosubs you can call with each of the PICAXE chips.



hybrid code:

Code:
'Foaling alarm.
'when a horse gives birth, it lies on its side.  This code detects this by using 
'mercury switches. after about 30 seconds the picaxe will control three small 
'relays which simulates pressing the 'attached nokia phones buttons.  The 
'Phone will ring and hang up the last two missed calls, This 'makes for easy 
'set up.  A reed switch is fitted, which when activated by a magnet will run a 
'reset routine that will clear the last missed calls.


'pin 1 = controls the down button on the phone
'pin 2 = controls the select button on the phone
'pin 0 = controls the power button on the phone
'pin 4 = reed switch input
'pin 3 = mercury switch input
main:

wait 3        	 'held in a wait state for power conservation. Checks the
                   'reed switch and the mercury switch every 6 seconds.



for B1 = 1 to 10                     ' if mercury switch is constant for 23 seconds then start   ring sequence.
if Pin4 = 1 then goto reset          ' reed switch for resetting the alarm.
if Pin3 = 0 then goto main           ' go back to main if mercury switch is not active.
wait 1
Next B1
goto Ring

Reset:

'first check to make sure the reed switch is still active then tun the 
'phone on and cycle  through the menu to reset the last missed 
'calls,and leave the phone on to allow new "missed calls" to be set up

if pin4 = 0 then goto main
high 0
wait 1
low 0
wait 5
gosub select1
gosub down
gosub select1 

for B3 = 1 to 3
gosub down
next B3

for B4 = 1 to 2
gosub select1
next b4

pause 1000
gosub powershort
wait 60
gosub powerlong
goto main

ring:

'goes through the process of ringing the last missed call
'and hanging up, it then goes to the next missed call.
'a delay of ten minutes is added before it starts check 
'the mercury switches again.

gosub powerlong                                
gosub select1                                     
gosub down                                       
for B4 = 1 to 3
gosub select1
next B4
for B5 = 1 to 7
gosub down
next B5
gosub select1
wait 3
for B6 = 1 to 2
gosub powershort
wait 1
next B6

ring2:
	 B2=1
	 
	 if B0=0  then goto select1
	 if B0=1  then goto down
	 if B0<4  then goto select1
	 if B0=4  then goto down
	 if B0=5  then goto select1
	 if B0<13 then goto down
	 goto select1
	 sleep 3
	 high 0
	 sleep 1
	 low 0

	 B0=0
	 B2=0



sleep 240

goto main

'sub routines - these simulate pressing the buttons.  Power
'long is a long hold to turn the phone 'on and off.  
'Power short just takes it back to the home page.

down:
high 1
pause 100
low 1
pause 100
if B2=0 then return
endif inc B0
goto ring2

select1:
high 2
pause 100
low 2
pause 100
if B2=0 then return
endif inc B0
goto ring2

powershort:
high 0
pause 100
low 0
pause 100
return

powerlong:
high 0
wait 1
low 0
wait 5
return
Good luck with your horses.

BTW: At the moment we also have a lot of wind and rain in Holland (but no snow).

[Edit]
I removed the updated code from the post as the post was to lenghty.
It became a bit larger (226 bytes). Please note some delays were alterd to make simulation faster, (sleep 240, and another sleep).
The new code should work as expected
[/Edit]
Cheers

Florian
 

Attachments

Last edited:

westaust55

Moderator
a hand with some code please - try this

Mark,
I believe (not tried) that when using the ON…GOSUB command, it only counts as one GOSUB. Therefore instead of the section of code:

Code:
gosub select1
gosub down
gosub select1 

for B3 = 1 to 3
gosub down
next B3

for B4 = 1 to 2
gosub select1
next b4

[B]pause 1000[/B]
gosub powershort
[B]sleep 100[/B]
gosub powerlong
goto main
You can try a construct like this:

Code:
FOR b13 = 0 TO 9
ON b13 GOSUB select1, down, select1, down, down, down, select1, select1, powershort, powerlong
NEXT b13
For the pause and sleep commands, you could look to making these the first instruction in the subroutines so that the delays are not lost.
 

hippy

Technical Support
Staff member
There's quite a lot of program memory used overhead with ON GOSUB but that can be worked round.

Instead of ...
Code:
FOR b13 = 0 TO 9
  ON b13 GOSUB select1, down, select1, down, down, ...
NEXT b13
This is more space efficient ...
Code:
FOR b13 = 0 TO 9
  GOSUB Choose
Next B13
:
Choose:
  ON b13 [b]GOTO[/b] select1, down, select1, down, down, ....
Still keep the RETURN's at the end of 'select1', 'down' etc
 

westaust55

Moderator
Hippy,

Good idea. We can all learn something new. :)


westaust55
-------------------------------------------------------------------------
The first day you don't learn something new is your first day in a wooden box
 

lahaye

Member
more [better] code

Hello,

I had a feeling that I might learn something from this....

The BRANCH structure turns out to be much more code-space effective than the if...then gotos. And they are easier to handle.
Just altering the code (2.3 not the hybrid) with branch saved over 20 bytes. So the count is now down to 200 bytes.

I tried to implement the on...gosub code but when simulating there is a problem or strange situation:
It only executes the first jump at position 0 and simply continues the code(when stepping through manually I have to push the step button seven times to get past the on..gosub line). So the actual on...gosub branching is never executed.
The syntax is exactly as specified by westaus55 and manual 2.
I didn't use the for...next code.
Here is the line of code:

On B1 gosub select1, down, select1, down, down, select1, select1

Code of a typical subroutine:

select1:
high 2
pause 100
low 2
pause 100
inc B1
return
So the increment of the offset is in the gosub routines

When I tried it with the for...next routine it worked...:confused:
What am I missing?
I realize (or think) that the for next loop ensures, besides incrementing the OFFSET variable, that the On...gosub routine is called repeatedly. So if for...next is omitted the On...gosub is only called once, after the return the program executes the next line. But why do I have to push the step through button 7 times to get past that line?
Not so important, but strange.

The working code (as already posted):
For B1 = 0 to 8
On B1 gosub select1, down, select1, down, down, select1, select1, reset1,reset2
next B1

The issue with this seems to be code space. Implementing this over the entire code resulted in about 300 bytes of required space.
If I got the idea of the method hippy has suggested correctly implementing it will get rid of all but one for...next loop. It still requires 3 on...gosub statements (as 3 unique sequence of subroutines are needed: reset, ring1, ring2). This requires still more than 250 bytes of memory.
I could be wrong here of course. Not sure if I got hippys idea right.

[Edit Nr.2, reply count 22]
Nope I didn't get hippys Idea right. Those of us who can read will have noticed hippy wrote on goTO not on goSUB.
[/Edit Nr.2, reply count 22]

As far as I can tell the goto structure with branch seems to be the most memory effective in this particular case.
There might be some more glitches in there as well as missing or altered delays in the new code (2.4, branch structure)

Code:
'Foaling alarm.
'when a horse gives birth, it lies on its side.  This code detects this by using 
'mercury switches. after about 30 seconds the picaxe will control three small 
'relays which simulates pressing the 'attached nokia phones buttons.  The 
'Phone will ring and hang up the last two missed calls, This 'makes for easy 
'set up.  A reed switch is fitted, which when activated by a magnet will run a 
'reset routine that will clear the last missed calls.


'pin 1 = controls the down button on the phone
'pin 2 = controls the select button on the phone
'pin 0 = controls the power button on the phone
'pin 4 = reed switch input
'pin 3 = mercury switch input
main:

wait 3        	 'held in a wait state for power conservation. Checks the
                   'reed switch and the mercury switch every 6 seconds.

for B2  = 1 to 10		             ' if mercury switch is constant for 23 seconds then start   ring sequence.
if Pin4 = 1 then goto reset1	       ' reed switch for resetting the alarm.
if Pin3 = 0 then goto main           ' go back to main if mercury switch is not active.
next B2					 ' #	 # only if Pin4 = 0 and Pin3 = 1 the program will get to this point and inc B2
wait 1		 
bit0=1					 ' #   #  in order to get to the "ring if...then" statements (handeled via distribute)...
						 ' #   # ... bit0 has to be 1. If smaller: reset routine is implemented
						  
Distribute:					 ' #   # jumps to ring routines or executes reset procedure
      inc B1				 ' #   # these inc of b= (increment: B1=B1+1) makes the program know which part of the... 
						 ' #   # ...program it must execute next [Branch OFFSET] the subroutines will always return...
						 ' #   # ...to the distribute routine.
						   
	
	if bit0 = 1 then goto ring1    ' #   #  this will jump  to the ring sequences if bit0=0 the remaining... 
	goto reset2				 ' #   #  ...reset sequence is executed.
Reset1:	
	'first check to make sure the reed switch is still active then tun the 
	'phone on and cycle  through the menu to reset the last missed 
	'calls,and leave the phone on to allow new "missed calls" to be set up

	if pin4 = 0 then goto main
	high 0
	wait 1
	low 0
	wait 5
Reset2:	
	Branch B1,(select1, down, select1, down, down,select1, select1, reset3, reset4)       '#  # as B1 gets incremented first main					   
						 									  '#  # has been added to the branch code
	B1=0					' #   # reset of B1 when branching is over		  '#  # to position 0 (never used!)
	goto main				' #   # return to main when Reset routine is over.					   


			    
						 
Reset3:					

	sleep 1				' #  # replacing pause 1000 with pause 100 (not the same) or sleep 1 (same) 1 byte is saved.
      goto powershort	 

Reset4:					' #  # if B1=9 progam will jump from Distribute to this point
   
	sleep 1				      
	goto powerlong
								
	



ring1:

	'goes through the process of ringing the last missed call
	'and hanging up, it then goes to the next missed call.
	'a delay of ten minutes is added before it starts check 
	'the mercury switches again.
	
	' #   # main has been added to the branchcode because b1 is not 0 but 1; goto main will never be executed
	if bit1 = 1 then goto ring2
	
	branch B1, (main, powerlong, select1, down, select1, select1, select1, down, down, down, down, down, down, down, select1)
	sleep 3
	if B1 = 13 then goto powershort
	sleep 1
	if B1 = 14 then goto powershort
	sleep 1
	bit1=1			' #  # program will now jump from ring1 to ring2
	B1=0				' #  # resetting OFFSET variable
	
ring2:
	branch B1, (main, select1, down, select1, select1, down, select1, down, down, down, down, down, down, down, select1)  
   	sleep 3	 
	high 0
	sleep 1
	low 0	
	sleep 2
	
	bit0=0			
	bit1=0
	B1=0				' #   # "reset" OFFSET variable

	goto main			' #   # end of phone-call-sequence							

'sub routines - these simulate pressing the buttons.  Power
'long is a long hold to turn the phone 'on and off.  
'Power short just takes it back to the home page.

down:
	high 1
	pause 100
	low 1
	pause 100			
	goto distribute		
				 
select1:
	high 2
	pause 100
	low 2
	pause 100
	goto distribute

powershort:
	high 0
	pause 100
	low 0	
	pause 100
	goto distribute

powerlong:
	high 0
	wait 1
	low 0
	wait 5
	goto distribute

regards,
Florian
 

Attachments

Last edited:

westaust55

Moderator
How ON ... GOSUB works

I tried to implement the on...gosub code but when simulating there is a problem or strange situation:
It only executes the first jump at position 0 and simply continues the code(when stepping through manually I have to push the step button seven times to get past the on..gosub line). So the actual on...gosub branching is never executed.
The syntax is exactly as specified by westaus55 and manual 2.
I didn't use the for...next code.
:
:
:
When I tried it with the for...next routine it worked...
What am I missing?
The ON ... GOSUB command only executes once, so with some code like:

command A
ON b0 GOSUB sel0, sel1, sel2
command B

when the ON ... GOSUB returns from the subroutine the program resumes execution at the next command (command B) so even if b0 has been incremented the ON ... GOSUB is not repeated.


by adding the FOR ... NEXT loop, when the first subroutine is completed and returns, the program commences with the next command which is now a NEXT command. This forces the program to increment the variable (b0) used as a pointer and go back to the ON ... GOSUB command where the selection variable is now incremented causing the next subroutine to be executed.


Hope that explanation is clear enough.

And remember, as Hippy has indicated, the slightly different construct that he has suggested have a simple GOSUB inside the loop and using ON... GOTO saves some space.
 
Last edited:

lahaye

Member
Hello westaus,

On on...goto:
I must apologize for my somewhat confusing post; I had discovered this only-execute-once nature of on...gosub; somewhere hidden in my post I wrote:
"I realize (or think) that the for next loop ensures, besides incrementing the OFFSET variable, that the On...gosub routine is called repeatedly. So if for...next is omitted the On...gosub is only called once, after the return the program executes the next line.".
I only realized this behaviour halfway through my post and didn't rewrite that part prior to this "discovery" completely so I created a wrong impression (I was working on two different versions of that code at the same time plus writing and changing the post... got a bit confusing :eek:)
Thank you for your effort anyway. The people who got confused by my post will surely appreciate it too...

The only issue left with this is that I have to push the single-step-simulator button 7 times (the number of addresses in that on...gosub) before it would jump to the next line. On the first push it jumps to adress0 then does nothing until, it seems, all the addresses have been cycled through.
But this doesn't seem to be a real issue (as it does no harm), I was just curious.

On hippys "compression":
[Edit]
Yep, I got that wrong. I thought it said "on gosub" but it was "on goto" (equal to branch). The goto was even emphasised.
uups
Please ignore the rest of the post. BTW first part isn't very "exiting" either... but you must have noticed by now...
[/Edit]
I am not sure if I am understanding it correctly but this is as I understand it: the code uses a single for...next loop do generate an incrementing offset and repeated calling of the on...gosub command.
As opposed to separate for...next loops for each on...gosub. That is, as far as I understand it, the point where the program bytes are saved.
For each of the 3 gosub sequences (reset, ring1, ring2) a separate on...goto command is still needed.

As far as I have experienced the on...goto commands use considerable amounts of memory so the program actually gets larger then before.

As said before I am not sure if I get hippy right, but if it saves only on the for...next loops it seems that it can't reduce memory usage sufficiently.

It's late now, but I think I will look at the memory use of on...gosub and for...next tomorrow.

greetings

Florian
 
Last edited:

lahaye

Member
A good day to all of you,

Somehow I missed that GOTO in hippys code and the post from westaus who stated this also. Uuups
As I discovered the real meaning of hippys suggestion (the subroutine called by the for next loop is an on goto [branch], not an on gosub), I have decided to give it a try.

And I must say it worked beautifully:

Altering Marks original code to fit the new format went really quick. The code has been compressed to a mere 202 bytes. It retained most of it's original structure.

So this is the new code using for...next gosub in combination with on...goto [branch]:

Code:
'#   # original size : 238 bytes
'#   # new     size : 202 bytes

'pin 1 = controls the down button on the phone
'pin 2 = controls the select button on the phone
'pin 0 = controls the power button on the phone
'pin 4 = reed switch input
'pin 3 = mercury switch input

main:

sleep 3         'held in a sleep state for power conservation. Checks the
                   'reed switch and the mercury switch every 6 seconds.



for B2 = 1 to 10                     ' if mercury switch is constant for 23 seconds then                                                               'start   ring sequence.
if Pin4 = 1 then goto reset          'reed switch for resetting the alarm.
if Pin3 = 0 then goto main           'go back to main if mercury switch is not active.
sleep 1
Next B2

goto Ring

Reset:

'first check to make sure the reed switch is still active then tun the 
'phone on and cycle  through the menu to reset the last missed 
'calls,and leave the phone on to allow new "missed calls" to be set up

	if pin4 = 0 then goto main
	high 0
	sleep 1
	low 0
	sleep 1

	for B1 = 0 to 7
	gosub reset1
	next B1

reset1:
	on B1 goto select1,down, select1, down, down, down, select1, select1

	pause 1000
	gosub powershort
	sleep 2
	gosub powerlong
	
	goto main

ring:

'goes through the process of ringing the last missed call
'and hanging up, it then goes to the next missed call.
'a delay of ten minutes is added before it starts check 
'the mercury switches again.

	for B1 = 0 to 13 
	if bit0=0 then gosub ring1
	next B1


ring1:
	on B1 goto powerlong, select1, down, select1, select1, select1, down, down, down, down, down, down, down,select1

	sleep 3

	for B6 = 1 to 2
	gosub powershort
	sleep 1
	next B6

	for B1 = 0 to 13
	gosub ring2
	next B1

ring2:
'ring the next number in the missed calls list.
	on B1 goto select1, down, select1, select1, down, select1, down, down, down, down, down, down, down, select1

	high 0
	sleep 1
	low 0
	sleep 4

	goto main

'sub routines - these simulate pressing the buttons.  Power
'long is a long hold to turn the phone 'on and off.  
'Power short just takes it back to the home page.

down:
	high 1
	pause 100
	low 1
	pause 100
	return


select1:
	high 2
	pause 100
	low 2
	pause 100
	return

powershort:
	high 0
	pause 100
	low 0
	pause 100
	return

powerlong:
	high 0
	sleep 1
	low 0
	sleep 5
	return

Not sure if it really does everything as the old one as far as I can tell it does. Delays have been altered for better simulation.


regards

Florian
 

Attachments

WOW

i am very impressed with the help. I printed off the code and have been going through it, reading up, thanks so much for the help.

Ill simulate it on the phone tonight and let you know how i go.

Cheers

Edit:

Got everything up and running, changed a bit of stuff around and it works great. I underclocked the chip and disabled the brownout during the testing phase to conserve battery. My cheapish meter wont even register a current, so it must be pretty low :) here the code

Code:
'Foaling alarm.
'when a horse gives birth, it lies on its side.  This code detects this by using 
'mercury switches. after about 30 seconds the picaxe will control three small 
'relays which simulates pressing the 'attached nokia phones buttons.  The 
'Phone will ring and hang up the last two missed calls, This 'makes for easy 
'set up.  A reed switch is fitted, which when activated by a magnet will run a 
'reset routine that will clear the last missed calls.




'pin 1 = controls the down button on the phone
'pin 2 = controls the select button on the phone
'pin 0 = controls the power button on the phone
'pin 4 = reed switch input
'pin 3 = mercury switch input

init:

let dirs = %00000111                      'set input outputs
poke $8F,%00000000		          'underclock to 31Khz
disableBOD	

main:

sleep 3        				 'held in a sleep state for power conservation. Checks the
                   			'reed switch and the mercury switch every 6 seconds.
for B2 = 1 to 9                     ' if mercury switch is constant for 23 seconds then                                                               
if Pin4 = 1 then goto reset          'reed switch for resetting the alarm.
if Pin3 = 0 then goto main           'go back to main if mercury switch is not active.
sleep 1
Next B2

goto Ring

Reset:
poke $8F,%01100000                                   'increase clock speed
pause 100
enableBOD		
		        


'first check to make sure the reed switch is still active then tun the 
'phone on and cycle  through the menu to reset the last missed 
'calls,and leave the phone on to allow new "missed calls" to be set up
	pause 500
	if pin4 = 0 then goto init                    	          'ensures not a randon reset
	sleep 5
	if pin4 = 1 then goto init				'if magnet is still there it will put it into a lowpoermode (for sending places e.t.c)							
	high 0
	sleep 1
	low 0
	sleep 5

	for B1 = 0 to 7
	gosub reset1
	next B1

reset1:
	on B1 goto select1,down, select1, down, down, down, select1, select1

	sleep 130
	gosub powerlong
	
	goto init               ' so that the brownout is disabled and the chip is underclocked again.

ring:

enableBOD				        
poke $8F,%01100000                  'increase clock speed

'goes through the process of ringing the last missed call
'and hanging up, it then goes to the next missed call.
'a delay of ten minutes is added before it starts check 
'the mercury switches again.

	for B1 = 0 to 14 
	if bit0=0 then gosub ring1
	next B1


ring1:
	on B1 goto powerlong, select1, down, select1, select1, down, select1, down, down, down, down, down, down, down,select1

	sleep 5

	gosub powershort
	sleep 1
	gosub select1
	gosub powershort
	sleep 1
	

	for B1 = 0 to 12
	gosub ring2
	next B1

ring2: 'ring the next number in the missed calls list.

	on B1 goto select1, down, select1, select1, select1, down, down, down, down, down, down, down, select1

	Sleep 5
	
	gosub powerlong
	
      sleep 234                    'will wait 10 mins between phone calls
	goto init

'sub routines  - simulate pressing the buttons on the phone.

down:
	high 1
	pause 100
	low 1
	pause 100
	return


select1:
	high 2
	pause 100
	low 2
	pause 100
	return

powershort:
	high 0
	pause 100
	low 0
	pause 1000
	return

powerlong:
	high 0
	sleep 1
	low 0
	sleep 7
	return
once again, thanks for all ur help :)
 
Last edited:
Top