how to assign pins, variables etc

rigidigital

Senior Member
This code works on the dev axe091 when switch is pressed 5 sec delay perfect !
I used Pin2 to assign pin 2 on an 08M


main:
high 1
pause 1000
low 1
pause 1000
if pin2 = 1 then pause 5000 endif
goto main

so 1 can mean pin 1 and then I used 1 to mean High
also you cant use a statement like

high pin1 , its a syntax error though I'dhave thought it meant or would be equal to
high 1
 

Andrew Cowan

Senior Member
I'm not sure I'm following you.

High (pin) switches that pin high. So High 1 makes pin 1 high.

In an IF statment, you can use pin names or variables. To check a variable, use the variable name (eg if b1=1 then...). To check a pin, use the pin name (eg if pin2=1 then...). The '=1' part is checking to see if it is logic high or logic low.

As high is always talking about a pin, never a variable, there is no need for the 'pin' part.

A
 

kd5crs

Senior Member
pin1, pin2, etc are variables containing the pin state.

If you are checking an input, use pin1, pin2...

If you are changing an output, use 1, 2...

Hope that helps. I still goof that up from time to time, but the compiler is good at telling your which one to use.

Brian
 

westaust55

Moderator
You probably need to spend a little more time reading manual 2.

for "pins"

For most PICAXE chips
- When used on the left of an assignment pins applies to the ‘output’ port
eg Let pin1 = 1

- When used on the right of an assignment pins applies to the input port
e.g. let bit9 = pin1 (bit9 is a one bit variable - a byte is 8 bits)

furthermore, Rev Ed also have the term outpin which can be (preferred) used when referring to outputs.
eg Let outpin1 = 1 ; may help make it clear it is an output


The look at the pages for each command and the syntax (structure) of the command parameters and the examples typically lower down the pages..

eg:
syntax: HIGH pin {,pin,pin...} example HIGH 5 or for X2 parts HIGH C.5 (port.pin)
syntax: HIGH PORTC pin {,pin,pin...} example HIGH PORTC 3


syntax: IF PORTC pin ?? value {AND/OR variable ?? value ...} THEN address
example IF PORTC pin5 = 1 THEN . . . .
 

BeanieBots

Moderator
high pin1 , its a syntax error though I'dhave thought it meant or would be equal to
high 1
Think about that for a little longer!
It is only a syntax error because people have made this mistake before and Rev-Ed decided to lock out of the syntax but it is actually perfectly fine and in my opinion should be permitted just in case somebody might genuinely want to do it.

pin1 is an INPUT condition.
It can either have the value 0 or 1
So, High pin1 would either make output 0 high or output 1 high depending the condition present at input 1.

Here's another simple example.

Do
Toggle pin1
pause 250
loop

If you had a switch on input 1, switching it would change from an LED on output 1 flashing to an LED on output 0 flashing. Useful? not terribly, but IMHO valid syntax.
 

hippy

Technical Support
Staff member
"High pin1" gives an error and an explanation as to what's most likely required rather than the usual "syntax error".

This was a pragmatic solution to numerous people using "High pin1" and similar and not understanding why the code did not behave as they expected. Though valid syntax it's extremely uncommon to use in that form and it's easy enough to have "b0=pin1:High b0" so the ability isn't missing if it is really wanted. It has a 'for the greater good' argument, solving more problems than it causes.
 

cmast

Member
So, High pin1 would either make output 0 high or output 1 high depending the condition present at input 1.
Its things like this that make me want to run back under the duvet!

I don't understand how "High pin1" could refer to more than one port, and also be an IF statement. Totally confused.
To me High pin1 reads as this:
"OK, please shove 5volts out on pin1 , oh yeah mr. interpreter and i mean physical pin1, thankyou.. ..now, next command please" . :confused:
 

hippy

Technical Support
Staff member
If you start with the following ...

High 0
High 1

They will set pin 0 and pin 1 high respectively.

You could also have used a variable which contained 0 or 1, and that would do the same thing ...

b0 = 0
High b0

b0 = 1
High b0

Of course you could have any number in b0 to set any of the pins 0 through 7 high ( for a PICAXE which has 8 outputs ).

You can also read the status of an input pin and put it in a variable ...

b0 = pin3

This will put a 0 or a 1 into b0 depending on the voltage applied to input pin 3 leg.

So ...

b0 = pin3
High b0

Input pin3 can be low or high, b0 will be 0 or 1 respectively, output pin 0 or 1 will be set high.

Because 'pin3' is a variable, just like 'b0' is a variable, you don't have to take the intermediate step of moving pin3 into b0 but could - if we allowed it - use ...

High pin3

So you're not saying "put pin3 high", what you are saying is read "pin3" and if low set pin 0 high, if high set pin 1 high.

Effectively it is the same as ...

If pin3 = 0 Then : High 0 : End If
If pin3 = 1 Then : High 1 : End If
 

hippy

Technical Support
Staff member
Analogy time, which may help ...

Think of "pin3" being an envelope which can contain a bit of card with a number on. The number can only be 0 or 1.

"High 0" says set output pin 0 high.

"High 1" says set output pin 1 high.

"High pin3" says open the envelope labelled "pin3", set the output pin whose number is written on the bit of card high.
 

cmast

Member
If you start with the following ...

High 0
High 1

They will set pin 0 and pin 1 high respectively.
OK - Understood

You could also have used a variable which contained 0 or 1, and that would do the same thing ...

b0 = 0
High b0

b0 = 1
High b0

Of course you could have any number in b0 to set any of the pins 0 through 7 high ( for a PICAXE which has 8 outputs ).
OK - So, if b4 contained value 3 then High b4 would set output 3 to 5v. If it contained 2 then it would set output 2 to 5v.
So then it would follow that Low b4 would set output 3 to 0v (or low) volts (assuming b4 contained 3?


You can also read the status of an input pin and put it in a variable ...

b0 = pin3

This will put a 0 or a 1 into b0 depending on the voltage applied to input pin 3 leg.
Understood
So ...

b0 = pin3
High b0

Input pin3 can be low or high, b0 will be 0 or 1 respectively, output pin 0 or 1 will be set high.

This is where I start reaching for the paracetamol, because not least it makes the program hard to follow. I'll try: So you're testing for the value at absolute pin 3, thats input 3. It can only be (as you said) a 1 or a 0. If it is a 1 then set ouput pin 1 high else set output 0 (pin0) high. So at this stage in the program you have now definatately set an output to On, but you dont know which one*, and there is every possibility that the next (or subsequant) lines of code will change the value of pin3 (which may or may not alter the output that has just been set).

*Unless you stick that value in a var for later use, or test it now, which is why I cant see how this could be a useful command to use, because it makes it hard to follow the program (IMHO).
Maybe there are circumstances that it is useful? Set either of 2 alarms/LEDs, leaving it up to the user to intervene; but then User has to be present, and notice the condition, then to intervene tho...


Because 'pin3' is a variable, just like 'b0' is a variable, you don't have to take the intermediate step of moving pin0 into b0 but could - if we allowed it - use ...


High pin3

So you're not saying "put pin3 high", what you are saying is read "pin3" and if low set pin 0 high, if high set pin 1 high.

Effectively it is the same as ...

If pin3 = 0 Then : High 0 : End If
If pin3 = 1 Then : High 1 : End If

So really its all a moot point because you're saying this is not an allowed command anyway?
Well! Headache gone, I think.

Thanks for the explanation Hippy. I can almost hear you (and others too) banging your head
 

hippy

Technical Support
Staff member
I cant see how this could be a useful command to use, because it makes it hard to follow the program (IMHO).

That's about the sum of it, and moot as you say as it's not allowed.

In the years I've been using PICAXE there's only been one program which used the technique ( before it was 'banned' ). I cannot remember what that code did but it was extremely optimised and more 'clever tickery' than anything else to squeeze every drop out of an original PICAXE-18. There are few cases where being allowed to use the command would be beneficial and there are alternatives if it is needed, and we have much faster PICAXE these days.
 

westaust55

Moderator
and there is every possibility that the next (or subsequant) lines of code will change the value of pin3 (which may or may not alter the output that has just been set).
Not possible except for the 08M.

pin3, or any other pin number is a pseudo variable that reflects the state of an input pin ie input 3.

Thus Program code cannot/should not alter the variable pin 3 (except for the 08M which has bi-direction bar pin3 which can only be an input -so many exceptions)

High pin3 is acting upon output pin 0 or 1 depending upon the state of input pin 3 and could (better) be written as.

If you want to control output pin3 then use you could use
outpin3 = 1 ; to set output pin 3 high
 

cmast

Member
If you want to control output pin3 then use you could use
outpin3 = 1 ; to set output pin 3 high
Yeah, that is sensible, easy to read and obvious; and should be the only way to assign an output (or anything* for that matter), I would reckon. Obviously variables values (and input/output values too) are used to assign values, but in the manner you are demonstrating makes it a readable program that can be understood by mere mortals like me (was brought up on VB so I like my code to have lots of plain words and very few strange looking characters!).
* I always get my operands and operators mixed up so used basic english!
 

hippy

Technical Support
Staff member
One thing to remember with outpin3 = 1 is that this only sets what the pin output level will be when that pin is an output, it does not make that pin an output. If the pin is an input it will not be made an output.

Using High 3 on the other hand will make the pin output and set its level high.
 

bpowell

Senior Member
Hello All,

I have quick question I'm having trouble resolving...

I'm working with a 20x2. I have a push-button switch, that if pressed, will start a download sequence (read from eeprom and spit out serial)...pushing the switch will tie pin C.6 to 5V via a 1k resistor.

So, I have this in my code:

Symbol Download = C.6

if Download = 1 then gosub DLSeq

the "If" statement gives me a syntax error...

This statement works:

if PinC.6 = 1 then gosub DLSeq

So...can I not assign a "friendly name" to pin C.6 and use that in future code?

FYI: As I typed this, I had an idea...I changed my Symbol line to:

Symbol Download = PinC.6

This seems to have fixed the issue (passes Syntax test at least)...I think I may have resolved it....just figured I'd post anyway so somebody else can learn from my trials!

Brendan
 
Last edited:
Top