Take this action when these numbers are reached. How?


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    88

    Default Take this action when these numbers are reached. How?

    Is there a way to select an action when a specific number is reached in a counter?

    For example, if I write:
    Code:
    counter = 0
    
    loophere:
     if counter = 21 then counter = 0
    
    counter = counter + 1
    
    pause 100
    
    goto loophere
    What I would like to do is along the lines of:

    if counter = 2 or 4 or 6 or 8 or 10 then
    led1 = 1
    else
    led1 = 0
    endif

    if counter = 12 or 14 or 16 or 18 or 20 then
    led2 = 1
    else
    led2 = 0
    endif

    I know you can write individual lines such as:

    if (counter = 2) OR (counter = 4) OR (counter = 6)...then

    or

    if counter = 2 then
    led1 = 1
    else
    led1 = 0
    endif

    if counter = 4 then
    led1 = 1
    else
    led1 = 0
    endif


    And so on, but that eats up too much space. Just looking for someone to point me in the right direction. It's late and I'm drawing a blank as to which method to use to save space, but still get it done.

    Thanks,
    Tony

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


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    select case counter

    case 2
    do stuff
    case 4
    do other stuff

    ....
    case else
    do something else

    end select

  3. #3
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    Unfortunately, this is part of a bigger piece of code and right now, I am jumping from a section of code via gosub and running through three gosub subroutines before heading back to the main code. The pic has to read it as the conversation would be spoken: if the counter equals 2 or 4 or 6 or 8 or 10 then turn LED1 on. If it's neither of these numbers, then turn it off.

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


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    select case counter

    case 2
    led1 = 1
    case 4
    led1 = 1
    case 6
    led1 = 1
    case 8
    led1 = 1
    case 10
    led1 = 1
    case else
    led1 = 0
    end select

    or
    if (counter = 2 )| (counter = 4) |,,,,, etc then
    led1 = 1
    else
    led1 = 0
    endif

  5. #5
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    if (counter = 2 )| (counter = 4) |,,,,, etc then
    led1 = 1
    else
    led1 = 0
    endif

    That's the simplest way huh? I guess being tired didn't matter. Thanks for the help.

    Tony

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    it depends on how high count goes up to and wether it starts at 0
    eq if the count was between 1 and 10 then all that's required is to check bit0 if it set then count is odd therefore led1 is off

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    Perhaps something like;
    Code:
    If Counter < 11 THEN
    
      If Counter // 2 = 0 THEN
        LED1 = 1
      ELSE
        LED0 = 0
      ENDIF
    
    ELSE
    
      IF Counter // 2 = 0 THEN
        LED2 = 1
      ELSE
        LED2 = 0
      ENDIF
    
    ENDIF
    /Henrik.

  8. #8
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    Can you do something like?

    Code:
    If Counter< 10 then LED1 = Counter.0 : LED2 = NOT LED1

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    Hi Tony. How about

    counter var byte
    clear 'all variables
    let portb =0 'off everything

    loophere:
    if counter = (your maximum number) then let counter = 0
    let counter = (counter + 1)
    pause 100
    let portb = counter
    goto loophere

    You will have a binary output on portb and can use the appropriate pins depending on the counter number.

  10. #10
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Take this action when these numbers are reached. How?

    Due to the complexity of the rest of the code, I have the main code setting the pause time. The three gosub commands I have written will be immediate, so as not to interfere with the timing cycle of the aforementioned pause.

    I have multiple outputs being controlled by two inputs. The outputs must reflect what the inputs are doing in real time, however, two of the outputs must run this code and not affect the timing of the rest of the outputs. A third set of outputs will execute another routine, but that code has already been written and tested. This is the last piece I have to finish.

    Using the method I mentioned above, it adds 240 words to my assembly. The way I came up for this program to work in my setup is to have the counter increment with each pass. Half of the count will be dedicated to LED1 and the other half to LED2. It will simply alternate flash the way the code is written.

    As I said, the timing portion is set by the main code, so no delays are in these subroutines, and hence, not affect the real time reflection of the inputs.

Similar Threads

  1. Inverting numbers
    By Picstar in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd January 2011, 13:36
  2. Numbers
    By jcleaver in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 7th April 2007, 18:32
  3. Action on line sync
    By hansknec in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd August 2006, 13:42
  4. Displaying numbers
    By Christopher4187 in forum General
    Replies: 8
    Last Post: - 20th March 2006, 23:14
  5. Help, Please....! :-( it is about numbers.
    By megamidi in forum General
    Replies: 0
    Last Post: - 8th December 2005, 22:44

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