An exercise using symbols.

The bear

Senior Member
Hi Everyone,
I'm trying to convert my projects into using symbols.
I can't work out how to use Pause, the program starts off OK, but the pauses don't run as calculated.
The program appears to use the preceding pause, instead of my 'w2'.
Any suggestions appreciated.
Code:
		;PIR Rat Trap vers 1.6 (Symbols)(28.03.15)
		;77 Bytes

	#picaxe 08m2	
	#no_data		
	#com 8
Symbol	 PIRin = b1			;PIR i/p
Symbol	MTR = C.2			;Motor
Symbol	Pushbutton = pinc.1 	;Norm 0v
Symbol	PIR = pinc.3		;PIR state
Symbol 	paus = w2			;pause
Let		w2 = 1000			;1000mS

let dirsC = %000101 	; pins c.0 c.2 outputs
		low 0
		
init:
Label_09:	
	if Pushbutton = 0 then ;pinc.1 wired to +ve-
	goto Label_09	 ;via 1k0 & Reset switch (C.1) 
	else				
	high 0		 ;Reset indicator, LED (C.0)
	paus = w2/10	 ;100mS
	low 0
	paus = w2*20*10	 ; 20000mS   ; wait 20 seconds- 
	endif			 ; for PIR to go LOW.
main:
	paus = w2/10/20	;100mS
Label_10:
	readadc C.4, PIRin
	if PIRin < 10 then  gosub TRAP ;Dusk
      goto Label_10 	  ;Daylight
TRAP:				  ;Checking Trap every 1 secs-
	paus = w2		  ;1000mS			; when dusk/dark	
	if PIR = 1 then gosub CAPTURE ;PIR active ; 
	return
CAPTURE:
	paus = w2/10		;100mS
	high MTR		  ; switch Motor ON
	paus = w2/50	  ; 20  	 ;Motor runs 20mS 
	low MTR		  ; switch Motor OFF
	paus = w2/2	;500    ; wait 500mS
	end
	return    ; return from sub

	;Notes; Vers 1.5 in operation 02.02.15
 

techElder

Well-known member
Bear, I don't think you quite understand the uses of symbols.

You have defined the symbol "paus" to replace w2 in your program, but then you revert back to setting w2 equal to a value.

The symbol is designed to be a descriptive label for the variable. If you define the symbol, then use it exclusively.

Get creative with your symbol definitions. For example, call your symbol "LED_PAUSE". See how that is more descriptive than "paus"?

I use CONSTANT symbol definitions like "PAUSE20MILS" or "TWOSECONDPAUSE". It makes it easy to correct your pauses if you decide to change CPU frequency, too.

Also, your labels can be more descriptive. How about using "HoldForResetSwitch" instead of "Label_09" ??

Be descriptive with your verbosity. <GRIN>
 

techElder

Well-known member
With a little more time, here are some examples of symbol definitions that I use:

Code:
[color=Green]' Examples of PICAXE symbol definitions (not all from one program)

' VARIABLE ASSIGNMENTS[/color]
[color=Blue]symbol [/color][color=Black]ThresholdCount         [/color][color=DarkCyan]= [/color][color=Purple]w2[/color]
[color=Blue]symbol [/color][color=Black]LoopCount              [/color][color=DarkCyan]= [/color][color=Purple]w3[/color]
[color=Blue]symbol [/color][color=Black]BatteryVoltage         [/color][color=DarkCyan]= [/color][color=Purple]w4[/color]
[color=Blue]symbol [/color][color=Black]Mailbox                [/color][color=DarkCyan]= [/color][color=Purple]bit2[/color]
[color=Blue]symbol [/color][color=Black]Battery                [/color][color=DarkCyan]= [/color][color=Purple]bit1[/color]
[color=Blue]symbol [/color][color=Black]YouGotMailFlag         [/color][color=DarkCyan]= [/color][color=Purple]bit0[/color]
[color=Blue]symbol [/color][color=Black]i                      [/color][color=DarkCyan]= [/color][color=Purple]w2  [/color][color=Green]'w2 ($04)[/color]
[color=Blue]symbol [/color][color=Black]ii                     [/color][color=DarkCyan]= [/color][color=Purple]w3  [/color][color=Green]'w3[/color]
[color=Blue]symbol [/color][color=Black]iii                    [/color][color=DarkCyan]= [/color][color=Purple]w4  [/color][color=Green]'w4[/color]
[color=Blue]symbol [/color][color=Black]Timeout                [/color][color=DarkCyan]= [/color][color=Purple]w5  [/color][color=Green]'w5[/color]
[color=Blue]symbol [/color][color=Black]Piezo                  [/color][color=DarkCyan]= [/color][color=Purple]b12 [/color][color=Green]'b12 ; "1" or "0" if using buzzer with driver[/color]
[color=Blue]symbol [/color][color=Black]Character              [/color][color=DarkCyan]= [/color][color=Purple]b13 [/color][color=Green]'b13[/color]
[color=Blue]symbol [/color][color=Black]Elements               [/color][color=DarkCyan]= [/color][color=Purple]b14 [/color][color=Green]'b14

' CONSTANTS & PIN ASSIGNMENTS[/color]
[color=Blue]symbol [/color][color=Black]LED                    [/color][color=DarkCyan]= [/color][color=Blue]C.1
symbol [/color][color=Black]TXDATA                 [/color][color=DarkCyan]= [/color][color=Blue]C.2
symbol [/color][color=Black]LIGHTLEVEL             [/color][color=DarkCyan]= [/color][color=Purple]pinC.3[/color]
[color=Blue]symbol [/color][color=Black]BATTERY                [/color][color=DarkCyan]= [/color][color=Navy]7         [/color][color=Green]; not "C.7" or "pinC.7"[/color]
[color=Blue]symbol [/color][color=Black]THRESHOLD              [/color][color=DarkCyan]= [/color][color=Navy]0         [/color][color=Green]; zero. Test for logic level input (could be ADC level)[/color]
[color=Blue]symbol [/color][color=Black]THRESHOLDCOUNTLEVEL    [/color][color=DarkCyan]= [/color][color=Navy]100       [/color][color=Green]; noise counts[/color]
[color=Blue]symbol [/color][color=Black]LOOPCOUNTLEVEL         [/color][color=DarkCyan]= [/color][color=Navy]10000     [/color][color=Green]; reset noise levels after this[/color]
[color=Blue]symbol [/color][color=Black]BATTERYCUTOFF          [/color][color=DarkCyan]= [/color][color=Navy]614       [/color][color=Green]; ADC value for 3.00 volts (512 = 2.50 volts, 716 = 3.50 volts)[/color]
Then, while developing the program, I only refer to the symbol name; NOT the referred variable name. In other words, the symbol name replaces the variable's name within your program. This will seem obvious to you as you read it.

By the way, there is no penalty for extreme verbosity.
 

The bear

Senior Member
Hi,
Thanks for the replies.
Ibenson, I didn't realise 'pause' still had to be used, got a lot to learn.

Tex, thank you for your in depth examples and explanation. Will study and apply.
I'm going to get into some serious verbose, now that I know what it means.

Regards, Bear ..
 

The bear

Senior Member
Hi,
I hope this is an improvement?

Code:
		;PIR Rat Trap vers 1.6 (Symbols)(28.03.15)
		;71 Bytes (With added verbose)<<<<<<<<<<<<<<

	#picaxe 08m2	
	#no_data		
	#com 8
Symbol	 PIRin = b1			;PIR i/p
Symbol	MTR = C.2			;Motor
Symbol	Pushbutton = pinc.1 	;Norm 0v
Symbol	PIR = pinc.3		;PIR state
Symbol      paus = b2			;pause
Symbol	delay = w2
Symbol	MTRruntime = b0
Symbol	ResetLEDindicator = C.0
Symbol	PIR_response = C.4

Let		b2 = 100	;100mS
Let		w2 = 20000	;20000mS
Let		b0 = 20	;20mS
let dirsC = %000101 	; pins c.0 c.2 outputs
		low 0
		
init:
HoldForResetSwitch:			
	if Pushbutton = 0 then 
	goto HoldForResetSwitch 	
	else				
	high ResetLEDindicator 	 ;Reset indicator, LED (C.0)
	pause  paus	 		 ;100mS
	low ResetLEDindicator 
	pause delay	 		 ;20000mS for PIR to go LOW 
	endif			
main:
	 pause paus   		;100mS
Waitfordusk:		
	readadc PIR_response,PIRin		 ;PIR level
	if PIRin < 10 then gosub TRAPready	 ;Dusk
      goto Waitfordusk		
TRAPready:				 
	pause paus;		  	;100mS			
	if PIR = 1 then gosub CAPTURE 	;PIR active ; 
	return
CAPTURE:
	pause paus;			;100mS
	high MTR		  	;switch Motor ON
	pause MTRruntime 	 	;Motor runs 20mS 
	low MTR		 	;switch Motor OFF
	pause paus	 		;wait 100mS
	end
	return    			;return from gosub

	;Notes; Vers 1.5 in operation 02.02.15
 

techElder

Well-known member
That version is certainly easier to read.

I would also remind you that once you have defined a symbol, it is better to only use that definition. You "fell off the wagon" when you started setting values to your variables.

In your line "Let b2 = 100 ;100mS", probably should be "Let paus = 100 ; 100mS" . The compiler doesn't care, but you will care if you get into a program that you have to make many changes.

For example, if you had to change the pin or leg where the motor (MTR) is connected, all you have to do now is change the symbol line. You won't have to go all through your program editing it line by line.

The next thing to work on is organizing your symbol definitions. Group your pin symbols together, your constant symbols together and your general variable symbols together. This will make it much easier to find stuff when you go looking for it.

Ok, it isn't always worth it for a short program, but are you always going to write short programs?

I always sigh at a user program listing on the forum that is just filled with the basic PICAXE variable names like "b0", "b1" and etc. They are difficult to get up to speed on.

Bring on the VERBOSITY!



FYI, in your CAPTURE subroutine, you have an end before the return. Perhaps just for troubleshooting?

PS. I'm able to write all of this, because a lot of folks on this forum have helped me get to this level. Thank you!
 

The bear

Senior Member
@Texasclodhopper,

Thank you for your update(Feedback), I've taken on board your suggestions.

Regards, Bear..
 
Top