Help with If or Elseif working in spin??
mikediv
Posts: 825
Hi guys I have been building a project and pouring over the prop manual and the Labs but here is my problem
I need to use a command not sure which one to run a subroutine if a condition occurs , let me explain
repeat ' Repeat
if ina[noparse][[/noparse]20] == 1 ' If PIN1 equals a 1 (PIR is triggered)
' !outa[noparse][[/noparse]16] ' Toggle PIN16 (LED on)
'waitcnt(clkfreq/10 + cnt) ' Wait a 10th of a second
'!outa[noparse][[/noparse]16] ' Toggle PIN16 (LED off) ignore this part just showing what the code looks like I commented out the 3 lines after if ina[noparse][[/noparse]20] == 1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
What I need to do is "if ina[noparse][[/noparse]20] == 1 ' if the input P-20 is equal to 1 then I need to run this routine below
Dira[noparse][[/noparse]16]:= 1
repeat
Outa[noparse][[/noparse]16]:= 1
waitcnt(clkfreq/900 + CNT)
outa[noparse][[/noparse]16]:= 0
waitcnt(clkfreq/400*2 +CNT) then I need the program to go back to the top of code and start over
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
this of course this is only a part of the code what I really need to be able to understand is what command to use if an input is equal to something I know how to do a if or elseif but ony to run another line of code not a little subroutine
I need to use a command not sure which one to run a subroutine if a condition occurs , let me explain
repeat ' Repeat
if ina[noparse][[/noparse]20] == 1 ' If PIN1 equals a 1 (PIR is triggered)
' !outa[noparse][[/noparse]16] ' Toggle PIN16 (LED on)
'waitcnt(clkfreq/10 + cnt) ' Wait a 10th of a second
'!outa[noparse][[/noparse]16] ' Toggle PIN16 (LED off) ignore this part just showing what the code looks like I commented out the 3 lines after if ina[noparse][[/noparse]20] == 1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
What I need to do is "if ina[noparse][[/noparse]20] == 1 ' if the input P-20 is equal to 1 then I need to run this routine below
Dira[noparse][[/noparse]16]:= 1
repeat
Outa[noparse][[/noparse]16]:= 1
waitcnt(clkfreq/900 + CNT)
outa[noparse][[/noparse]16]:= 0
waitcnt(clkfreq/400*2 +CNT) then I need the program to go back to the top of code and start over
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
this of course this is only a part of the code what I really need to be able to understand is what command to use if an input is equal to something I know how to do a if or elseif but ony to run another line of code not a little subroutine
Comments
The Formatted code button is very helpfull for posting code thats a bit easier to read.
I think your question is how to run a subroutine based on an if statement? Or at least several lines of code?
As always there is more than one way to skin the cat, see I can't tell by your code if your aware of the obsession that spin has with indentation:
I think you want something like this, but I'm unsure what the repeat statement is for after the if so I've removed it.
·This will keep going forever...keep checking the value of ina[noparse][[/noparse]20]..if it does equal 1 then it will execute all of the indented lines under the if...once they are done it will go back and check the if statement again because it is indented below the repeat statement. You should see the little gray lines that get drawn by the proptool when you indent, either with spaces or tabs.
Is this what you where looking for?
Rick
cassLan I will format my code going forward if you could just give me a quick how to, also I added the code you supplied thank you
very much it worked fine , see I kind of knew how to do that but I thought there would be some way to name my routine
like make a small piece of code then write
if ina is equal to something then go to motor or something like that , but as I stated your sample worked I am very grateful
Post Edited (mikediv) : 5/6/2009 2:21:15 AM GMT
-Phil
That what you meant?
There are no goto's in spin, it's more like gosub, just remember you have to indent.
Rick
so no matter how many times we read the same thing it does not always register, but I digress ,, Thank you for taking the time to explain this and show me samples I have already implemented into my code
and plan on sharing this latest project with everyone thanks everyone