Shop OBEX P1 Docs P2 Docs Learn Events
Help with If or Elseif working in spin?? — Parallax Forums

Help with If or Elseif working in spin??

mikedivmikediv Posts: 825
edited 2009-05-06 17:35 in Propeller 1
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

Comments

  • CassLanCassLan Posts: 586
    edited 2009-05-06 02:07
    Hey Mike,
    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.
    repeat
       if ina[noparse][[/noparse]20] ==1
          Dira[noparse][[/noparse]16]:= 1 
          Outa[noparse][[/noparse]16]:= 1
          waitcnt(clkfreq/900 + CNT)
          outa[noparse][[/noparse]16]:= 0 
          waitcnt(clkfreq/400*2 +CNT)   
     
    

    ·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
  • SRLMSRLM Posts: 5,045
    edited 2009-05-06 02:09
    I'm sorry, but the code that you posted is unreadable. This is due mainly to the fact that the formatting, and hence function, is lost. Use the code option to embed the code in your post and keep formatting, or attach the file.
  • mikedivmikediv Posts: 825
    edited 2009-05-06 02:15
    Thanks guys cassLan how do I format my code online and I will fix it


    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-06 02:20
    Embed it in a [noparse][[/noparse]code]...[noparse][[/noparse]/code] block. You can also copy it to www.phipi.com/format to make it forum-compatible.

    -Phil
  • SRLMSRLM Posts: 5,045
    edited 2009-05-06 02:22
    You use the [noparse][[/noparse] code ] [noparse][[/noparse] /code ] tags (without the spaces) if you don't have the GUI enabled, or you can use the code button on the editor. In the second case, it should show you a blank, light blue box.
  • CassLanCassLan Posts: 586
    edited 2009-05-06 02:36
    I usually use the # button which creates a little grey box to put formatted code in.
    mikediv said...
    see I kind of knew how to do that but I thought there would be some way to name my routine
    You can do it that way as well, especially if you want to re-use that code somewhere else in the program:
    
    
    PUB start   repeat
          if ina[noparse][[/noparse]20] == 1
              motorthingy
     
    PUB motorthingy
       Dira[noparse][[/noparse]16]:= 1 
       Outa[noparse][[/noparse]16]:= 1
       waitcnt(clkfreq/900 + CNT)
       outa[noparse][[/noparse]16]:= 0 
       waitcnt(clkfreq/400*2 +CNT)
    

    That what you meant?
    There are no goto's in spin, it's more like gosub, just remember you have to indent.

    Rick
  • mikedivmikediv Posts: 825
    edited 2009-05-06 17:35
    Thanks guys, CassLan your the man, I am not a programmer but have started with the prop I even have the hydra the prop educational lab kits and can not count how many props, Sometimes the obvious is not that obvious to someone not familiar to programing
    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
Sign In or Register to comment.