Button again.

alband

Senior Member
I've just read the other button thread and thought "hu, that looks useful and not too hard" but I've just tried it and I'm lost.

I would have kept going in the other tread but I think it is a more advanced discussion of the button command, I just want to know some basics (I hope).

Eventually I want to have three buttons. Up, down and select. I want it do loop through a 7x7 grid of LED's using the whole press and hold thing to scroll through them. But for now I'm going to start basic. Just try and get a variable to scroll up and down. I also want to incorporate a select button so that in the real thing, the currently lit LED is selected. But for the basic one now, just to select the byte variable's current number, and display "PICAXE" on a screen or something silly, just so I know it's worked. In the final thing I also want to use porta as the pin but I don't think this is possible. Is it?

I know the the button command can be used like a keyboard key; press and hold for a certain length of time and it will repeat for you. But after trying it I'm not at all sure. For a start I havn't got a clue what it means by:
Targetstate is a variable/constant (0 or 1) which specifies what state (0=not
pressed, 1=pressed) the button should be in for a branch to occur.
- Address is a label which specifies where to go if the button is in the target
state.
Nor as to what this bit is for.

Can someone please help me and explain what each bit of the syntax means, in english.

For ease here is the manual link and its page 29: http://www.picaxeforum.co.uk/docs/datasheets/picaxe_manual2.pdf

Attached is my current rubbish code.

Thanks in advance and well done for reading this lengthy post.:D
 

Attachments

BeanieBots

Moderator
I'll try to explain:-

Button Pin,DownState,Delay,Rate,Var,TargetState,Address

Let's say you have a program with loop like this:-

Main:
Var=0
Do
Pause 10
Button Pin,DownState,Delay,Rate,Var,TargetState,Address
Loop

Address:
W1=W1+1
goto main

Obviously, you need to define a few things first.
Pin needs to be a single digit for your button input pin
DownState is the logic level on that pin when the button is pushed.
Delay will be how many times around the loop until the repeats start.
Rate is how many times around the loop until the next repeat.
Var can be any variable but obviously not b1 or b2 in this example.
TargetState is the logic level on 'pin' that will cause your program to jump to "Address".
Address, is where it will jump to when "pin" is at logic state "TargetState".

Hope that helps.
 

alband

Senior Member
Ok,

Well I've now got this:
Code:
		let b0 = 0 'it says to make sure this starts as zero.
main:		
		button 1,1,20,10,b0,1,label_1
		goto main
		
label_1:	b1 = b1 + 1
		goto main
But when I press the button, b1 goes to 1 then stays there. The program loops until I release the button (b1still staying at 1) then when I press the button again b1 still stays at 1.
 

BeanieBots

Moderator
You MUST have a pause as in my example code or the loop will be too fast to distinguish the first press from the repeats.
For pause 10, you want a "delay"/"rate" value of 1 for every 0.1S.
So maybe 100 and 10 to start with.

Appart from that, it should work, so maybe a hardware problem.
Check that your "button" is on the right pin and that it pulls up to Vcc and has a pull-down resistor fitted.
 

alband

Senior Member
That makes sence but is still only stays on 1???

Code:
		let b0 = 0 'it says to make sure this starts as zero.
main:		
		button 1,1,20,100,b0,1,label_1 'not know what that las one is for so I just put is to main
		pause 10
		goto main
		
label_1:	b1 = b1 + 1
		goto main
 

BeanieBots

Moderator
It works for me on a PICAXE.
However, it does NOT work in the simulator.
Seems to get "lost" after the first jump.

One for Technical I'm afraid.
 

alband

Senior Member
Right, I was only trying on the simulator.
Lets try the download...

Seems to work. I'll do some experimenting instead of bothering you again.
Thanks!
 

alband

Senior Member
Well I going to bug you again; sorry.
I've got it working (attached) but I want to use porta as the input pin.
Is there any way to do that?
 

Attachments

westaust55

Moderator
Port A use 28X/X1 and 40X/X1

I see reference in a past thread you started to PICAXE 40X (and “I do not have a 40X1”) so I presume the 40X is the chip you have.

The 28X/X1 and 40X/X1 have port A which is primarily used for analog inputs.

The Port A inputs can be used as digital inputs but you are limited to testing the state (High or Low) using the IF portA (pin no) THEN . . .
 

tiscando

Senior Member
On the simulator, the program flow gets stuck at the first button command when pin1=1 but the second button command down works ok. could adding 'pause 10' before the button command make it work?
(the variables should be 0 after the picaxe is resetted.)

main:
button 1,1,100,10,b3,1,inc1 'gets stuck at this one.
button 2,1,100,10,b4,1,dec1 'this one works ok.
pause 10
goto main

inc1:
inc b1
goto main

dec1:
dec b1
goto main
 

alband

Senior Member
Yes I found that, on the second one it works but on the first it doesn't. Mustn't be programmed the same as the chip or something.

Do you think there is any way of using the analogue inputs as digital pins as normal, (if porta etc...) but getting the keyboard effect. Are there any other limitations as to what the pin number can be. Could it be a variable for instance?
e.g.
Code:
read adc b0
button b0,1,100,10,b3,1,inc1
 
Top