Shop OBEX P1 Docs P2 Docs Learn Events
need help with ina command using If statement — Parallax Forums

need help with ina command using If statement

mikedivmikediv Posts: 825
edited 2010-01-03 02:59 in Propeller 1
Hi guys , Happy New Year to everyone .
I need some help I wrote a very simple input command using an if statement but it does not work can anyone tell me why and show me the proper way to do this.

here is my code


Pub TestIf

Dira [noparse][[/noparse] 16 ] := 0

If ina [noparse][[/noparse] 16 ] == 1
Dira [noparse][[/noparse] 15 ] = outa [noparse][[/noparse] 16 ] := 1

repeat
I was hoping that when I pushed a switch ties to P16 that would pull the line high then it would show the output on P15 I know the circuit works when I push the switch P16 goes high but P15 does not . If I change the indentation of the last line Dira [noparse][[/noparse]15 ] = outa [noparse][[/noparse] 16 ] so this line is in front of the If statement by one space then P15 goes high all the time it does not matter what the switch does??? Thanks guys

Comments

  • RossHRossH Posts: 5,534
    edited 2010-01-01 00:34
    Hi mikediv,

    When posting, use [noparse][[/noparse] code ] and [noparse][[/noparse]/ code ] (removing the spaces) to show your code formatted correctly - otherwise you lose indents (which are important in SPIN).

    Shouldn't your "if" statement be within the scope of your "repeat" statement, like this:

    
    Pub TestIf
    
    Dira [noparse][[/noparse] 16 ] := 0
    
    repeat
    
      If ina [noparse][[/noparse] 16 ] == 1
        Dira [noparse][[/noparse] 15 ] = outa [noparse][[/noparse] 16 ] := 1
    
    
    



    Otherwise the "If" statement only gets executed once - probably before you get a chance to hit the switch - then you enter an infinite loop that does nothing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina

    Post Edited (RossH) : 1/1/2010 12:42:34 AM GMT
  • w8anw8an Posts: 176
    edited 2010-01-01 05:34
    Should the line

    Dira [noparse][[/noparse] 15 ] = outa [noparse][[/noparse] 16 ] := 1

    be

    Dira [noparse][[/noparse] 15 ] := outa [noparse][[/noparse] 16 ] := 1

    ?
  • kuronekokuroneko Posts: 3,623
    edited 2010-01-01 05:57
    Actually writing to outa[noparse][[/noparse]15] will help too [noparse]:)[/noparse]

    dira[noparse][[/noparse]15]~~
    repeat
      outa[noparse][[/noparse]15] := ina[noparse][[/noparse]16]
    
  • SamMishalSamMishal Posts: 468
    edited 2010-01-01 07:29
    Hi Mike,
    ·
    Let me see if I can point out what is wrong with the code which will help you learn better and then I will show you how to correct it.
    ·
    I am putting line numbers in· you original code for the purposes of the discussion. I am also going to assume that you had the correct
    INDENTING even though it is·not obvious from the code you posted due to it being in the body of the message.
    ·
    Next time when you want to post code use the # button in the Top of the post editor. This will insert a boxed area and makes it blue
    background like you will see below. You then can cut and paste your code there.
    ·
    Here is the numbered and correctly indented original code
    01    Pub TestIf
    02        Dira [noparse][[/noparse] 16 ] := 0
    03        If ina [noparse][[/noparse] 16 ] == 1
    04             Dira [noparse][[/noparse] 15 ] = outa [noparse][[/noparse] 16 ] := 1
    05        repeat 
    

    ·
    The program of course is not complete since you have not indicated any crystal information…..unless you are going to use the internal oscillator.
    I will not bother to correct this since I am sure you can do that if you need to.
    ·
    When you call the command or when the program starts if the TestIf method is the first one in the code, the method will execute lines 02 03
    and if ina[noparse][[/noparse]16] == 1 also line 04.
    ·
    BUT ONLY ONCE.
    ·
    Then it will enter the empty repeat loop on line 05 and will stay there FOREVER (on line 05 that is).
    ·
    I am sure that is not what you wanted….so lets correct the program as a FIRST ITERATION.· Also notice the indentation.
    ·
    01    Pub TestIf 
    02        Dira [noparse][[/noparse] 16 ] := 0
    03        repeat
    04            If ina [noparse][[/noparse] 16 ] == 1
    05                Dira [noparse][[/noparse] 15 ] = outa [noparse][[/noparse] 16 ] := 1 
    
    

    By placing lines 04-05 inside the repeat statement (notice indentation) you are making the action you want to perform keep being performed repeatedly for ever.
    ·
    Now we still have problems. If you look at line 05 you are using the = operator not the := which is what it should be.
    ·
    But also there is yet another problem. What you are trying to do is set Pin 16 to a value even though it is an input pin. I am sure you meant to do so for pin 15 not pin 16…no?
    But there is another problem. You are setting the direction of Pin15 many times. This is not really needed, since if you set it once outside the loop it will stay that way.
    ·
    So lets improve the program further in one more iteration.
    ·
    01    Pub TestIf 
    02        Dira [noparse][[/noparse] 16 ] := 0  'set Pin 16 as input
    03        Dira [noparse][[/noparse] 15 ] := 1  'set pin 15 as output
    04        repeat
    05            If ina [noparse][[/noparse] 16 ] == 1
    06                Outa [noparse][[/noparse] 15] := 1 
    
    

    ·
    Ok now we have a good program. It· ALMOST works but not quite. We have what you wanted. The program sets pin 16 as an input and pin 15 as an output.
    It then sits in a loop looking at Pin16 and if it is·high it sets Pin 15 to high.
    ·
    BUT….what happens if it is not high???? Pin 15 will just stay high after the first time it is set high because it is never set low again.
    ·
    So one more iteration
    01    Pub TestIf 
    02        Dira [noparse][[/noparse] 16 ] := 0  'set Pin 16 as input
    03        Dira [noparse][[/noparse] 15 ] := 1  'set pin 15 as output
    04        repeat
    05            If ina [noparse][[/noparse] 16 ] == 1
    06                Outa [noparse][[/noparse] 15] := 1 
    07            else 
    08                 OutA[noparse][[/noparse]15] := 0 
    
    

    ·
    OK….that is better….we now have a WORKING program (just do not type the line numbers).
    ·
    BUT….. it is not the best way to do things.
    ·
    If you notice in lines 05 to 08 we are actually setting pin15 to 0 when pin16 is 0 and 1 when it is 1……..can you see that that is actually the same value. So why not replace lines 05 to 08 with just this
    05··········· OutA[noparse][[/noparse]15] := Ina[noparse][[/noparse]16]
    ·
    this will achieve what the previous 4 lines do in just one line.
    ·
    So here is another iteration
    ·
        Pub TestIf 
            Dira [noparse][[/noparse] 16 ] := 0        'set Pin 16 as input
            Dira [noparse][[/noparse] 15 ] := 1        'set pin 15 as output
            repeat
                OutA [noparse][[/noparse] 15 ] := Ina [noparse][[/noparse] 16] 
    
    

    ·
    So here is your final program and you now have a working efficient program.
    ·
    Just one more improvement that is not really NECESSARY it is just another step that would make your program look more like a SPIN program and thus Nifty.
    ·
    However, it may not be easily understood by other people who do not know SPIN and thus you may want to not do it.
    ·
    In SPIN the operator ~ sets something to 0 and ~~ sets something to 1. So the new improvement looks like this
    ·
        Pub TestIf 
            Dira [noparse][[/noparse] 16 ]~                'set Pin 16 as input
            Dira [noparse][[/noparse] 15 ]~~              'set pin 15 as output
            repeat
                OutA [noparse][[/noparse] 15 ] := Ina [noparse][[/noparse] 16] 
    
    

    Now you are a COOL SPIN programmer….but not easily readable by non-Spin programmers…..so you may want to stay at the previous iteration.
    ·
    ·
    One final thing. The line DirA[noparse][[/noparse]16]~· is not REALLY necessary if you have never set Pin16 to output before. The Propeller will have all its pins set as
    inputs already upon boot up. So if you know that Pin16 has not been previously set to output since booting then you can dispense with the line
    ·
    So here is a final final program
    ·
    ·
        Pub TestIf 
            Dira [noparse][[/noparse] 15 ]~~                       ' set pin 15 as output
            repeat
                OutA [noparse][[/noparse] 15 ] := Ina [noparse][[/noparse] 16]   ' pin16 is assumed to be input which is normal state upon boot up 
    
    


    One more point· .......use comments to clarify your logic and what you are trying to do not just for the sake of others
    but also for your own sake when you come back to read the code a few moths from now. Most of the time comments can be
    your Pseudo code during the design stage .... you did design your program ...no?? always design your program before
    coding it.....

    Good luck and I hope you continue to love SPIN.
    ·
    ·
    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com


    Post Edited (SamMishal) : 1/1/2010 7:38:41 AM GMT
  • TonyWaiteTonyWaite Posts: 219
    edited 2010-01-01 11:54
    Samuel,
    A great tutorial!
    T o n y
  • mikedivmikediv Posts: 825
    edited 2010-01-01 17:59
    Wow, you guys just answered so many more question for me than just this one. I have a really bad habit of always putting my repeat command at the bottom I don't know why I just keep thinking it needs to repeat the whole piece of code.
    Ross thank you for the tip I was always afraid to post code here becuase it gets changed that tip will come in handy,,

    SamMishal Thank you so much for taking the time to explain this in such detail it was incredibly helpful sir. The rest of the guys as well Thank you all ....

    I am stuck home for a while and I have been having so much fun with Prop . It makes hardware projects so easy now if I can get spin down it would be awesome...

    Oh w8an you are correct sir ,, but I had only made that mistake posting it here I had the correct syntax on my real program.. Now with Ross's tip I will post the actual code I have on my computer.

    Just wondering though why does the website butcher spin code??? You would think it would just type text like what I am writing right now??

    Post Edited (mikediv) : 1/1/2010 6:04:47 PM GMT
  • SamMishalSamMishal Posts: 468
    edited 2010-01-02 06:36
    mikediv said...
    Wow, you guys just answered so many more question for me than just this one. I have a really bad habit of always putting my repeat command at the bottom I don't know why I just keep thinking it needs to repeat the whole piece of code.
    You are welcome Mike.

    Regarding the Repeat at the bottom, it is sometimes needed. When???

    When the Propeller reaches the end of the code it will go to sleep.....well this is not strictly true,
    but you have to know about cogs and parallel execution for that to make sense and we won't go there
    just yet......

    So if you have code that will run and then you want the Propeller (cog) to go to sleep then you do not
    put a Repeat at the end. HOWEVER, if your code has started a counter to do something (again more
    advanced stuff) then you do not want the Cog to go to sleep since that will also stop the counter.

    In this case even though your code needs to run only once you want to keep the Cog running but just
    not doing much. This is when you just put a Repeat statement all the way at the end of the code.

    That keeps the Cog running and the counters which are doing some work in the background will then
    continue to function.

    So you see it may be useful after all.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com
    ·
  • mikedivmikediv Posts: 825
    edited 2010-01-02 19:02
    Samuel Thank you again. Sam are you the person who wrote Robot BAsic I bought your book and the Robot Bonanza. They are always near by , Everyone should get a copy of both
    they are the best books and program. I have to ask are you going to come out with a more hardware featured book??? I would be first in line.
    Thank you ....
  • SamMishalSamMishal Posts: 468
    edited 2010-01-03 02:58
    mikediv said...
    Samuel Thank you again. Sam are you the person who wrote Robot BAsic I bought your book and the Robot Bonanza. They are always near by , Everyone should get a copy of both
    they are the best books and program. I have to ask are you going to come out with a more hardware featured book??? I would be first in line.
    Thank you ....
    Yes Mike I am and I am very thankful for your kind remarks.... I am glad that you like RB and thanks for using it.
    I am also extremely delighted with your comments about Robot Programmer's Bonanza. I am thankful to you for
    reading and liking it. THANK YOU....

    John and I are definitely working on a more hardware oriented book right now and I think you will like it (I hope). It will
    also have a lot of Propeller stuff too.

    But we are also thinking hard about writing a·more Spin oriented book....but more astute minds than mine are already
    doing so... so maybe it is not necessary.

    Thanks again for using RB and reading my book and I hope that you continue to find RB useful in the future.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com
    ·
  • SamMishalSamMishal Posts: 468
    edited 2010-01-03 02:59
    TonyWaite said...
    A great tutorial!
    THANK YOU Tony....you are kind.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com
    ·
Sign In or Register to comment.