Menu

command exit for, exit sub

Help
JANIS
2016-11-08
2016-11-09
  • JANIS

    JANIS - 2016-11-08

    Hallo all!
    I have a program with a PWM generator for RGB Led. PWM generators implemented using function- for. I need to pass the PWM generator subroutine and jump to another subroutine, if there is a short signal (~ 0.5 sec) from the input. At the bottom of the visible example of do not want to work. The signal from the input must be greater than 0.5 sec, and sometimes does not work. I wrote a small program.

    Sub RGB

    for cnt1 = 0 to 250 Step 5
    speed = 6
    call PWM
      brightVB = cnt1
      brightKB = cnt1
      brightLB = cnt1

    brightVG = 250-cnt1
    brightKG = 250-cnt1
    brightLG = 250-cnt1

    brightVR = 250-cnt1
    brightKR = 250-cnt1
    brightLR = 250-cnt1
                                                IF porta.1 = 1 then exit for

    Next cnt2
    '================
    for cnt2 = 0 to 250 Step 5
    speed = 6
    call PWM

    brightVR = cnt2
       brightKR = cnt2
       brightLR = cnt2
                                            IF porta.1 = 1 then exit for
      Next cnt1

    end Sub

    Really "for cnt xx = 0 to 250 Step 5" parts are many.

     
  • JANIS

    JANIS - 2016-11-08

    yess, PWM is a sub

    sub pwm

    for cyc=0 to speed

    for re=0 to 255

    if re<brightVR then set V_R on
    if re=brightVR then set V_R off

    if re<brightKR then set K_R on
    if re=brightKR then set K_R off

    if re<brightLR then set L_R on
    if re=brightLR then set L_R off

    ''================================

    if re<brightVG then set V_G on
    if re=brightVG then set V_G off

    if re<brightKG then set K_G on
    if re=brightKG then set K_G off

    if re<brightLG then set L_G on
    if re=brightLG then set L_G off

    ''================================
    if re<brightVB then set V_B on
    if re=brightVB then set V_B off

    if re<brightKB then set K_B on
    if re=brightKB then set K_B off

    if re<brightLB then set L_B on
    if re=brightLB then set L_B off

    next re
    next cyc
    end sub

     
  • JANIS

    JANIS - 2016-11-08

    ok, i later will write all code.

     
  • JANIS

    JANIS - 2016-11-08

    Please.program code in a simplified form:

    chip 16f88,8
    config osc=int

    inittimer1 osc, ps1_8
    on interrupt timer1overflow call timer
    stoptimer 1
    cleartimer 1
    define V_R PORTa.2
    define V_G PORTa.4
    define V_B PORTA.3
    define K_R PORTb.0
    define K_G PORTb.2
    define K_B PORTb.1
    define L_R PORTb.3
    define L_G PORTb.5
    define L_B PORTb.4
    dir PORTA.2 out
    dir PORTA.3 out
    dir PORTA.4 out
    dir PORTb.0 out
    dir PORTb.1 out
    dir PORTb.2 out

    dir PORTb.3 out
    dir PORTb.4 out
    dir PORTb.5 out
    define mic PORTA.1
    define button PORTA.6
    dir PORTA.1 in
    dir PORTA.6 in
    re=0
    brightVR=0
    brightKR=0
    brightLR=0

    brightVB=0
    brightKB=0
    brightLB=0

    brightVG=0
    brightKG=0
    brightLG=0
    fast=0
    full=0
    sec=0
    min=0

    main:
    if mic=1 then fast=1

    if mic Then
    fast=1
    full=0
    sec=0
    min=0
    end if

    if fast=1 then
    starttimer 1
    end if

    if fast=1 & sec=>1 & sec<=6 then activity
    if fast=1 & sec=>6 & sec<15 then RGB

    if sec=>15 Then
    stoptimer 1
    cleartimer 1
    standby
    fast=0
    sec=0
    full=0
    end if
    goto main

    sub activity
    set V_R on
    set K_B on
    end sub

    sub standby
    set V_R off
    set K_B off
    end sub

    sub RGB
    for cnt1= 0 to 250 step 5
    speed=6
    call pwm
    brightVB=cnt1
    brightKB=cnt1
    brightLB=cnt1
    brightVG=0
    brightKG=0
    brightLG=0
    brightVR=250-cnt1
    brightKR=250-cnt1
    brightLR=250-cnt1
    ??? if mic then exit for??????
    next cnt2
    ''================
    for cnt2= 0 to 250 step 5
    speed=6
    call pwm
    brightVR=cnt2
    brightKR=cnt2
    brightLR=cnt2
    ??? if mic then exit for??????
    next cnt1
    end sub

    sub pwm
    for cyc=0 to speed

    for re=0 to 255

    if re<brightVR then set V_R on
    if re=brightVR then set V_R off

    if re<brightKR then set K_R on
    if re=brightKR then set K_R off

    if re<brightLR then set L_R on
    if re=brightLR then set L_R off

    ''================================

    if re<brightVG then set V_G on
    if re=brightVG then set V_G off

    if re<brightKG then set K_G on
    if re=brightKG then set K_G off

    if re<brightLG then set L_G on
    if re=brightLG then set L_G off

    ''================================
    if re<brightVB then set V_B on
    if re=brightVB then set V_B off

    if re<brightKB then set K_B on
    if re=brightKB then set K_B off

    if re<brightLB then set L_B on
    if re=brightLB then set L_B off

    next re
    next cyc
    end sub

    sub timer
    full++
    if full=4 then
    sec++
    full=0
    end if
    if sec>59 then
    min++
    sec=0
    end if
    if min>59 then
    min=0
    end if
    end sub

    In essence, when the mic = 1,need call sub active. mic = 1 ~ 0,5sec. but the problem is that in this way, as noted by question marks, does not jump out of the sub RGB. :(((

     
  • Anobium

    Anobium - 2016-11-08

    This does not compile. Too many errors. Can you post as an attachment?

     
    • Anobium

      Anobium - 2016-11-08

      And, please indent your code. Give us a chance to understand the structure of the code from the logical indents.

      Also, some comments may help our understanding of the issue.

       
  • Anobium

    Anobium - 2016-11-08

    I just laid this out - so, I can read it. I see errors. These need correcting - see bold. There may be more.

    So, what are you trying to to do? Are you wanting timer1 to overflow as some specific time period? There may be easier ways to do this. I will let others advise on the best method.

    Anobium

      #chip 16f88,8
      '#option Explicit
      #config osc=int
      inittimer1 osc, ps1_8
      on interrupt timer1overflow call timer
      stoptimer 1
      cleartimer 1
      #define V_R PORTa.2
      #define V_G PORTa.4
      #define V_B PORTA.3
      #define K_R PORTb.0
      #define K_G PORTb.2
      #define K_B PORTb.1
      #define L_R PORTb.3
      #define L_G PORTb.5
      #define L_B PORTb.4
      dir PORTA.2 out
      dir PORTA.3 out
      dir PORTA.4 out
      dir PORTb.0 out
      dir PORTb.1 out
      dir PORTb.2 out
      dir PORTb.3 out
      dir PORTb.4 out
      dir PORTb.5 out
      #define mic PORTA.1
      #define button PORTA.6
      dir PORTA.1 in
      dir PORTA.6 in
    
    
      re=0
      brightVR=0
      brightKR=0
      brightLR=0
      brightVB=0
      brightKB=0
      brightLB=0
      brightVG=0
      brightKG=0
      brightLG=0
      fast=0
      full=0
      sec=0
      min=0
    
      main:
              if mic=1 then fast=1
       **       if mic Then**
                  fast=1
                  full=0
                  sec=0
                  min=0
              end if
              if fast=1 then
                  starttimer 1
              end if
              if fast=1 & sec=>1 & sec<=6 then activity
              if fast=1 & sec**=>**6 & sec<15 then RGB
              if sec**=>**15 Then
                  stoptimer 1
                  cleartimer 1
                  standby
                  fast=0
                  sec=0
                  full=0
              end if
      goto main
    
    
      sub activity
          set V_R on
          set K_B on
      end sub
      sub standby
          set V_R off
          set K_B off
      end sub
    
      sub RGB
          for cnt1= 0 to 250 step 5
              speed=6
    
              call pwm
    
              brightVB=cnt1
              brightKB=cnt1
              brightLB=cnt1
              brightVG=0
              brightKG=0
              brightLG=0
              brightVR=250-cnt1
              brightKR=250-cnt1
              brightLR=250-cnt1
      '        ??? if mic then exit for??????
          next cnt2
          ''================
          for cnt2= 0 to 250 step 5
              speed=6
    
              call pwm
    
              brightVR=cnt2
              brightKR=cnt2
              brightLR=cnt2
      '              ??? if mic then exit for??????
    
          next cnt1
      end sub
    
    
      sub pwm
          for cyc=0 to speed
                for re=0 to 255
                    if re<brightVR then set V_R on
                    if re=brightVR then set V_R off
                    if re<brightKR then set K_R on
                    if re=brightKR then set K_R off
                    if re<brightLR then set L_R on
                    if re=brightLR then set L_R off
                    ''================================
                    if re<brightVG then set V_G on
                    if re=brightVG then set V_G off
                    if re<brightKG then set K_G on
                    if re=brightKG then set K_G off
                    if re<brightLG then set L_G on
                    if re=brightLG then set L_G off
                    ''================================
                    if re<brightVB then set V_B on
                    if re=brightVB then set V_B off
                    if re<brightKB then set K_B on
                    if re=brightKB then set K_B off
                    if re<brightLB then set L_B on
                    if re=brightLB then set L_B off
                next re
          next cyc
      end sub
          sub timer
            full++
            if full=4 then
            sec++
            full=0
            end if
            if sec>59 then
            min++
            sec=0
            end if
            if min>59 then
            min=0
            end if
    **  end sub**
    
     
  • JANIS

    JANIS - 2016-11-08

    I need at a time when the subroutine RGB is called and a impuls from the mic, then jump out of the subroutine RGB and jump on subrotine - activity. Here I need use the function exit for, but in those places where there are marked with a question mark, exit for -not work.

     
  • kent_twt4

    kent_twt4 - 2016-11-08

    Use equates when evaluating the variable, so "If mic = 1 Then", which is what Anobium could have been inferring?

     
    • Anobium

      Anobium - 2016-11-08

      ??? if mic then exit for?????? that was real logic. Oh. @kent that is a good spot. I would not have understood that - I thought it was a code that did not compile. My err.

       
  • JANIS

    JANIS - 2016-11-08

    So , at the time when the subroutine RGB is active , if there is the signal from the input mic, then need jump out of the subroutine RGB.

     
    • Anobium

      Anobium - 2016-11-08

      Yes.

                'port is high
                if mic = 1 then exit for
      
       
  • JANIS

    JANIS - 2016-11-08

    IF mic = 1 then fast = 1 is redundant, but it does not interfere

     
    • Anobium

      Anobium - 2016-11-08

      Trying to help. Can you simplify the code? The bare minimum. This is so I can understand what you trying to do (in the minumum code without all the vars etc). I truly do not understand what the outcome is.

      I understand you have an issue but I need to understand the logic.

       
  • JANIS

    JANIS - 2016-11-08

    Anobium

    Yes.
               'Port is high
               IF mic = 1 then exit for

    But it does not work !!!

     
    • Anobium

      Anobium - 2016-11-08

      Please... !!! = not happy. Really trying to help.

      In this example - when I press porta.1 then porta.2 will toggle only when cnt1 is not 250. This simple example works. And, porta.3 is high other times.

      '
        #chip 16f88,8
      
      
        #config osc=int
        inittimer1 osc, ps1_8
      
      
      
        on interrupt timer1overflow call timer
        stoptimer 1
        cleartimer 1
      
        dir porta.3 out
        dir porta.2 out
        dir porta.1 in
      
        do forever
      
          for cnt1 = 0 to 250 step 5
      
              if porta.1 = 1 then
      
                exit for
      
              end if
      
      
          Next
          if cnt1 <> 250 then
      
              porta.2 = !porta.2
      
          else
      
              porta.3 = !porta.3
          end if
      
        loop
      
        sub Timer
      
          timertt++
      
        end sub
      

      Is your port pull high (or low) - it is floating?

       
  • David Stephenson

    The signal on the "mic" pin needs to be of a sufficient stength and duration to reliably be detected. It might work better with the signal generating an interrupt.

     
  • JANIS

    JANIS - 2016-11-08

    exit for - in my case sometimes works only if the mic port is high more than 2 seconds.

     
  • JANIS

    JANIS - 2016-11-08

    But in my case, a mic port is high in only 0.5 sec

     
  • JANIS

    JANIS - 2016-11-08

    David
    "It might work better with the signal generating an interrupt."
    how to realize the program code, please help?

     
  • Anobium

    Anobium - 2016-11-08

    What a great idea. Do a search in the Demos folder of your installation for 'interrupts". There are many good examples.

     
  • JANIS

    JANIS - 2016-11-09

    Thanks!!!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.