PDA

View Full Version : Take this action when these numbers are reached. How?



ERMEGM
- 8th July 2015, 07:20
Is there a way to select an action when a specific number is reached in a counter?

For example, if I write:


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

richard
- 8th July 2015, 07:40
select case counter

case 2
do stuff
case 4
do other stuff

....
case else
do something else

end select

ERMEGM
- 8th July 2015, 08:06
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.

richard
- 8th July 2015, 08:21
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

ERMEGM
- 8th July 2015, 08:39
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

richard
- 8th July 2015, 08:47
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

HenrikOlsson
- 8th July 2015, 12:36
Perhaps something like;

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.

Amoque
- 8th July 2015, 13:55
Can you do something like?



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

peterdeco1
- 8th July 2015, 17:36
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.

ERMEGM
- 8th July 2015, 17:58
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.