Looping

RustyH

Senior Member
Afternoon All,

I recently finish my code for an RGB circuit I was working on. It all works great and Im happy with the results.

I was just wondering though, is there a more efficient way to code the below section of the code. As you can see, at the moment I a fade loop (both on and off) for each colour, but was wondering if there was a better method of coding it that would use only 1 on fade loop and 1 off fade loop, but would then loop through the colours as well?
Only asking to learn really.

Code:
symbol Red = 0
symbol Blue = 1
symbol Green = 2
symbol speed = w0
symbol on_time = w2
symbol off_time = w3

setint %00001000, %00001000


main:
readadc C.4,b1  ‘read the value
if b1<20 then chase  ‘range 0-20
goto main


chase:
speed = b0 * 10000

	RedOn: 
		for on_time = 0 to 5000 step 2
            	pulsout Red,on_time
            	off_time = 5000 - on_time
            	pauseus off_time
        	next on_time
        high Red
        
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
        
	GreenOff:
		for on_time = 0 to 5000 step 2
          		pulsout Green,on_time
          		off_time = 5000 - on_time
          		pauseus off_time
       	next on_time
       low Green        

	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed

	BlueOn: 
		for on_time = 0 to 5000 step 2
            	pulsout Blue,on_time
            	off_time = 5000 - on_time
            	pauseus off_time
        	next on_time
	  high Blue
	  
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
        
	RedOff:
		for on_time = 0 to 5000 step 2
          		pulsout Red ,on_time
          		off_time = 5000 - on_time
          		pauseus off_time
       	next on_time
        low Red

	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed

	GreenOn: 
		for on_time = 0 to 5000 step 2
            	pulsout Green,on_time
            	off_time = 5000 - on_time
            	pauseus off_time
        	next on_time
        high Green

	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
        
	BlueOff:
		for on_time = 0 to 5000 step 2
          		pulsout Blue ,on_time
          		off_time = 5000 - on_time
          		pauseus off_time
       	next on_time
        low Blue

	pause speed
	pause speed
	pause speed
	pause speed
	pause speed
	pause speed

goto main
 

nick12ab

Senior Member
You could use a variable to switch between the colours in the loops instead of having one for each colour.
 

g6ejd

Senior Member
Code:
 symbol Red = 0
 symbol Blue = 1
 symbol Green = 2
 symbol speed = w0
 symbol on_time = w2
 symbol off_time = w3
 symbol pause_events = b6
 symbol colour_control = b7
 setint %00001000, %00001000
 
main:
 readadc C.4,b1 &#8216;read the value
 if b1<20 then chase &#8216;range 0-20
 goto main
 
chase:
 	speed = b0 * 10000
 
RedOn: 
	colour_control = Red
	gosub control_colour
	high Red
	gosub speed_pause

 GreenOff:
 	colour_control = green
 	gosub control_colour
 	low Green 
	gosub speed_pause
 
BlueOn: 
	colour_control = blue
 	gosub control_colour
 	high Blue
 	gosub speed_pause

 RedOff:
 	colour_control = red
 	gosub control_colour
 	low Red
 	gosub speed_pause
 
GreenOn: 
	colour_control = green
 	gosub control_colour
 	high Green
 	gosub speed_pause

 BlueOff:
 	colour_control = blue
 	gosub control_colour
 	low Blue
 	gosub speed_pause
 
goto main
 
speed_pause:
	for pause_events = 1 to 6
 		pause speed
 	next pause_events
 	return

 control_colour:
 	for on_time = 0 to 5000 step 2
 		pulsout colour_control,on_time
 		off_time = 5000 - on_time
 		pauseus off_time
 	next on_time 
	return
Orignal = 208 bytes, now = 136 bytes
 
Last edited:

nick12ab

Senior Member
Er...

Does it use less program space now? Could you use [code][/code] tags to retain white spacing to make it tidier?
 

g6ejd

Senior Member
Yes, sorry, done that as above, there are more storage savings to be had, but makes it more complex.
 

westaust55

Moderator
@RustyH
There may be some confusion/conflict in your determination of the delay time between each step.
Consider the code segment:
Code:
symbol speed = w0
main:
    readadc C.4,b1  &#8216;read the value
    if b1<20 then chase  &#8216;range 0-20
    goto main
chase:
    speed = b0 * 10000
In the READADC command you read a value into byte variable b1

In calculating the &#8220;Speed&#8221; word variable you use byte variable b0 as the &#8220;source&#8221; value. Byte variable b0 does not appear anywhere else in the code you posted.

Word variable Speed = w0 which comprises two bytes as the most significant byte (MSB) and least significant byte (LSB). The PICAXE variables overlap such that b1 is also the MSB of variable w0 and b0 is the LSB of variable w0.

While you can reuse a variable there is a need to be careful in doing this.
Since b0 is never seemingly defined elsewhere in the program the delay parameter &#8220;speed&#8221; as calculated by speed = b0 * 10000 would always be zero (0). Should that b0 in fact be b1?

As previously mentioned, if b0 is greater than 6, the variable &#8220;speed&#8221; will overflow &#8211; you may need a multiplier less than 10,000.
 

Goeytex

Senior Member
This may be the reason for the multiple pause commands.

Since b0 defaults to zero, then "speed" will also be zero. A "pause speed" is then the same as "Pause 0" and
will pause the program only for the time it takes to process the pause command (+-250us) .
So six consecutive "pause speed" commands will pause for about 1.5ms.
 
Top