If endif loop with 'OR'


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Jan 2013
    Posts
    64

    Default If endif loop with 'OR'

    Hi,
    Can someone let me know how to do the following please? I'm using PIC BASIC and programming an 18F2431 PIC.

    I am trying to move an arm around a 360 circle. For example if it is at 1degree and it needs to move to 2 degrees, then it should move cwise 1 degree. I want it always to take the shortest route. Example if the arm is at 1 degree and needs to move to 359 then how do I make it go ccwise? Because the motor moves if depending on whether ax is > or < than bx, but in this case 359 is > than 0 so it will go right round. Here is an example something like how I think it will work, but there isn't an OR in PICBASIC for this purpose.

    If ax < bx Or
    If bx - ax >= 180 Then
    Gosub cwise
    Endif
    Endif

    Help will be appreciated,

    Camerart.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    If ( ax < bx) Or (bx - ax >= 180) Then

    Gosub cwise
    Endif
    this will work if your math is correct

  3. #3
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Hi Richard,

    Thanks for your reply.

    I forgot to say that I am using Oshonsoft.

    See attached result image.

    C
    Attached Images Attached Images  

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    this compiles just fine from mcs



    Code:
    poscnt var word
    azimval var word
    time var byte
    
    main:
    if (poscnt<azimval) or (azimval-poscnt >= 1800) then
    gosub cwise
    time=0
    endif
    goto main
    end
    cwise:
    return
    take up the issue with Oshonsoft.

  5. #5
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Hi Richard,

    There are a few glitches in Oshonsoft, and queries are usually not answered, so I just work with what's there. I tried a few combinations of OR IF THEN ELSE ETC, but no compile.

    If I say the answer to my question is:
    If poscnt < azimval Then
    If azimval - poscnt >= 180 Then
    Gosub ccwise
    counter = 0
    Else
    Gosub cwise
    counter = 0
    Endif
    Endif
    You may notice that I had asked the wrong question I have more difficulty than I should have with lines like: (If azimval - poscnt >= 180 Then) so things take 50 times as long as they should. Anyway It's working now.

    Thanks and HAPPY NEW YEAR.

    C.

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    There’s nothing wrong with what you’re doing with the IF/THEN statements
    so long as you follow it, the compiled code is similar, and possiblly even worse.

    Code:
    if (battery=charged) && (lightbulb != blown) then ‘ AND
    bulb will light
    endif
    
    if (battery = flat) || (lightbulb = blown) then ‘ OR
    bulb will not light
    endif

  7. #7
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Hi Art,

    As I've been monolingual with basic since the early 80s, your reply is another puzzle, but thanks. I'll stick to IF/THEN.

    C.

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Hi,

    Code:
    if (poscnt<azimval) or (azimval-poscnt >= 1800) then
    I just would had written ...

    Code:
    if (poscnt<azimval) or ((azimval-poscnt) >= 1800) then
    to be sure of what happens ... ( might be overkill ... but ... )

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    That's a good point. What does happen first, the comparison or the subtraction, in the first snippet. In the second you'd be sure the subtraction was done prior to the comparison. I'm sure I'll get schooled here but with code there is often no such thing as overkilled overkill.

  10. #10
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Hi,

    In my case the subtraction happens first.

    C.

  11. #11
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    At least for C languages this, according to wikipedia, is the order of operations. I tried to find something more specific to PBP or the BASIC stamp but couldn't. This topic may be getting off topic but I thought it interesting to know at least a pseudo answer.


    1 () [] -> . :: Function call, scope, array/member access
    2 ! ~ - + * & sizeof type cast ++ -- (most) unary operators, sizeof and type casts (right to left)
    3 * / % MOD Multiplication, division, modulo
    4 + - Addition and subtraction
    5 << >> Bitwise shift left and right
    6 < <= > >= Comparisons: less-than, ...
    7 == != Comparisons: equal and not equal
    8 & Bitwise AND
    9 ^ Bitwise exclusive OR (XOR)
    10 | Bitwise inclusive (normal) OR
    11 && Logical AND
    12 || Logical OR
    13 ? : Conditional expression (ternary)
    14 = += -= *= /= %= &= |= ^= <<= >>= Assignment operators (right to left)
    15 , Comma operator

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Precedence from the pbp3 manual

    The following table lists the operators in default hierarchal order. (Parentheses will
    override.) Operators with the same precedence level (on the same line in the table
    below) will be evaluated in the order (left to right) that they are encountered in the
    written expression.
    Highest Precedence
    ( ) (anything enclosed in parentheses)
    - (unary)
    ! or NOT, ABS, COS, DCD, DIV32, NCD, SIN, SQR
    <<, >>, ATN, DIG, HYP, MAX, MIN, REV
    *, /, **, */, //
    +, - (in math), ~
    = or ==, <> or !=, <, <=, >, >=
    &, |, ^, &/, |/, ^/
    AND, OR, XOR, ANDNOT, ORNOT, XORNOT
    Lowest Precedence

    camerart's problem is that he is using Oshonsoft's version of picbasic not pbppro

    picbasic != pbppro

  13. #13
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Ah, what I posted should compile in C or PBP, and in both languages inner brackets should evaluate first, and in order toward outer brackets.

    PBP
    Code:
    if (battery=charged) && (lightbulb != blown) then ‘ AND
    bulb will light
    endif
    C
    Code:
    if (battery=charged) && (lightbulb != blown) { ‘ AND
    bulb will light
    }




    Quote Originally Posted by camerart View Post
    Hi Art,

    As I've been monolingual with basic since the early 80s, your reply is another puzzle, but thanks. I'll stick to IF/THEN.

    C.

  14. #14
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Ah, what I posted should compile in C or PBP, and in both languages inner brackets should evaluate first, and in order toward outer brackets.

    C

    if (battery=charged) && (lightbulb != blown) { ‘ AND bulb will light }
    not really in C (battery=charged) is not a comparison it's an assignment ,while the statement will compile in C the results will be incorrect

    (battery==charged) is a comparison statement in C

  15. #15
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: If endif loop with 'OR'

    Whoops .

Similar Threads

  1. Exit from an IF/ENDIF loop with a GOTO
    By aratti in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 17th February 2014, 13:05
  2. Replies: 2
    Last Post: - 28th July 2011, 16:23
  3. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23
  4. Problems with IF ENDIF
    By jetpr in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd November 2005, 16:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts