Shop OBEX P1 Docs P2 Docs Learn Events
If statement — Parallax Forums

If statement

DinodenDinoden Posts: 19
edited 2010-02-03 02:31 in Propeller 1
Hi

I have a quick question on the IF statement. I know how to use it like shown below. But what I am after is to know how to go to another place in a program if the IF statement is true. like with this PIC example " GOTO START" if an IF statement is true. I have downloaded the PE labs fun book and I am using it but no good example.

Sorry if this is basic, but I am very new at spin. I have only worked with the PIC ic's


IF INA [noparse][[/noparse]15]:=1
OutA[noparse][[/noparse]0]:= 1

I would like to change the example above to work like the bottom example

IF INA [noparse][[/noparse]15]:=1
GOTO START

I know this is not the right syntax, but I need something that works like this.

Thanks

Comments

  • CassLanCassLan Posts: 586
    edited 2010-02-02 19:11
    Hi Dinoden,
    A·Couple of things:
    Spin is very indentation specific, for that reason you should use the code buttons so that you may indent properly on the forum.
    IF INA [noparse][[/noparse]15]==1
         OutA[noparse][[/noparse]0]:= 1
    

    Also notice that in an IF statement, you must use the " == " operator, for setting the value you would use the " := " operator.

    There is also no GOTO function in Spin, you must use a sub-routine instead like this example:
    PUB Main
         IF INA [noparse][[/noparse]15]==1
              Start
     
    PUB Start
         OutA[noparse][[/noparse]0]:=1
    

    Does that make sense?

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PrEditor·- Text Editor


    NYC Area Prop Club





    ·
  • mikedivmikediv Posts: 825
    edited 2010-02-02 19:14
    Hey CassLan that's awesome I really understand this now Dinoden didn't meant to interrupt Just wanted CassLan to know how helpful this example is
  • DinodenDinoden Posts: 19
    edited 2010-02-02 19:37
    Yes it does make sense. I do use the indent function all the time so I can get use to it. When I cut and pasted, it removed my indent. It's one of my old test programs where I used := instead of ==. I lost a lot of hair with that early mistake. So then I can use more PUB's in my program like "LABELS" for PIC basic to go to if needed.

    Thanks a lot!

    This will keep me going for a while.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-02 19:55
    You can't use PUBs (or PRIs) like the labels in Basic. They're subroutines. If you treat them as labels (and subroutines as GOTOs) you'll get a stack overflow pretty quickly because you're doing subroutines calls without returns.

    You just have to program differently without GOTOs. Use the REPEAT statement for loops and the IF / THEN / ELSE statement for decision making. Do use subroutine (method) calls and RETURNs for encapsulating functionality. You can use the ABORT statement to get out of deep parts of your program, particularly for error exits.
  • DinodenDinoden Posts: 19
    edited 2010-02-02 20:16
    Thanks again guys.

    Every little bit helps. Thanks for the tip. I have done hardware design (discreet ic's) pretty much my whole professional career, but I sure like how software / processors can reduce a circuit down to practically nothing.
    I am really enjoying this new to me Propeller!

    Thanks
  • DinodenDinoden Posts: 19
    edited 2010-02-02 20:33
    Hi Again.

    I just wanted to share one of my biggest PIC18F452 projects. I know it is not Propeller, but I probably will do another large project with the Propeller when I become fluent with it.
    2592 x 1944 - 2M
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-02-03 01:57
    Sweet!. You built a pinball machine?

    Thinking laterally here, wouldn't it be just cool to use a Propeller to drive some old-school 16 segment alphanumeric led displays for a pinball machine.

    Re GOTO, pic basic and other forms of basic kind of force you to use GOTO. There is a concept called structured programming and it was well established before the 1980s so it has been around for a long time, which says you can write anything without GOTO. It takes a little getting used to, but it leads to the idea of "top down programming".

    I tend to find programs are one of two types - run once then end, or run forever.

    run once:
    main
    ...
    end
    
    


    and run forever (in a basic form)
    main
      do
        subroutine1
        subroutine2
      loop
    
    



    that is the 'top' bit of top down programming as subroutine1 has not been written yet but you have a vague idea that it is going to flash a led or something.

    then add a condition to it
    main
      do
         if condition = 1 then call subroutine1
    
      loop
    
    subroutine1
      ' put code here
    return
    
    



    Then translate that to spin. Going from Basic to Spin I find there are some key things to watch :=,== and = as they all crop up in spin but in basic they are all =
    And there is no 'end' to statements. eg in C you put { then } to signify the end of a block of code. In structured basic languages (sbasic, vb.net etc) you might have 'end' or 'end if' or 'repeat... until' or 'while... wend'. And in between those you put proper indenting because it looks better that way. In spin, the indenting is the essential part of the language. So change
    do
       expression1
       expression2
    loop
    
    



    becomes
    repeat
      expression1
      expression2
    
    



    And the end of the indenting is the indicator the same as } or 'end' or 'endif'

    What is the project you are working on?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller

    Post Edited (Dr_Acula) : 2/3/2010 2:03:14 AM GMT
  • DinodenDinoden Posts: 19
    edited 2010-02-03 02:31
    Hi Dr_Acula.

    Yes, I built this 1/2 scale pinball. I built the cabinet, all circuit boards & PIC programming. It took me about a year to complete. I love it! Works great!
    I have run vacuum fluorescent alphanumeric displays from discarded microwave ovens / VCR's with PIC's. Very neat.

    But right now I don't really have a project in mind for the Propeller. I want to learn this chip because I see so many advantages with the independent cogs, speed(80MHz) etc.

    My project will probably lean towards gaming of course. I love building games! I also built a video slot machine a few years ago on a small 7" colour CRT I salvaged. I used the STV5730 OSD for the video display.

    Anyway, my propeller is now running a 5X7 matrix which I am going to make a simple game to start learning the Propeller. There is a small "ship" 4 pixels, which I am going to make fire, different leds lighting up as it leaves the ship. The ship can move left to right and falling targets will fall (individual leds). This may seem easy to all you pros, but for my first self made propeller "tutorial" it's been a bit of a challenge. I can't wait to figure this thing out.

    Nice talking to you.
Sign In or Register to comment.