Sub Routines.

Ambutech

New Member
HI all

I am ATTEMPTING to get my head into programming picaxe chips. Not done any real programming before.

My problem..

I have some code that runs on an 18M2 chip on the 18 way project board. This, in turn, then operates an Arduino 8 way relay switch board. I run this using a Radio Control TX/RX., multiple jabs on/off depending on which relay I was to operate.

I would like to use one relay to "Flash" on/off while still using the other relays on the board. "JabOn" goes off to a subroutine that toggles the relay at 1/2 sec intervals until a "JabOff" is sent, leaving the other 7 relays free to operate whilst the Flash relay does its' thing.

I have managed to turn the relay on via a GOSUB routine, but then everything locks up and nothing else can be done.

After talking to a local guru, I realise I need to "TEST" for a JabOff inside the subroutine, but can't figure out how to let the subroutine and leave it running until the relavant JabOff is received.

Any clues will be gratefully received

Rgds Ken
 

Ambutech

New Member
This is my Trial Code. Taking out the "FLASH" parts in this code leaves the code as is already running fine

The original is from a fellow member of the radio control boat club I belong to.

Code:
'
' PICAXE 18M2 CONNECTIONS
'
' leg 01 (C.2)    (input2)      r/x input
' leg 02 (C.3)    (serial out)
' leg 03 (C.4)    (serial in)    connect to 0v
' leg 04 (C.5)    (reset)        tied to Vdd via 4k7  
' leg 05 0V       (Vss)           0v
' leg 06 (B.0)    (output0)    channel1
' leg 07 (B.1)    (output1)    channel2
' leg 08 (B.2)    (output2)    channel3 
' leg 09 (B.3)    (output3)    channel4
' leg 10 (B.4)    (output4)    channel5
' leg 11 (B.5)    (output5)    channel6 
' leg 12 (B.6)    (output6)    channel7
' leg 13 (B.7)    (output7)    channel8
' leg 14 +V       (Vdd)          +5v 
' leg 15 (C.6)
' leg 16 (C.7)
' leg 17 (C.0)
' leg 18 (C.1)
'===============================================================

symbol full_stick=b1        'number of frames the joystick threshold has been exceeded. 
symbol ONkount=b2        'number of times the joystick has been 'jabbed' 
symbol OFFkount=b3
symbol centre_stick=b4    'number of frames the joystick has been centre position 
symbol cownt=b6            'number of elapsed frames before each volume adjustment
symbol quartersec=b7
symbol halfsec=b8
symbol wholesec=b9
symbol flashblue=b10

'measure TX frame rate for setting time periods - some new transmitters have 6.7mSec 
'rate rather than the traditional 20mSec

init:
    let dirsB = %11111111
    let pinsB = %10000000
    pause 500
    let pinsB = %00000000
    pause 10
    pulsin 0,1,b0
    if b0>75 and b0<225 then
    count 0,1000, wholesec
    halfsec=wholesec/2
    quartersec=wholesec/4
    else 
    goto init
    end if 

'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          MAIN PROGRAM STARTS HERE 
'-------------------------------------------------------------------------------------------------------------------------------------------------------

main:  
    full_stick=0                'reset frame count timer
    pulsin 0,1,b0                'read joystick position
    
'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS JABS TO ON SIDE OF NEUTRAL
'-------------------------------------------------------------------------------------------------------------------------------------------------------

    if b0>170 then                'joystick beyond threshold?     
    ONkount=ONkount+1 max 9            'increment jab counter
'    if ONkount = 9 then skip6
'    if ONkount = 4 then flashon
    centre_stick=0                'reset neutral position timer 

skip:                            're-read joystick position, looping until released or held
    pulsin 0,1,b0                'read joystick position         
    full_stick=full_stick+1 max 255        'increment frame count timer
    if b0>165 then                'is joystick still above threshold?
                                'note 170-165=50uSec hysteresis
    if full_stick>halfsec then        'yes, above threshold, but is it 'held'? 
                                'yes it's held
    ONkount=ONkount-1            'counts 1 to 8 but ports 0 to 7
    if ONkount = 8 then skip6
    if ONkount = 3 then flashon
    high ONkount                'turn on appropriate port
    
skip2:                        'now wait for joystick to be released
    pulsin 0,1,b0                'read joystick position 
    if b0>165 then skip2            'joystick still held? note 170-165=50uSec hyst.
    ONkount=0                'reset jab counter 
    OFFkount=0 
    end if
    goto skip                'no,it's not "held" (1/2 sec), but neither is it released
    end if                    'so loop round again until one or the other
    end if
    full_stick=0

'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS JABS TO OFF SIDE OF NEUTRAL
'-------------------------------------------------------------------------------------------------------------------------------------------------------

    if b0<130 then                'joystick beyond threshold?     
    OFFkount=OFFkount+1 max 9        'increment jab counter
    if OFFkount = 9 then skip5
    if OFFkount = 4 then flashoff
    centre_stick=0                'reset neutral position timer 

skip3:                            're-read joystick position, looping unti released or held
    pulsin 0,1,b0                'read joystick position         
    full_stick=full_stick+1 max 255        'increment frame count timer
    if b0<135 then                'is joystick still above threshold?
                                'note 170-165=50uSec hysteresis
    if full_stick>halfsec then        'yes, above threshold, but is it 'held'? 
    OFFkount=OFFkount-1
    low OFFkount

skip4:                        'now wait for joystick to be released
    pulsin 0,1,b0                'read joystick position 
    if b0<135 then skip4            'joystick still held? note 170-165=50uSec hyst.
    OFFkount=0 
    ONkount=0                'reset jab counter 
    end if
    goto skip3                'no,it's not held, but neither is it released
                        'so loop round again until one or the other

skip5:
    let pinsB=%00000000        'Turn all channels off
    OFFkount=0
    ONkount=0                'reset jab counter 
    goto skip3

skip6:
    let pinsB=%11111000 
    OFFkount=0
    ONkount=0                'reset jab counter 

flashon:
    flashblue=1
    gosub flash

flashoff:
    flashblue=0
    gosub flash

flash:
    if flashblue=1 then
    high b.3
    else
    low b.3
    return
    
    end if
    end if
    end if
    
'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS TIME SPENT WITH STICK IN NEUTRAL POSITION
'-------------------------------------------------------------------------------------------------------------------------------------------------------

    if b0>140 and b0<160 then        'is stick in neutral position?
    centre_stick=centre_stick+1 max 255    'yes,increment neutral counter preventing
                        'overflow
    end if
    if centre_stick>wholesec then        'stick has been in neutral for one second
    ONkount=0                'reset jab counters
    OFFkount=0
    end if
'-------------------------------------------------------------------------------------------------------------------------------------------------------
    goto main
'-------------------------------------------------------------------------------------------------------------------------------------------------------
My Circuit...
CHI030A PICAXE-18 STANDARD PROJECT BOARD
 
Last edited by a moderator:

SAborn

Senior Member
Try putting your FLASH routine below the last goto main, as i think your program is dropping through to flash on each pass through, also the endif should be before Return not after Return.

Also switching a relay on/off every 500ms in not good and will likely shorten the relay life, i suggest buying a flashing LED and simply switch the relay on and let the led flash.

For your sub routine you could use the toggle command which would save using if-then and else.


flashon:
gosub flash

flashoff:
gosub flash


Code here...........

Goto Main
'......................................................

flash:
Toggle B.3
return
 
Last edited:

Ambutech

New Member
I have cleaned the code up a bit with all my additions now at the end of the file below the "goto main"

Unfortunately, no matter what I do with the FLASH routine, I cannot seem to get the code to activate the FLASH and continue with the rest of the code, leaving the flash working until I explicitly say to turn the flash routine off.

How can I get the flash to work allowing me to use the rest of the code as normal. ie leave the falsh routine running in parallel.

Code as it is, shown below.

Rgds Ken

Code:
' PICAXE 18M2 CONNECTIONS
'
' leg 01 (C.2)	(input2)  	r/x input
' leg 02 (C.3)	(serial out)
' leg 03 (C.4)	(serial in)	connect to 0v
' leg 04 (C.5)	(reset)	    tied to Vdd via 4k7  
' leg 05 0V   	(Vss)       	0v
' leg 06 (B.0)	(output0)	channel1
' leg 07 (B.1)	(output1)	channel2
' leg 08 (B.2)	(output2)	channel3 
' leg 09 (B.3)	(output3)	channel4
' leg 10 (B.4)	(output4)	channel5
' leg 11 (B.5)	(output5)	channel6 
' leg 12 (B.6)	(output6)	channel7
' leg 13 (B.7)	(output7)	channel8
' leg 13 (B.7)	(output7)	channel8
' leg 14 +V  	 (Vdd)      	+5v 
' leg 15 (C.6)
' leg 16 (C.7)
' leg 17 (C.0)
' leg 18 (C.1)
'===============================================================

symbol full_stick=b1		'number of frames the joystick threshold has been exceeded. 
symbol ONkount=b2		'number of times the joystick has been 'jabbed' 
symbol OFFkount=b3
symbol centre_stick=b4	'number of frames the joystick has been centre position 
symbol cownt=b6			'number of elapsed frames before each volume adjustment
symbol quartersec=b7
symbol halfsec=b8
symbol wholesec=b9
symbol flashblue=b10

'measure TX frame rate for setting time periods - some new transmitters have 6.7mSec 
'rate rather than the traditional 20mSec

init:
	let dirsB = %11111111
	let pinsB = %10000000
	pause 500
	let pinsB = %00000000
	pause 10
	pulsin 0,1,b0
	if b0>75 and b0<225 then
	count 0,1000, wholesec
	halfsec=wholesec/2
	quartersec=wholesec/4
	else 
	goto init
	end if 

'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          MAIN PROGRAM STARTS HERE 
'-------------------------------------------------------------------------------------------------------------------------------------------------------

main:  
	full_stick=0				'reset frame count timer
	pulsin 0,1,b0				'read joystick position
	
'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS JABS TO ON SIDE OF NEUTRAL
'-------------------------------------------------------------------------------------------------------------------------------------------------------

	if b0>170 then				'joystick beyond threshold?     
	ONkount=ONkount+1 max 9			'increment jab counter
	centre_stick=0				'reset neutral position timer 

skip:							're-read joystick position, looping until released or held
	pulsin 0,1,b0				'read joystick position         
	full_stick=full_stick+1 max 255		'increment frame count timer
	if b0>165 then				'is joystick still above threshold?
								'note 170-165=50uSec hysteresis
	if full_stick>halfsec then		'yes, above threshold, but is it 'held'? 
								'yes it's held
	ONkount=ONkount-1			'counts 1 to 8 but ports 0 to 7
	if ONkount = 8 then allon
	if ONkount = 3 then flashon
	high ONkount				'turn on appropriate port
	
skip2:						'now wait for joystick to be released
	pulsin 0,1,b0				'read joystick position 
	if b0>165 then skip2			'joystick still held? note 170-165=50uSec hyst.
	ONkount=0				'reset jab counter 
	OFFkount=0 
	end if
	goto skip				'no,it's not "held" (1/2 sec), but neither is it released
	end if					'so loop round again until one or the other
	end if
	full_stick=0

'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS JABS TO OFF SIDE OF NEUTRAL
'-------------------------------------------------------------------------------------------------------------------------------------------------------

	if b0<130 then				'joystick beyond threshold?     
	OFFkount=OFFkount+1 max 9		'increment jab counter
	centre_stick=0				'reset neutral position timer 

skip3:							're-read joystick position, looping unti released or held
	pulsin 0,1,b0				'read joystick position         
	full_stick=full_stick+1 max 255		'increment frame count timer
	if b0<135 then				'is joystick still above threshold?
								'note 170-165=50uSec hysteresis
	if full_stick>halfsec then		'yes, above threshold, but is it 'held'? 
	OFFkount=OFFkount-1
	if OFFkount = 8 then alloff
	if OFFkount = 3 then flashoff
	low OFFkount

skip4:						'now wait for joystick to be released
	pulsin 0,1,b0				'read joystick position 
	if b0<135 then skip4			'joystick still held? note 170-165=50uSec hyst.
	OFFkount=0 
	ONkount=0				'reset jab counter 
	end if
	goto skip3				'no,it's not held, but neither is it released
						'so loop round again until one or the other


	end if
	end if
	
'-------------------------------------------------------------------------------------------------------------------------------------------------------
'          PROCESS TIME SPENT WITH STICK IN NEUTRAL POSITION
'-------------------------------------------------------------------------------------------------------------------------------------------------------

	if b0>140 and b0<160 then		'is stick in neutral position?
	centre_stick=centre_stick+1 max 255	'yes,increment neutral counter preventing
						'overflow
	end if
	if centre_stick>wholesec then		'stick has been in neutral for one second
	ONkount=0				'reset jab counters
	OFFkount=0
	end if
'-------------------------------------------------------------------------------------------------------------------------------------------------------
	goto main
'-------------------------------------------------------------------------------------------------------------------------------------------------------

alloff:
	let pinsB=%00000000		'Turn all channels off
	OFFkount=0
	ONkount=0				'reset jab counter 
	goto skip3

allon:
	let pinsB=%11110000 
	OFFkount=0
	ONkount=0				'reset jab counter 
	goto skip3
	
flashon:
	flashblue=1
	gosub flash

flashoff:
	flashblue=0
	gosub flash

flash:
	do
	high b.3
	pause 300
	low b.3
	pause 300
	if flashblue = 0 then exit
	loop while flashblue = 1
	return
 

westaust55

Moderator
Since you are using an M2 part and seemingly at default clock speed,
One option is to use multi tasking.
In the main/first task, set a variable as a flag to indicate whether flashing should be on or off.
Then in a second task check if the flag is set for "on" and if so toggle the output on and off at the desired intervals, otherwise set the relay output off/low.

See PICAXE manual 1 from page 62 for information on parallel/multi tasking.
http://www.picaxe.com/docs/picaxe_manual1.pdf
 
Top