Help with program code

GAP

Senior Member
I have been given some code for a project I am doing but I can't seem to fathom a couple of lines/statements could someone please help?
The code I am having trouble understanding is;
do
readadc c.0, right
ifb4=5 yhen
gosub change lights
b4=0
endif
++b4
loop while right>128

When I run the code through syntax checker I get an error at line ++b4

What am I looking at here?

I was told that when the program goes from one loop to another it carries the current value of b4.

The other statement is XOR b5 (I have tried shift =6 as recommended in the manual) but the sybtax chedcker doesn't like it.

Any help greatly appreciated
Regards
 

GAP

Senior Member
That line should be inc b4.
Code:
symbol left=b0
symbol right=b1
 
start:
         
Low B.1				;Set B.1 as an output and low
Low B.2				;Set B.2 as an output and low
b5 = 0                    	;this is to keep track of the state of the lights
 
readadc c.4,left       		;read adc into variable b0
readadc c.0,right     		;read adc into variable b1 

readadc c.4,left        	;looks at both inputs
readadc c.0,right
 
if left>128 then         	;if left is greater than 128 then flash lights

GoSub ChangeLights
GoSub BoomDown  
GoSub ChangeLights
do
readadc c.0,right
    If b4 =5 Then
        GoSub ChangeLights
        b4 = 0
    end if
    ++b4
loop while right<128
 
do
readadc c.0,right

    If b4 =5 Then
        GoSub ChangeLights
        b4 = 0
    end if
    ++b4
loop while right>128
end if

if right>128 then
do
readadc c.4,left

    If b4 =5 Then
        GoSub ChangeLights
        b4 = 0
    end if
    ++b4
loop while left<128
 
do
readadc c.4,left

    If b4 =5 Then
        GoSub ChangeLights
        b4 = 0
    end if
    ++b4
loop while left>128 
 
GoSub ChangeLights
GoSub BoomUp  

else if
GoTo start
 
end if 
 
GoTo Start 
 

ChangeLights:				;this sets which light is on/off based on the value of the b5 variable and then XORs (flips it between 1 and 0) it so that next time the sub is called it switches them back.

 

if b5 = 0 then

    low B.1

    high B.2

else

    high B.1

    low B.2

end if

XOR b5

Return

 

BoomUp:
 
for count = 75 to 225
    servo (servopin),count
    pause 1
next count
 
Return
 
BoomDown:
 
for count = 225 to 75 -1
    servo (servopin),count
    pause 1
next count
 
Return
This is the whole program, that may help you to help me, and the explanation I was given says" When the program goes from one loop to the next it carries the current value of b4 so that the light flash duration stays constant regardless of what it was set to when the input changed"

This program , when triggeredby eg left, is supposed to read 2 adc inputs to a 14M2 then drive 2 servos from one end to the middle and at the same time alternately flash 2 LED's.
It then waits till "right goes high then low and then turns off the lights and moves the servo back to its original position.

Hope that explains what I am trying to do.
Regards
 

GAP

Senior Member
What was getting at is that this code
Code:
 ++b4
should read
Code:
inc B4
I'm a real newbie at this so please excuse any dumb questions.

Doesn't inc B4 incerease the value of B4?

I originally thought that inc was the correct command but reading the manuals I was under the impression that it increased the value

The explanation says that it "carries the current value of b4" and this is where I am getting confused.

The other stumbling block is the "XOR" command I have no idea what is going on here.

regards
 

Buzby

Senior Member
Doesn't inc B4 incerease the value of B4?

I originally thought that inc was the correct command but reading the manuals I was under the impression that it increased the value

The explanation says that it "carries the current value of b4" and this is where I am getting confused.
The value of B4 is incrementing every time round the first loop.
When the first loop finishes it drops to the second loop, with B4 still holding the last value it had from the first loop.

The second loop then runs, also incrementing B4.
When this loop finishes it goes to the next loop, with B4 still holding whatever the second loop finished at.

XOR is a logical operator, see the section in Manual 2 for details.
It's usage here is supposed to 'toggle' the value of B5, but the line is wrong.
Another way is to use 'B5 = NOT B5', which toggles the byte just the same as B5 = B5 XOR 255, but I think is easier to understand.
 

SAborn

Senior Member
This comes back to how the manuals are set out, where in the manual is the "NOT" command listed?
It would be nice to see ALL commands listed in the side menu, regardless of where they are filed in the manuals.
I should be able to look up the "NOT" command in the menu and be directed to the section that covers it use.

Its the biggest gripe i have with the manuals, you need to already know the command to know where to look in the manual to find the information.
All commands should be listed regardless of what section covers it.
 

GAP

Senior Member
thanks for all the help I'll think this over and will surely come back with questions

especially about operating the servos

Regards
 

GAP

Senior Member
This comes back to how the manuals are set out, where in the manual is the "NOT" command listed?
It would be nice to see ALL commands listed in the side menu, regardless of where they are filed in the manuals.
I should be able to look up the "NOT" command in the menu and be directed to the section that covers it use.

Its the biggest gripe i have with the manuals, you need to already know the command to know where to look in the manual to find the information.
All commands should be listed regardless of what section covers it.
That is exactly what the sort of thing I am having trouble with.

The other issues is that when you get a syntax error in program editor there is no indication of where the error lies you just have to try and find it, not exactly educational for a newbie like myself trying to self teach.

If a list of correct syntax was available then maybe people could find out where they are going wrong instead of having to resort to asking the same old question over and over to the annoyance of more experienced users.
 

Buzby

Senior Member
... where in the manual is the "NOT" command listed?
It would be nice to see ALL commands listed in the side menu, regardless of where they are filed in the manuals.
This is my biggest gripe also.

I think the reason for the manual's omission of XOR, NOT, etc., from the 'command' list is because, strictly speaking, they are not 'commands', they are 'operators'.

Unfortunately I don't think the distinction needs to be made.

Anything I type in the PICAXE I see as a command to tell the PICAXE to do something.
The nitty-gritty of the difference between a 'command' and an 'operator' is irrelevant.
 

nick12ab

Senior Member
This comes back to how the manuals are set out, where in the manual is the "NOT" command listed?
It would be nice to see ALL commands listed in the side menu, regardless of where they are filed in the manuals.
I should be able to look up the "NOT" command in the menu and be directed to the section that covers it use.
Totally agree. It's even worst for more advanced keyword commands like NCD and BINTOBCD where you want to tell a member about this command and include a link and you have to hunt for the correct 'Variables' page to find it which wastes a lot of time. They are all listed on the picaxe.com website under let, which is OK but would be better if it had HTML anchors (so you could do http://www.picaxe.com/BASIC-Commands/Variables/let/#ncd and it will jump to that command).
 

hippy

Ex-Staff (retired)
It's an awkward one. Where some will say operators should be treated as a command others will argue differently. The historical tradition for program language manuals is to not treat operators as commands. It's always hard to balance what should be indexed and what should be part of a section or be a section in its own right.

If operators are mixed with commands in the index it can be confusing if the page for that is not between the pages for the commands around it. Not so much a problem with a website but more so with PDF and if that PDF is printed out. If each operator has its own page that's an additional 30 or so pages that likely each contain very little information.

Plus people will still want to see a list of all operators collated together.

It is the age old problem that whatever is done it will never satisfy everyone.
 

westaust55

Moderator
This comes back to how the manuals are set out, where in the manual is the "NOT" command listed?
It would be nice to see ALL commands listed in the side menu, regardless of where they are filed in the manuals.
I should be able to look up the "NOT" command in the menu and be directed to the section that covers it use.
NOT along with the "unary minus" are mentioned/covered at the top of the page under the sub heading
Variables - Unary Mathematics
(page 25 in PICAXE manual 2 @ V7.9)

NOT is also used as a parameter for the SETINT command.



and an example provided in my "signature" line ;)
 
Last edited:

westaust55

Moderator
@GAP,

The ++b4 seemingly come form the C programming language rather than PICAXE BASIC. Within C this is the increment function/operator.
As others have indicated this needs to be changed to inc b4

Another part of the program where there is a bad construct is:
Code:
       if
:
:
:

	else if
		GoTo start
 
	end if 
 
	GoTo Start
The line with "else if" alone will not work as the else parameter is expecting a test/comparison as the basis for the next line(s).
Sine the "else if" only leads to "Goto Start" on the subsequent line and following the "end if" line there is also "Goto Start" ,
the part
Code:
	else if
		GoTo start
can be deleted.


Below is a "new" version of your code with:
1. Some further syntax corrections (for example "count" is a PICAXE command and cannot be used as a variable name - changed here to "countr")
2. nested lines indented which make the groups of lines in each nest level easier to identify.

Indenting the lines AND adding more comments to indicate the purpose of the program lines together with a few header lines describing the program purpose are the key to good documentation.

Code:
symbol left=b0
symbol right=b1
SYMBOL countr = b2

SYMBOL servopin = b.4

start:
         
	Low B.1			;Set B.1 as an output and low
	Low B.2			;Set B.2 as an output and low
	b5 = 0                 	;this is to keep track of the state of the lights
 
	readadc c.4,left    	;read adc into variable b0
	readadc c.0,right   	;read adc into variable b1 

	readadc c.4,left        	;looks at both inputs
	readadc c.0,right
	if left>128 then         	;if left is greater than 128 then flash lights

		GoSub ChangeLights
		GoSub BoomDown  
		GoSub ChangeLights
		
		do
			readadc c.0,right
    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while right<128
 
		do
			readadc c.0,right
			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while right>128
	end if

	if right>128 then
		do
			readadc c.4,left

    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while left<128
 
		do
			readadc c.4,left

    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while left>128 
 
		GoSub ChangeLights
		GoSub BoomUp  
 
	end if 
 
	GoTo Start 
 

ChangeLights:				;this sets which light is on/off based on the value of the b5 variable and then XORs (flips it between 1 and 0) it so that next time the sub is called it switches them back.

 

	if b5 = 0 then

    		low B.1

    		high B.2

	else

    		high B.1

    		low B.2

	endif

	b5 = b5 XOR 1 

	Return

 

BoomUp:
 	for countr = 75 to 225
    		servo servopin,countr
    		pause 1
	next countr
 	Return
 
BoomDown:
 	for countr = 225 to 75 step -1
    		servo servopin,countr
    		pause 1
	next countr
 	Return
 

SAborn

Senior Member
Thanks Westy,

Well i must be stupid not to think of looking in the "Variables - Unary Mathematics" section to find the answer, lets face it that aint the obvious place to look.

Hippy, i hear your point of view and understand the logic, but a index is exactly as the name implys... its a index to look up a reference point to a subject.
Think if the local yellow page phone book left half the entrys out because it was thought they fitted a prior listing group, what a nightmare that would create.
Perhaps the approach is .... if it aint broke why fix it.... that dont help make a better manual or help anyone understand programming a picaxe better.

I did not suggest rewriting the manual, only a better index, which would seem a simple task that would aid many.
 

John West

Senior Member
As the entirety of the manuals consist of data files instead of ink on paper, I would think it would be reasonable to compile them in two versions, one for noobs, and one following the typical structure of a traditional programmers reference guide. I too had difficulty locating what I at first thought of as a command, but which turned out to be a variable. Better cross-referencing makes for a better information product.

Also, I used the downloaded manuals on my PC for a month (at least) before I read (in here) that placing the mouse over the indexed command and clicking on it actually took me directly to the command. Many online documents don't do that, and having been raised in the days of printed media I simply hadn't noticed that the Rev. Ed. manuals had hyper-linked indexes, because they were not color highlighted as most links are, and I never placed the mouse over them. I instead scrolled tediously through dozens of pages of the manuals using the down arrow on the keyboard in order to get to each command.

I know a good bit of electronics, but that doesn't seem to keep me from feeling stupid, and on a regular basis.
 

GAP

Senior Member
@GAP,

The ++b4 seemingly come form the C programming language rather than PICAXE BASIC. Within C this is the increment function/operator.
As others have indicated this needs to be changed to inc b4

Another part of the program where there is a bad construct is:
Code:
       if
:
:
:

	else if
		GoTo start
 
	end if 
 
	GoTo Start
The line with "else if" alone will not work as the else parameter is expecting a test/comparison as the basis for the next line(s).
Sine the "else if" only leads to "Goto Start" on the subsequent line and following the "end if" line there is also "Goto Start" ,
the part
Code:
	else if
		GoTo start
can be deleted.


Below is a "new" version of your code with:
1. Some further syntax corrections (for example "count" is a PICAXE command and cannot be used as a variable name - changed here to "countr")
2. nested lines indented which make the groups of lines in each nest level easier to identify.

Indenting the lines AND adding more comments to indicate the purpose of the program lines together with a few header lines describing the program purpose are the key to good documentation.

Code:
symbol left=b0
symbol right=b1
SYMBOL countr = b2

SYMBOL servopin = b.4

start:
         
	Low B.1			;Set B.1 as an output and low
	Low B.2			;Set B.2 as an output and low
	b5 = 0                 	;this is to keep track of the state of the lights
 
	readadc c.4,left    	;read adc into variable b0
	readadc c.0,right   	;read adc into variable b1 

	readadc c.4,left        	;looks at both inputs
	readadc c.0,right
	if left>128 then         	;if left is greater than 128 then flash lights

		GoSub ChangeLights
		GoSub BoomDown  
		GoSub ChangeLights
		
		do
			readadc c.0,right
    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while right<128
 
		do
			readadc c.0,right
			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while right>128
	end if

	if right>128 then
		do
			readadc c.4,left

    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while left<128
 
		do
			readadc c.4,left

    			If b4 =5 Then
        			GoSub ChangeLights
        			b4 = 0
    			endif
    			inc b4
		loop while left>128 
 
		GoSub ChangeLights
		GoSub BoomUp  
 
	end if 
 
	GoTo Start 
 

ChangeLights:				;this sets which light is on/off based on the value of the b5 variable and then XORs (flips it between 1 and 0) it so that next time the sub is called it switches them back.

 

	if b5 = 0 then

    		low B.1

    		high B.2

	else

    		high B.1

    		low B.2

	endif

	b5 = b5 XOR 1 

	Return

 

BoomUp:
 	for countr = 75 to 225
    		servo servopin,countr
    		pause 1
	next countr
 	Return
 
BoomDown:
 	for countr = 225 to 75 step -1
    		servo servopin,countr
    		pause 1
	next countr
 	Return
Thankyou all for the replies and help.
The guy who gave me the program is an Arduino user as well as picaxe, he interfaces them, so I guess he got a bit confused.
 

GAP

Senior Member
Thankyou all for the replies and help.
The guy who gave me the program is an Arduino user as well as picaxe, he interfaces them, so I guess he got a bit confused.


Code:
;This program reads the adc input from C.4(left) or C.0 (right) to trigger flashing lights and to lower or raise a boom on a model train layout.
;The program will trigger on whichever adc input changes from high to low first, when this happens it starts the flashing lights and lowers the boom. 
;It then waits till the opposite adc input to the triggering input changes from high to low, signifying that the train has passed and then turns off the lights and raises the boom.


symbol left=b0
symbol right=b1
SYMBOL countr = b2 
SYMBOL servopin = b.4
SYMBOL servopina = b.5

start:
         
	Low B.1				;Set B.1 as an output and low
	Low B.2				;Set B.2 as an output and low
	b5 = 0                    	;this is to keep track of the state of the lights
 
	readadc c.4,left       		;read adc into variable b0
	readadc c.0,right     		;read adc into variable b1 

	readadc c.4,left        	;looks at both inputs
	readadc c.0,right
 
	if left>150 then         	;if left is greater than 128 then flash lights and lower booms

		GoSub ChangeLights
		
		GoSub ChangeLights

		do
			readadc c.0,right
    			If b4 =5 Then
       			 GoSub ChangeLights
       			 b4 = 0
    		end if
  		inc b4
	loop while right<150
 
		do
			readadc c.0,right
			if b4 =5 Then
        			 GoSub ChangeLights
        			 b4 = 0
    		end if
    		inc b4
	loop while right>150
	end if

	if right>150 then
	do
			readadc c.4,left
			if b4 =5 Then
				GoSub ChangeLights
        			b4 = 0
        	end if
        	inc b4
        loop while left<150
 
		do
			readadc c.4,left
			if b4 =5 Then
        			 GoSub ChangeLights
        			 b4 = 0
    		end if
    		inc b4
	  loop while left>150 
 
	GoSub ChangeLights
	GoSub BoomUp  

	else 
	GoTo start
 
end if 
 
GoTo Start 
 

ChangeLights:				;this sets which light is on/off based on the value of the b5 variable and then XORs (flips it between 1 and 0) it so that next time the sub is called it switches them back.

 
			if b5 = 0 then
	    		low B.1
	    		high B.2
	    		pause 500
	    		low B.2
	    		High B.1

			else

    			high B.1
    			low B.2 
	    		pause 500
	    		high B.2
	    		low B.1 

			endif

	b5 = b5 XOR 1 

	Return

 

BoomUp: 
    servo B.4,150			;set servos at 150
    servo B.5,150
    
    for countr = 150 to 225             
    	servo B.4,countr
        servo B.5,countr

    servo B.5, countr
    if b4=5 Then
        GoSub ChangeLights
        b4=0
    end if
    pause 1
    next countr				;increment counter by one count
Return

 
BoomDown:
    servo b.4,225   ;set servos at 225
    servo b.5,225
    
    for countr = 225 to 150 step -1
        servo b.4,countr
        servo b.5,countr

    servo B.5,countr
    if b4=5 Then
        GoSub ChangeLights
        b4=0
    end if
    pause 1
    next countr				;decrement by one count
Return
I have loaded this into my chip after messing with the sevo function and have now got the lights working as I planned but the servo just refuses to respond at all no matter what I try to do.
Where am I going wrong here?
All I want to do is lower the boom when the lights start flashing and for it to remain down till the lights stop flashing.
I thought that the gosubs would work but obviously I have not got it right.
Any help greatly appreciated.
I am seriously considering using a 08M2 as a slave to drive the servos instead of running it all from a 14M2
 

JimPerry

Senior Member
Define a 14M2 at start then use servopos after the first definitions of servo - ran it in simulator and never seems to hit the boomup boomdown subroutines :confused:
 

westaust55

Moderator
Near the start of the Main loop, there are the lines:
readadc c.4,left ;read adc into variable b0
readadc c.0,right ;read adc into variable b1​

This is followed by similar sections of code such as:
Code:
	if left>150 then         	;if left is greater than 128 then flash lights and lower booms

		GoSub ChangeLights
		
		GoSub ChangeLights

		do
			readadc c.0,right
    			If b4 =5 Then
       			 GoSub ChangeLights
       			 b4 = 0
    		endif
  		inc b4
	loop while right<150
If you "trigger"/activate on the left sensor all is well and you enter the DO...LOOP structure.
To exit the DO...LOOP structure, you wait for a change in the right ADC input form the second sensor with the line:
loop while right<150​

But for this to work, unless the right sensor is also activated/"triggered" before you enter the DO...LOOP structure, which I would think is unlikely, you do not read a new input from the second sensor so the code seemingly will get "stuck looping forever waiting for the second sensor to "trigger" and exit the LOOP which it never will as the ADC input is not read within the loop.
 

GAP

Senior Member
I have built the circuit on a breadboard and tested it.

The sensors are 2 LDR's acting as avoltage divider between the 5V rail and earth with input taken from the centre which give a voltage approx equal to half the +ve rail (approx 2.5V).

When the "left" sensor is covered, input rises toward 5V rail (high),the lights start flashing.

The lights remain flashing after the sensor is uncovered, input drops to approx 2.5V (low)

The lights keep flashing till the "right" sensors goes (high) and then drops (low).

When this happens the lights stop flashing.

The same thing happens if "right" is covered first then "left" changes from (high) to (low).

What is supposed to happen is that when for example "left" goes (high) the lights flash and a servo lowers a boom ie travels from 150 (raised) to 225 (lowered)

Then when right goes (high) then (low) the lights go off and the boom raises ie returns back to 150.

What i just can't get my head around is how I make the servo react when the lights start flashing.

This is the program
Code:
symbol left=b0
symbol right=b1
SYMBOL countr = b2 
SYMBOL servopin = b.4
SYMBOL servopina = b.5

start:
         
	Low B.1				;Set B.1 as an output and low
	Low B.2				;Set B.2 as an output and low
	b5 = 0                    	;this is to keep track of the state of the lights
 
	readadc c.4,left       		;read adc into variable b0
	readadc c.0,right     		;read adc into variable b1 

	readadc c.4,left        	;looks at both inputs
	readadc c.0,right
 
	if left>150 then         	;if left is greater than 128 then flash lights and lower booms

		GoSub ChangeLights
		GOSub BoomDown
		GoSub ChangeLights

		do
			readadc c.0,right
    			If b4 =5 Then
       			 GoSub ChangeLights
       			 b4 = 0
    		end if
  		inc b4
	loop while right<150
 
		do
			readadc c.0,right
			if b4 =5 Then
        			 GoSub ChangeLights
        			 b4 = 0
    		end if
    		inc b4
	loop while right>150
	end if

	if right>150 then
	do
			readadc c.4,left
			if b4 =5 Then
				GoSub ChangeLights
        			b4 = 0
        	end if
        	inc b4
        loop while left<150
 
		do
			readadc c.4,left
			if b4 =5 Then
        			 GoSub ChangeLights
        			 b4 = 0
    		end if
    		inc b4
	  loop while left>150 
 
	GoSub ChangeLights
	GoSub BoomUp  

	else 
	GoTo start
 
end if 
 
GoTo Start 
 

ChangeLights:				;this sets which light is on/off based on the value of the b5 variable and then XORs (flips it between 1 and 0) it so that next time the sub is called it switches them back.

 
			if b5 = 0 then
	    		low B.1
	       	high B.2
	    		pause 500
	    		low B.2
	    		High B.1

			else

    			high B.1
    			low B.2 
	    		pause 500
	    		high B.2
	    		low B.1 

			endif

	b5 = b5 XOR 1 

	Return

 

BoomUp: 
    servo B.4,150			;set servos at 150
    servo B.5,150
    
    for countr = 150 to 225             
    	servo B.4,countr
        servo B.5,countr

    servo B.5, countr
    if b4=5 Then
        GoSub ChangeLights
        b4=0
    end if
    pause 1
    next countr				;increment counter by one count
Return

 
BoomDown:
    servo b.4,225   ;set servos at 225
    servo b.5,225
    
    for countr = 225 to 150 step -1
        servo b.4,countr
        servo b.5,countr

    servo B.5,countr
    if b4=5 Then
        GoSub ChangeLights
        b4=0
    end if
    pause 1
    next countr				;decrement by one count
Return
 

cravenhaven

Senior Member
Why dont you try dropping the boom just a few steps at a time while you are in the flashing light mode, instead of dropping it completely before you go into the flashing lights routines, ie something like
..
..
..
flashinglights routine
toggle lights
IF boom_not_down THEN drop boom by a few steps
Test_for_end_of_flashing_lights ELSE LOOP
..
..
..
 

hippy

Ex-Staff (retired)
As this is seemingly 14M2 based it should be possible to use the M2 multi-tasking capabilities to perform the flashing light functions and boom raising and lowering in parallel. That would probably be the easiest and best approach to take, exactly the type of thing multi-tasking is designed for.
 

GAP

Senior Member
As this is seemingly 14M2 based it should be possible to use the M2 multi-tasking capabilities to perform the flashing light functions and boom raising and lowering in parallel. That would probably be the easiest and best approach to take, exactly the type of thing multi-tasking is designed for.
Could someone please explain multitasking capabilities using the simplest of terms.

I have made some progress with the program in that the lights work as advertised and the boom raises and lowers slowly albeit not when I want it to.
 

hippy

Ex-Staff (retired)
Multi-tasking is running two or more separate programs alongside each other. You could have one program flashing LED's and another moving a servo. The M2 PICAXE handle this automatically so all you need is to write the separate programs, then place them in a single program. When that runs both programs will do what they each would be expected to do.

Each separate program in a single chip is called a "task" to distinguish those from separate programs running in separate chips.

The next step is in controlling each of the two tasks to only run when they need to, then sequencing that control so the outcome desired is achieved.
 

GAP

Senior Member
Multi-tasking is running two or more separate programs alongside each other. You could have one program flashing LED's and another moving a servo. The M2 PICAXE handle this automatically so all you need is to write the separate programs, then place them in a single program. When that runs both programs will do what they each would be expected to do.

Each separate program in a single chip is called a "task" to distinguish those from separate programs running in separate chips.

The next step is in controlling each of the two tasks to only run when they need to, then sequencing that control so the outcome desired is achieved.
Thanks for explanation.
I have now split this program into 2 parts the lights part works but not the servo part.
I am having trouble getting the servo to move up and down when the inputs change like the lights part does and just am having so much trouble trying to figure out what is going on.
Do I just use the trigger bit and remove all reference to change lights and run the servo gosub or am I so far off track here?
I have run some modified servo bits in the simulator but the indications I get just tell me that there is a signal being sent to the servo but not what it is doing as opposed to the lights where I could see them changing.
The other issue I am having is how do I tell the servo to stay in a position till the input conditions are met to change its position.
I just can't seem to find anything that simply explains the basic language, the manuals I have found to be a little unhelpful.
 

hippy

Ex-Staff (retired)
I just can't seem to find anything that simply explains the basic language, the manuals I have found to be a little unhelpful.
The manuals won't really help you because you are designing an algorithm to do what you want and there are 'infinite possibilities' there. What the manuals help with is how the individual actions of an algorithm are required to be represented.

What you first need to do is define the sequence of operations required and what triggers them. Post #22 seems to describe that, which I'd interpret as -

Wait for a left or right trigger
If it was a left trigger then
- Start flashing the lights
- Raise boom
- Stop flashing the lights
- Wait for a right trigger
- Start flashing the lights
- Lower boom
- Stop flashing the lights
else
- Start flashing the lights
- Raise boom, and flash the lights
- Stop flashing the lights
- Wait for a left trigger
- Start flashing the lights
- Lower boom
- Stop flashing the lights
End if
Repeat from start

Refine that until it is absolutely correct ( you may want to set the lights to some particular colour after boom raised or lowered etc ) then you can turn that into your high level control program without worrying about how the parts of it are achieved.

We can all help with that but the algorithm specification needs to be a good as it can be or it won't do what is required and may require a lot of change to make it how it should be.
 

GAP

Senior Member
The manuals won't really help you because you are designing an algorithm to do what you want and there are 'infinite possibilities' there. What the manuals help with is how the individual actions of an algorithm are required to be represented.

What you first need to do is define the sequence of operations required and what triggers them. Post #22 seems to describe that, which I'd interpret as -

Wait for a left or right trigger
If it was a left trigger then
- Start flashing the lights
- Raise boom
- Stop flashing the lights
- Wait for a right trigger
- Start flashing the lights
- Lower boom
- Stop flashing the lights
else
- Start flashing the lights
- Raise boom, and flash the lights
- Stop flashing the lights
- Wait for a left trigger
- Start flashing the lights
- Lower boom
- Stop flashing the lights
End if
Repeat from start

Refine that until it is absolutely correct ( you may want to set the lights to some particular colour after boom raised or lowered etc ) then you can turn that into your high level control program without worrying about how the parts of it are achieved.

We can all help with that but the algorithm specification needs to be a good as it can be or it won't do what is required and may require a lot of change to make it how it should be.
Have I bitten off more than I can chew here?

What I am trying to achieve is control of a level crossing on a model railway.

The sequence should go like this;

-If left sensor(adc) changes from low to high then start lights flashing and lower boom
-read adc and if left has gone from low to high then keep lights flashing and boom down
-keep reading adc and looping lights and boom till right goes from low to high
-when right goes from high to low then stop lghts flashing and raise boom
-wait till next trigger be it left going low to high or right going low to high to start the sequence again


After deleting any reference to the boom function I can get the lights to operate the way I want them to but the boom is totally another matter.

I am toying with the idea of running the boom function on a slave 08M2 using a digital output from a master 08M2 running the lights where if the lights are flashing then the output is high resulting in the being boom is down and if low it is up the digital output will change while the changelights sub routine is running.
 
Top