IF ....THEN.... problems


Closed Thread
Results 1 to 17 of 17
  1. #1
    Join Date
    Feb 2011
    Posts
    9

    Smile IF ....THEN.... problems

    Hi, I`ve got a simple problem I can`t solve. Maybe someone can help me .
    There is a part of code:

    begin:
    pause 100
    IF timer = 1 AND relay = 1 THEN
    pause 500
    timer = 0 'turn off timer'
    PAUSE 1000
    portb.1 = 0
    timer = 1 'turn on timer'

    IF timer = 1 AND relay = 1 THEN

    pause 500
    timer = 0 'turn off timer'
    pause 1000
    portb.1 = 1
    pause 1000
    timer = 1 'turn on timer'
    ENDIF
    ENDIF
    .
    .
    .
    The problem is with second IF....THEN...., whole cycle goes without examining the second IF ...AND...THEN. But I need following:

    When first IF...AND ....THEN "show", go to commands after THEN. After that, examine second(the same as first) IF...AND...THEN and go to commands after its THEN. How can I fix this to do what I want?
    Thanks in advance .

  2. #2
    Join Date
    May 2010
    Location
    Chile
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    You've got a redundancy in your code (maybe on purpose),

    First you activate the timer = 1 flag, and in the next line of code you put an " IF timer = 1..."

    Also the second IF THEN ELSE is inside the first IF THEN, try separating them.

    Try using indent in your coding to see in a better way your routine operation

    this is your code...commented
    Code:
    begin:
    pause 100
    IF timer = 1 AND relay = 1 THEN
        pause 500
        timer = 0 'turn off timer'
        PAUSE 1000
        portb.1 = 0
        timer = 1 'turn on timer'               '--turned timer = 1 
    
        IF timer = 1 AND relay = 1 THEN   '--inside first loop? move it out.  Timer already turned 1 on previous line, so is always true for that part
            pause 500
            timer = 0 'turn off timer'
            pause 1000
            portb.1 = 1
            pause 1000
            timer = 1 'turn on timer'
        ENDIF
    ENDIF
    Last edited by El_AMPo; - 21st February 2011 at 15:31.
    "If at first doesn't work, kicking it wont help either"

  3. #3
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Lightbulb Re: IF ....THEN.... problems

    Ok, first of all thanks .

    This was part of code.... there is more. First is BUTTON command which activate timer. Then comes the rest of code.... then timer = 0 and again just turn the timer on without pressing button. Here is complete code with button command and corrections:

    TRISA = $FF
    TRISB = $00
    B0 var byte
    symbol t1 = porta.1
    symbol timer = portb.0
    symbol relay = porta.0


    begin:
    pause 100
    IF timer = 1 AND relay = 1 THEN
    pause 500
    timer = 0 'turn off timer'
    PAUSE 1000
    portb.1 = 0
    timer = 1 'turn on timer'
    ENDIF

    IF timer = 1 AND relay = 1 THEN

    pause 500
    timer = 0 'turn off timer'
    pause 1000
    portb.1 = 1
    pause 1000
    timer = 1 'turn on timer'
    ENDIF

    B0 = 0
    button t1,0,255,0,B0,1,step 'if button is pressed, go to step'
    pause 50
    goto begin

    step:
    timer = 1

    goto begin

    End


    This still do the same. Any help now?

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    try coding like this:
    Code:
    IF (timer = 1) and (relay = 1) then
    I am not sure if this will work, it seems maybe relay never = 1?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Unhappy Re: IF ....THEN.... problems

    yes relay will be =1 . I will explain. The timer is hardware piece with ne 555 and it has a relay on board. The portb.0 turning on timer(with transistor), and timer relay is on(relay = 0) for some time depending of potentiometer position. After timer turn off(ne555 ), the portb.0 is still on, but timer is not on becouse ne555 turn it off.The relay is off but it is connected to a porta.1 like button. So if relay is off, the pin porta.1 is on high (1) (pull up resistor). That is why the relay = 1 (it should be off and portb.0 is on, but timer is off).
    I hope you have understand me .

    All I need is to do this, example:
    begin:
    IF step1 AND step2 THEN
    step3

    IF step1 AND step2 THEN
    step4

    IF step1 AND step2 THEN
    step5

    goto begin
    end



    with this order.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    Dumb question, what pic are you using and have you set PORTA.0 and 1 to digital?

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    so can you do this:
    Code:
    IF (step 1) and (step 2) then : ' notice the : it lets you have multiple steps
     step 3
     step 4
     step 5
    endif
    did you try it with () around the conditions?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Smile Re: IF ....THEN.... problems

    I am using 16F84, all porta have been declarated as inputs( it is like pushbuttons) It works on the begining.. Yes I`ve tried this with ( ) and there is no difference still not working as I want. Maybe this can be done with while....wend or for.... cycle....just don`t know how.

  9. #9
    Join Date
    May 2010
    Location
    Chile
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    In 16F84 MCLR pin can't be disabled, so 4k7 resistor pullup to +5V or its never going to work
    Also for the used inputs use 10K pullups and instead of using the button command, try something a lot simpler:

    Code:
    b0 = 0 'reset flag
    if ti = 0 then 'if pullup pin is in ground
        b0 = 1 'activate button press flag
        pause 250 'debounce
    endif
    Try explaining a little better your circuit so i can help you with your code (maybe an schematic or diagram?)

    Also remeber that a 16F84 its not the same as a 16F84A for the compiler, check for that.
    "If at first doesn't work, kicking it wont help either"

  10. #10
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    Thanks for your quick response. I wroted a scheme, but it is hand drawing for now, I hope it will be ok.
    In the mean time, I found a way to get this work, but there is another problem again.
    This is code:

    TRISA = $FF 'Postavi sve pinove porta A kao ulazne
    TRISB = $00 'Postavi sve pinove porta B kao izlazne
    B0 var byte
    i var byte
    symbol t1 = porta.1
    symbol tajmer = portb.0
    symbol rele = porta.0


    begin:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1
    goto petlja
    petlja:
    i = 0
    for i = 1 to 2
    If i = 1 then
    pause 300
    goto petlja1
    endif

    if i = 2 then
    pause 300
    goto petlja2
    endif

    next i

    petlja1:
    PAUSE 500
    tajmer = 0 'iskljuci tajmer'
    portb.1 = 0 'ukljuci crvenu diodu veliku'
    pause 3000 'pauza 3 sekunde'

    petlja2:
    tajmer = 1
    portb.2 = 0 'ukljuci diodu zelenu'
    portb.1 = 1 'iskljuci crvenu diodu'
    pause 200

    wend 'vrlo bitna pauza'

    IF tajmer = 1 AND rele = 1
    portb.3 = 0
    portb.1 = 1
    portb.2 = 1
    ENDIF

    B0 = 0
    button t1,0,255,0,B0,1,ukljucenje 'ako je taster pritisnut idi na ukljucenje'

    goto begin

    ukljucenje:
    tajmer = 1


    goto begin

    End

    The commented is in Serbian so don`t look at it .

    The code do following: after press a pushbutton t1, tajmer is on and it works until it turn off, but only a timer. Its transistor is still on and have to be off and again on to reset. Now transistor for timer is off, some leds are on, pause 3 seconds, then on again transistor and timer, after a while timer goes off, and continuo for the begining again.
    The problem now is how to continue to cycle after wend and not to go from begin what this program do ? Becouse program after wend go from the begining again, not considering this IF...AND....Then after wend. I need this, and maybe more examining loops before going to begin again. Maybe this FOR cycle is not good?
    Kind regards.
    Attached Images Attached Images  

  11. #11
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    Your IF..Then's are causing you to jump out of the for...next. If you change the GOTO to GOSUB, then add a RETURN after the blocks of code you are jumping to, maybe this will fix the problem.

    I must admit I don't really get the goal of your program, but I think you don't intend to jump out maybe?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  12. #12
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    This with gosub works to, but the exactly same thing happens. I tried to put IF ...then condition in body of gosub to examin again but program just not "see" that and go again from the begin.
    Here is a new code:

    begin:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1
    goto petlja
    petlja:
    PAUSE 500
    tajmer = 0 'iskljuci tajmer'
    portb.1 = 0 'ukljuci crvenu diodu veliku'
    pause 3000
    gosub petlja1 'iskljuci crvenu diodu


    petlja1:
    tajmer = 1 'ukljuci tajmer'
    portb.1 = 1 'iskljuci crvenu led'
    portb.2 = 0 'ukljuci zelenu led'
    return

    wend 'vrlo bitna pauza'

    B0 = 0
    button t1,0,255,0,B0,1,ukljucenje 'ako je taster pritisnut idi na ukljucenje'
    goto begin




    ukljucenje:
    tajmer = 1


    goto begin

    End

    The reason for using hardware timer is becouse I don`t know how to make it with microcontroller and potentiometer, maybe that will makes this code easier. In all this code very important information is when timer stops counting(countdown) which is enabled by relay which is similiar to pushbutton on porta.0.

  13. #13
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    OK, Maybe I see the problem. AND MAYBE I can explain it. Here goes. This is your program from a few posts back with some added comments:
    Code:
    TRISA = $FF 'Postavi sve pinove porta A kao ulazne 
    TRISB = $00 'Postavi sve pinove porta B kao izlazne 
    B0 var byte
    i var byte
    symbol t1 = porta.1
    symbol tajmer = portb.0
    symbol rele = porta.0
    
    
    begin:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1 ' when both are 1, we do this while loop
    goto petlja 
    petlja:
    i = 0
    for i = 1 to 2
    If i = 1 then
    pause 300
    goto petlja1'     EVERY time timer and relay are 1, we go to petlja1
    endif
    
    if i = 2 then
    pause 300
    goto petlja2 
    endif
    
    next i 
    
    petlja1: '                        We are here because timer and relay are = 1
    PAUSE 500
    tajmer = 0 'iskljuci tajmer' '          now timer = 0?
    portRETb.1 = 0 'ukljuci crvenu diodu veliku'
    pause 3000 'pauza 3 sekunde'
    RETURN ' I think you need to add this here
    
    petlja2: '                         We are here because we fall in here from petlja1
    tajmer = 1 '                     now timer = 1 again
    portb.2 = 0 'ukljuci diodu zelenu'
    portb.1 = 1 'iskljuci crvenu diodu'
    pause 200
    RETURN'    and here
    wend 'vrlo bitna pauza'           ' go back and check while again. right now timer = 1, relay = ??
    '                                            If relay also = 1, you will never get out of the loop.
    
    IF tajmer = 1 AND rele = 1 ' I think you will never get to this check, stuck in the while loop.
    portb.3 = 0
    portb.1 = 1
    portb.2 = 1
    ENDIF
    
    B0 = 0
    button t1,0,255,0,B0,1,ukljucenje 'ako je taster pritisnut idi na ukljucenje'
    
    goto begin
    
    ukljucenje:
    tajmer = 1
    
    
    goto begin
    
    End
    If you change the RED goto to gosub,add the returns, and move the petlja1 and petlja2 routines to the end of the program. It may act as you expect.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  14. #14
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    This with gosub, return.... not work well. The same again. What about putting in While loop another loop who checking the same condition and if it is not, go to this loop again until real state shows, and then go to another operation. All this inside this begin while....wend loop.
    I tried to put petlja1 and petlja2, after wend... and also after buttons goto begin, and after second timer on, it goes off and on immediately, and again from the begining.

    I`ll try another couple of combinations, and let you know tommorow about it.

    Thanks for helping me .

    Best regards.

  15. #15
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Wink Re: IF ....THEN.... problems

    In this changed code you wrote, the sentence in line with wend, you said: 'go back and check while again. right now timer = 1 and rele = ? 'if releay also = 1, you will never get out of the loop'.

    Yes, you are right. The rele is = 1 but after some of time depends of ne 555 chip and potentiometer, otherwise it is = 0 when timer working. When relay is = 1 timer is still ON! but not working until turn the transistor on and off again (+12V for timer). That is big problem, I have to go out from that loop and do testing again in while, and then go back into previous loop. But without colision of theese two loops.

  16. #16
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: IF ....THEN.... problems

    Quote Originally Posted by bursach View Post
    'go back and check while again. right now timer = 1 and rele = ? 'if releay also = 1, you will never get out of the loop'.
    I am sorry for not being clear. I did not mean for you to go and check, I ment this is what the program is doing. and the conditions when it gets there.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  17. #17
    Join Date
    Feb 2011
    Posts
    9


    Did you find this post helpful? Yes | No

    Unhappy Re: IF ....THEN.... problems

    Ok, I understand.
    Check this new code. There is very strange things happening in pic work. THis code was compiled and after pushing button and timer iz on, after relay is 1, the portb.2 is on?! and then nothing happens. Second compiling the same code in chip, results different cycle. I was wondering if something is wrong with power or chip ? What is difference betwen 16F84A and 16F628, can I use 628 with same code but compile with this type? Maybe that is the problem.

    TRISA = $FF 'Postavi sve pinove porta A kao ulazne
    TRISB = $00 'Postavi sve pinove porta B kao izlazne
    B0 var byte
    i var byte
    symbol t1 = porta.1
    symbol tajmer = portb.0
    symbol rele = porta.0

    begin:

    gosub petlja
    pause 1000
    gosub petlja1
    pause 1000
    gosub petlja2
    goto begin

    petlja:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1
    PAUSE 500
    tajmer = 0 'iskljuci tajmer'
    portb.1 = 0 'ukljuci crvenu diodu veliku'
    pause 3000
    tajmer = 1

    return
    wend
    petlja1:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1
    PAUSE 100
    tajmer = 0 'iskljuci tajmer'
    portb.1 = 1 'ukljuci crvenu diodu veliku'
    pause 3000
    portb.2 = 0
    tajmer = 1
    return
    wend 'vrlo bitna pauza'
    petlja2:
    pause 100 'vrlo bitna pauza'
    while tajmer = 1 and rele = 1
    PAUSE 100
    tajmer = 0 'iskljuci tajmer'
    portb.1 = 1 'ukljuci crvenu diodu veliku'
    pause 3000
    portb.2 = 1
    portb.3 = 0
    return
    wend

    B0 = 0
    button t1,0,255,0,B0,1,ukljucenje 'ako je taster pritisnut idi na ukljucenje'
    goto begin

    ukljucenje:
    tajmer = 1


    goto begin

    End

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