Problem using Else


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1

    Default Problem using Else

    I am using the latest version of Picbasic Pro with Micro Studio Plus, if I write something like:

    IF Portb.0=0 THEN Portb.1=1
    ELSE
    Portb.1=0
    ENDIF

    Or
    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF

    ERROR Line 49: ELSE without a matching IF..THEN. (TEST(mod1).pbp)

    Just about anything I try using else doesn't work, Any suggestions? Thanks again

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF


    should be like

    IF i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3


    Did you find this post helpful? Yes | No

    Default

    IT WORKS!!!!!!!!!!!!!

    Thank you very much

  4. #4


    Did you find this post helpful? Yes | No

    Default

    can I put a pause in for debounce?
    IF i=%00000001 THEN
    HIGH 1
    pause 50
    ELSE
    LOW 1
    ENDIF

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    can I put a pause in for debounce?
    IF i=%00000001 THEN
    HIGH 1
    pause 50
    ELSE
    LOW 1
    ENDIF
    debounce?

    Code:
    IF i=%00000001 THEN
    
     WHILE i=%00000001
      PAUSE 50 
     WEND
    
     HIGH 1
    
    ELSE
    
     LOW 1
    
    ENDIF
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6


    Did you find this post helpful? Yes | No

    Default

    I have tried the:
    IF i=%00000001 THEN

    WHILE i=%00000001
    PAUSE 50
    WEND

    HIGH 1

    ELSE

    LOW 1

    ENDIF

    using this on four outputs my program does nothing with these four. If I take out the while statement and use the :
    If i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    then my outputs for these four comes on and stays on, forever.
    I been experimenting with SELECT CASE and what I am trying to accomplish is if I am pressing the button on the transmitter, the output selected comes on and stays on until I release the button instead of cycling on/off. So here is my latest rx version which does nothing, what am I doing wrong?

    INCLUDE "bs2defs.bas"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF
    DEFINE OSC 4
    mydata1 VAR byte
    mydata2 var byte
    address1 var byte
    address2 var byte
    checksum var byte
    chk_sum var byte
    serpin VAR porta.1
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %10000000
    CMCON=%00000111
    PAUSE 50

    loop:
    gosub loop1
    CheckSum = (address1 + address2)
    CheckSum = CheckSum + (mydata1 + mydata2)
    IF checksum != chk_sum THEN loop
    IF (mydata1) != (mydata2) THEN loop
    IF (address1) != (address2) THEN loop

    select case mydata1
    case 0
    mydata1=%01010110
    toggle 0
    pause 50

    case 1
    mydata1=%01011001
    high 1
    pause 50
    case else
    low 1

    case 2
    mydata1=%01011010
    high 2
    pause 50
    case else
    low 2

    case 3
    mydata1=%01100101
    high 3
    pause 50
    case else
    low 3

    case 4
    mydata1=%01100110
    high 4
    pause 50
    case else
    low 4

    case 5
    mydata1=%01101001
    toggle 5
    pause 50

    case 6
    mydata1=%01101010
    toggle 6
    pause 50
    end select
    goto loop

    loop1:
    SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
    Return

    One more thing when I copy and paste my code to this forum everything becomes lower case. I have been reminded as a courtesy of DEFINE OSC 4 just wanted to let ya'll know
    why everything appears in the lower case. Many Thanks

  7. #7
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF


    should be like

    IF i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

    IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
    (And I don't remember if the Endif is required in this situation or not.)

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tenaja View Post
    If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

    IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
    (And I don't remember if the Endif is required in this situation or not.)
    I'm not sure that statement is 100% valid, and I'm all about using colons! I'll have to try it out later today.
    Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.

  9. #9


    Did you find this post helpful? Yes | No

    Default Colons & If

    The manual says:

    W2 = W0
    W0 = W1
    W1 = W2
    is the same as:
    W2 = W0 : W0 = W1 : W1 = W2

    So why wont this compile:
    IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    ERROR Line 48: Bad expression. (Test(test mod2).pbp)

    I've even tried it as this :
    IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop

    Still no go
    I just started to try to use multi-line statements as suggested, sure would look better
    Last edited by tazntex; - 24th July 2008 at 19:23. Reason: Typing error

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    The manual says:
    W2 = W0
    W0 = W1
    W1 = W2
    is the same as:
    W2 = W0 : W0 = W1 : W1 = W2
    Yes, that is completely valid..

    So why wont this compile:
    IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    ERROR Line 48: Bad expression. (Test(test mod2).pbp)

    I've even tried it as this :
    IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    Just like I said in my last post, I'm not 100% sure that an else in the IF/THEN is valid, and apparently it's not. So you still have to split up the If/Then/Else/Endif statements.

    This is valid:
    Code:
    If mydata = x then
        high 7
    else
        low 7
    endif
    pause 50:goto loop
    You're examples above are not valid.
    These don't work either:
    Code:
    IF mydata = x then pause 50 : GOSUB something
    
    If mydata = x then for x = 1 to 10 : pause 1 : next x
    These work:
    Code:
    if mydata = x then
     pause 50 : gosub something
    endif
    
    if mydata = x then
     for x = 1 to 10 : pause 1 : next x
    endif
    Just a few examples of what works and what doesn't when using colons...and I know colons!

  11. #11
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.
    I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?

    I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).

    Example

    If A=0 then Goto Start else A=A+1:Gosub DoThis

    This is equivalent to...

    If A=0 then
    Goto Start
    else
    A=A+1
    Gosub DoThis
    Endif

    And NOT as may be expected...

    If A=0 then
    Goto Start
    else
    A=A+1
    endif
    Gosub DoThis

    There is a distinct difference (and some tricky workings) if you bung all your code on one line separated with colons. It's not really the way it should be written. There are rules for IF/THEN/ELSE, WHILE/WEND, FOR/NEXT etc.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Joe, here is the code I am sending from pic to pic:

    INCLUDE "MODEDEFS.BAS"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    ' Watchdog Timer
    @ DEVICE pic16F628a, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628a, MCLR_ON
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628a, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628a, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628a, CPD_OFF
    ' Data Memory Code Protect
    ' Set to CPD_OFF for Development Copy
    ' Set to CPD_ON for Release Copy
    @ DEVICE pic16F628a, PROTECT_OFF
    define osc 4
    CMCON=%00000111
    trisb = %11111111
    trisa = %00010111
    chk_sum var byte
    dgood var porta.2
    serpin var porta.3
    nokey con %11111111
    synch con 254
    address con %00000001
    keyin var portb
    keydata var byte
    E VAR PORTB.0
    D VAR PORTB.1
    G VAR PORTB.2
    A VAR PORTB.3
    B VAR PORTB.4
    C VAR PORTB.5
    F VAR PORTB.6
    na VAR PORTB.7
    PreAmble CON $A5 ' 10100101
    findkey:
    if A = 0 then sw0
    if B = 0 then sw1
    if C = 0 then sw2
    if D = 0 then sw3
    if E = 0 then sw4
    if F = 0 then sw5
    if G = 0 then sw6

    goto findkey
    sw0:
    keydata = %01010110
    goto dout
    sw1:
    keydata = %01011001
    goto dout
    sw2:
    keydata = %01011010
    goto dout
    sw3:
    keydata = %01100101
    goto dout
    sw4:
    keydata = %01100110
    goto dout
    sw5:
    keydata = %01101001
    goto dout
    sw6:
    keydata = %01101010
    goto dout

    dout:
    low dgood
    pause 100
    chk_sum = (address + address)
    chk_sum = chk_sum + (keydata + keydata)
    goto transmit

    transmit:

    serout serpin,N9600[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    if keyin = nokey then x2
    goto findkey

    x2:
    high dgood
    keydata = %00000000
    serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    goto findkey
    end

    Anyhow, it works, tx side seems to be fine, my big problem is on the rx side each output that is a if/then statement will come on but when I release the button it stays on. The only way to make it turn off is to press another momentary button. The toggle function works fine, although if I hold onto the button to long it will toggle on/off. if/then example I have tried after Skimask post:

    if mydata1=%01011001 then pause 50
    if mydata1=%01011001 then
    high 1
    else
    low 1
    endif

    I am using a direct cable link, and will try again later with the Linx modules.

    "logic dictates, cmcon goes before anything portA, then tris registers dictate input or output, then port dictates hi or low status."
    And I have modified the suggestion to this tx program yet, but I will later this evening.

    Many thanks for the help.
    Last edited by tazntex; - 7th August 2008 at 04:41.

  13. #13
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?
    I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).
    I don't have any specific examples off the top of my head, although I've never actually used an If/Then/Else on one continuous line. Thanks for the idea
    And yes, you are right about the colons. I try to stuff as much into a single line as I can, just so I don't have to scroll up and down so much. Therefore I tend to run into various issues with the aforementioned statements. I just know what works, and when something doesn't, I have a rough idea of where to look My current 'project' is about 4000 lines of my 'colonized' code. I split it up one day and came up with something around 19,000 lines. Sure, it's not easy to read, but I wrote it, I know what's where, and for the most part, others don't have to read it (and probably couldn't anyways )
    And you're right, it's probably not so much the way that code should be written, but there's nothing that says it CAN'T be written in such a fashion...Unless of course it doesn't work right.

Similar Threads

  1. problem using GOSUB under interrupt
    By fobya71 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 5th March 2010, 19:52
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 21:04
  4. Hardware problem or what ?
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th March 2007, 21:39
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 0

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