How to check if a pin is high/low ?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

How to check if a pin is high/low ?

Post by octal » Tue Jan 30, 2007 2:38 pm

Hi,
is there any (internal) SWFBasic command to check if a Pin of a selected port is high or low ?
in PBP we where to use a syntax like

Code: Select all

if PORTD.0 then doSomething
I would like to write a function that check if a Pin passed as a parameter byRef is High or Low, so I have written something like

Code: Select all

Function CheckButton(ByRef aPin As Bit) As Boolean
    If aPin Then 
      Debounce(Temporisation) ...
      If aPin Then   <<<<<<<< SWFBasic do not accept this test
        result = true
        Exit
      End If
    Else
      result = false      
    End If   
End Function
I would like to be able to pass any pin as parameter, for example

Code: Select all

   if CheckButton(PORTD.0) then ....
   if CheckButton(PORTA.3) then ....
   if CheckButton(PORTB.2) then ....
Thanks

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jan 30, 2007 3:18 pm

Passing bits by reference is not very efficient, but if you want to do it...

>> If aPin Then <<<<<<<< SWFBasic do not accept this test

Because it is not a boolean expression, try

IF aPin = 1 THEN

or use a typecast. This should also work...

Code: Select all

function CheckButton(byref aPin as bit) as boolean
    result = (aPin = 1)
    if result then
      Debounce(Temporisation) 
      result = (aPin = 1)
    endif
end function 

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Jan 30, 2007 3:30 pm

Thank you David,
I can pass the param by value. It's not a problem for me. The ByRef in the code I posted is just a result of Copy/Paste operation :oops: from another function I had written (and that affects the state of the Pin).
In this case I'll pass it by value.

regards
Octal

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jan 30, 2007 3:35 pm

If you pass by value, you will not be able to read the state of the pin when (or if) the pin changes state after the debounce, because you are only passing a copy of the pin state - so you will have to pass by reference.

I was just pointing out it is not very efficient, as the compiler has to use indirect addressing on a byte and then create a mask to access the bit value.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Jan 30, 2007 3:48 pm

David Barker wrote:If you pass by value, you will not be able to read the state of the pin when (or if) the pin changes state after the debounce, because you are only passing a copy of the pin state - so you will have to pass by reference.

...
.
Ahhhhhh .... yes ... you are right David. Thank you very much.

Well,
if I declare my Pins like that

Code: Select all

Dim 
    Button1 as PORTD.Booleans(1),
    Button2 as PORTA.Booleans(2),
    Button3 as PORTB.Booleans(5)

function CheckButton(byref aPin as Boolean) as boolean
    result = aPin
    if result then
      Debounce(Temporisation)
      result = aPin
    endif
end function

And I call it like that 
if CheckButton(Button1) then ....
if CheckButton(Button2) then ....
if CheckButton(Button3) then ....
Does this code do the same? are there any differences from the previous function (in terms of functionnalities) ?

Kind regards

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jan 30, 2007 3:59 pm

> Does this code do the same? are there any
> differences from the previous function (in terms of functionnalities) ?

Yes, it looks fine and the functionalty appears to be the same.

Post Reply