Take this action when these numbers are reached. How?


Closed Thread
Results 1 to 10 of 10

Hybrid View

  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,680


    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,680


    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,680


    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

Similar Threads

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

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