Variable Code

RustyH

Senior Member
Evening All,

Just after a spot of help with a small part of my code im trying to come up with.

I want to make a RGB programme that slowly fades from one color to another, I have a push button switch that I will be using inorder to change the how quickly the change occurs and wondered how I would set up the variable in the code so that is could read how many times the button had been pressed and therefore what speed it should be running it. I plan to have say 10 speed setting, so once the variable reaches 10, is it possible to make it start again back at 0?

Thanks
 

RustyH

Senior Member
Thanks Dippy

How do I do the variable part of the code though (im still not very good at variables!!)

So when the button is pressed, more variable up by 1
 

lbenson

Senior Member
Try this in the simulator. Keep toggling pin 3.
Code:
#picaxe 08M2

main:
  do
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      if b0 > 10 then 
        b0 = 0
      endif
    endif
    ' here do whatever you like using the value of b0
  loop
 

RustyH

Senior Member
Hey lbenson,

Thanks for the post, just tried it but it just loops with nothing happening from any input. C.3 seems to be locked
 

RustyH

Senior Member
Sorry Guys, Im struggling with this :confused:

What Im trying to do is create an RGB circuit with a bank of LED strips. I plan to have the colours fade in to each other slowly.
I think I can do the fading using the code I was previously helped with as follows
up_fadeup:

for LED = b.3 to b.7

for on_time = 0 to 3000 step 40
pulsout LED,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
high LED

next LED

Do
Loop Until pinB.0 = 0 And pinB.1 = 0


up_fadedown:

for LED = b.3 to b.7

for on_time = 0 to 3000 step 30
pulsout LED ,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
low LED

next LED

However, what I want to do is use an input switch that with each press it change the speed at which the colours change from one to the other, probably 10 level of speed setting.


Im not to good at coding yet, so currently have a hand full of hair from pulling it out!!!

Sorry to ask....but any chance of some help?
 

RustyH

Senior Member
Ive just added in the button code thanks to the previous posts, how do I say that is b0 = 1 then Speed should be 1000, if b0 = 2 then speed = 2000, etc??

#picaxe 08m2
#no_data
setfreq m16


symbol Red = 0
symbol Blue = 1
symbol Green = 2
symbol Speed = b0
symbol on_time = w2
symbol off_time = w3


main:
do
if pinc.3 = 1 then
do while pinc.3 = 1 : loop ' wait for release
inc b0
if b0 > 10 then
b0 = 0
endif
endif

RedOn:
for on_time = 0 to 3000 step 1000
pulsout Red,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
high Red

GreenOff:
for on_time = 0 to 3000 step 1000
pulsout Green,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
low Green


Pause Speed

BlueOn:
for on_time = 0 to 3000 step 1000
pulsout Blue,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
high Blue

RedOff:
for on_time = 0 to 3000 step 1000
pulsout Red ,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
low Red

pause Speed

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

BlueOff:
for on_time = 0 to 3000 step 1000
pulsout Blue ,on_time
off_time = 3000 - on_time
pauseus off_time
next on_time
low Blue

pause speed

goto main
loop
 

RustyH

Senior Member
Dont I feel stupid now!! Thats quite simple

Do I enter that as part of the symbol command

symbol speed = b0 * 1000
 

lbenson

Senior Member
Hey lbenson,

Thanks for the post, just tried it but it just loops with nothing happening from any input. C.3 seems to be locked
Works for me in the simulator. When you click pinc.3 on it loops in the "do while" until you click it off. Then it increments b0 and resets it to 0 when it is greater than 10.
 

lbenson

Senior Member
Do I enter that as part of the symbol command

symbol speed = b0 * 1000
Because a byte variable cannot contain any non-zero value times 1000, you must make speed a word variable. Your symbol line cannot contain executable code, so:

symbol speed = w7

...

speed = b0 * 1000
 

RustyH

Senior Member
whooohooooo, it works a treat, thank you so much for all your help.

Final code is below, hopefully it all looks ok and efficient.

One thing though, if you want to change the speed (with the external button), you would have to press and hold it till it ran through all the code and got back to the b0 loop. Is there anyway to run it independantly so you can press it any time and it will alter the value??

Cheers

Code:
#picaxe 08m2
#no_data
setfreq m16


symbol Red = 0
symbol Blue = 1
symbol Green = 2
symbol Speed = w0
symbol on_time = w2
symbol off_time = w3


main:
  do
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      b0 = b0 + 1 // 11 ' 0 to 10
      endif
Speed = b0 * 10000

RedOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Red,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Red
        
GreenOff:
	for on_time = 0 to 3000 step 1000
          pulsout Green,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Green        

    
Pause Speed

BlueOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Blue,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Blue
        
RedOff:
	for on_time = 0 to 3000 step 1000
          pulsout Red ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Red

pause Speed

GreenOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Green,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Green
        
BlueOff:
	for on_time = 0 to 3000 step 1000
          pulsout Blue ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Blue

pause speed

goto main
loop
 

lbenson

Senior Member
Note that this line, "Speed = b0 * 10000" will give incorrect values when b0 is greater than 6, because a word variable has a maximum value of 65,535 (2 to the 16th minus 1).

You may be able to avoid the use of an interrupt to respond to button presses by testing to see if pinc.3 is pressed within each "for" loop as follows: "if pinc.3 = 1 then main".

The command "goto main" before "loop" is not needed--the "loop" will take you back to check for a button press again.
 

westaust55

Moderator
limit the mutliplier to 6550 and all will be well.
then when b0 = 10 (the max value)
speed = 6550 * 10 = 65500, which is within the max value of 65535 for a word variable.
 

RustyH

Senior Member
evening all,

I had a read about the interrupt command and entered it in to my code below. It works well for the first instance but then it does not seem to reset. Where have I gone wrong?

Code:
main:
  do
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      b0 = b0 + 1 // 11 ' 0 to 10
      endif
Speed = b0 * 6550
setint %00001000, %00001000



RedOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Red,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Red
        
GreenOff:
	for on_time = 0 to 3000 step 1000
          pulsout Green,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Green        

    
Pause Speed

BlueOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Blue,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Blue
        
RedOff:
	for on_time = 0 to 3000 step 1000
          pulsout Red ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Red

pause Speed

GreenOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Green,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Green
        
BlueOff:
	for on_time = 0 to 3000 step 1000
          pulsout Blue ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Blue

pause speed

goto main
loop

interrupt:
setint %00001000, %00001000
goto main
 

eclectic

Moderator
1. Your present interrupt routine doesn't do anything except return.

2. Try adding, say b0 = b0 *.. or whatever you need.

3. And put the first Setint command at the TOP of the program
before Main.
Easier to control.


e
 

RustyH

Senior Member
Hi e,

Thanks for the quick reply.

I tried adding in just the line of b0= b0 + 1 // 11 ' 0 to 10 and tried it but again it works fine on the first activation of the interrupt, but it didnt seem to reset and pressing pinC.3 a second time did nothing.

I also tried it with the whole of the main routine entered in to the interrupt routine as below......but again it didnt seem to reset?

Ive also added the first setint to the top as you recommended

Code:
#picaxe 08m2
#no_data
setfreq m16


symbol Red = 0
symbol Blue = 1
symbol Green = 2
symbol Speed = w0
symbol on_time = w2
symbol off_time = w3


setint %00001000, %00001000


main:
  do
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      b0 = b0 + 1 // 11 ' 0 to 10
      endif
Speed = b0 * 10000



RedOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Red,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Red
        
GreenOff:
	for on_time = 0 to 3000 step 1000
          pulsout Green,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Green        

    
Pause Speed

BlueOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Blue,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Blue
        
RedOff:
	for on_time = 0 to 3000 step 1000
          pulsout Red ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Red

pause Speed

GreenOn: 
	for on_time = 0 to 3000 step 1000
            pulsout Green,on_time
            off_time = 3000 - on_time
            pauseus off_time
        next on_time
        high Green
        
BlueOff:
	for on_time = 0 to 3000 step 1000
          pulsout Blue ,on_time
          off_time = 3000 - on_time
          pauseus off_time
       next on_time
       low Blue

pause speed

goto main
loop

interrupt:
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      b0 = b0 + 1 // 11 ' 0 to 10
      endif
setint %00001000, %00001000
goto main
 

eclectic

Moderator
Quick reply

Very bottom line.
Interrupt:
needs a Return,
not a goto main.

Secondly, just my opinion
The Pulsout system has to continually work to stand still.

How about using a
14M2 or a 20M2?

Four Pwmout channels which are "set and forget"

e
 

RustyH

Senior Member
Hi e,

Ive already built my circuit based on the 08, but I guess if 14 or 20 is need I could switch. Im not sure I follow you in regards to "The Pulsout system has to continually work to stand still" :confused:
 

RustyH

Senior Member
Ok :) I have finished my code I think, thanks for alot of people on here :)

It works well, the only issue I see from the memory log during simulation, is when I press the C.3 pin, it does not seem to increment by 1, instead its quite random, sometimes jumps up by 2 sometime 3. Is this just a simulator issue?

Also, just fro a point of view of learning to code efficiently, is there actually a more slimline method of using the On and Off fade loop only once and have it sweep through the Red, Blue and Greeb, rather than using the On / Off fade for each colour seperately?

Code:
#picaxe 08m2
#no_data
setfreq m16

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.1,b1  ‘read the value
if b1<20 then chase  ‘range 0-20
goto main


chase:
speed = b0 * 6650

	RedOn: 
		for on_time = 0 to 3000 step 1000
            	pulsout Red,on_time
            	off_time = 3000 - on_time
            	pauseus off_time
        	next on_time
        high Red
        
	GreenOff:
		for on_time = 0 to 3000 step 1000
          		pulsout Green,on_time
          		off_time = 3000 - on_time
          		pauseus off_time
       	next on_time
       low Green        

	pause speed

	BlueOn: 
		for on_time = 0 to 3000 step 1000
            	pulsout Blue,on_time
            	off_time = 3000 - on_time
            	pauseus off_time
        	next on_time
	  high Blue
        
	RedOff:
		for on_time = 0 to 3000 step 1000
          		pulsout Red ,on_time
          		off_time = 3000 - on_time
          		pauseus off_time
       	next on_time
        low Red

	pause speed

	GreenOn: 
		for on_time = 0 to 3000 step 1000
            	pulsout Green,on_time
            	off_time = 3000 - on_time
            	pauseus off_time
        	next on_time
        high Green
        
	BlueOff:
		for on_time = 0 to 3000 step 1000
          		pulsout Blue ,on_time
          		off_time = 3000 - on_time
          		pauseus off_time
       	next on_time
        low Blue

	pause speed

goto main


interrupt:
    if pinc.3 = 1 then
      do while pinc.3 = 1 : loop ' wait for release
      inc b0
      b0 = b0 + 1 // 11 ' 0 to 10
      endif
setint %00001000, %00001000
return
 

eclectic

Moderator
Hi e,

Ive already built my circuit based on the 08, but I guess if 14 or 20 is need I could switch. Im not sure I follow you in regards to "The Pulsout system has to continually work to stand still" :confused:

With
Pulsout, you have to continually send instructions.
A bit like continually steering

With Pwmout, you send one instruction
and it stays at that value.

A bit like automatic pilot.


Play with a real Picaxe, and you'll soon see the difference.

e
 

RustyH

Senior Member
Just set all my circuit up and it works a treat :)

as mentioned though, the speed increament seems to be quite random....can anyone see a reason why??
This was doing the same in the simulator as well
 
Last edited:

RustyH

Senior Member
its ok, I think I found the issue, I think I had doubled up the incriment command......

interrupt:
if pinc.3 = 1 then
do while pinc.3 = 1 : loop ' wait for release
inc b0
b0 = b0 + 1 // 11 ' 0 to 10
endif
setint %00001000, %00001000
return

So have taken out the "inc b0" part and it seems to work spot on now.

I have also added in a LED flash to tell me how many times the button has been pressed (or what value is in b0).........does that look correct to you guys??

interrupt:
if pinc.3 = 1 then
do while pinc.3 = 1 : loop ' wait for release
b0 = b0 + 1 // 7 ' 0 to 6
endif

setint %00001000, %00001000

do
for b1 = 1 to b0
high Red
pause 2000
low Red
pause 1000
next b1
exit
loop
return
 
Top