Simple Count Up then Down

Hi. New to the forums, been loving xod for a couple years. Many thanks to all the contributors. It is intuitive and ive made some great stuff as a hobbyist.

I have run into what must be a simple problem but i cant seem to figure it out on my own.

***How can one make a counter that say starts at 0 goes Up to 5 and reverses back Down and so on. ***

I have counters that go up and others that go down and repeat etc but its the switch from one direction to the other that has me scratching my head.

Its actually part of a more complex thing but if someone could help point me in right direction?.. again, many thanks to all of you for the great posts and work. Im sure there are many like me who have gained from it quietly in far flung places.

Hi, if you use if-else with +1 and -1 and comparator, there is also the up-down in nkrkv/encoder

Since you are cycling up and down, you need to remember state as well as use comparator to determine direction you should be counting. Here is one solution:

image
Since the flip-flop is going to default to false and count defaults to zero, we want false to be our “count up” state, so our count-STEP is 1 when flip-flop-MEM is false. When we get to our max (5), we want to trigger flip-flop-SET, which will change count-STEP to -1, so we will now count down until we get to our min (0) and we trigger flip-flop-RST, changing count-STEP back to 1 so we are counting up again.

I only use greater-or-equal and less-or-equal instead of equal because technically count is using floating point numbers, and there is a chance for rounding errors to cause count to become something like 0.000000001, which is NOT equal to zero. It is a simple fail-safe to add to your programs so you don’t accidentally run past where you want to stop; the worst-case is now that you will go to -1 or 6 before changing direction rather than running to infinity (or in this case an error because the float value over-flows–the value becoming too small/big to store).

NOTE: since the count node does not provide a way to start at any number besides 0, counting from 1 to 5 instead of 0-5 becomes difficult. Probably the easiest solution is to let the count node count between 0 and 4, then add 1 to the output of count to get 1-5. The same technique can be used to count -5 to 5, or any other range. You can also use it to reverse directions; for example, if you wanted to start at 5, count down to 0, then back up to 5, you can use the code above, but subtract count output from 5. The 1st number would be 5-0=5, then 5-1=4, etc.

1 Like

This seems to work ok

Or this :slight_smile:

@williamobrien-sk’s solution takes advantage of nkrkv/encoder/count (which requires adding rkrkv/encoder library).

The only thing I don’t like about his code is that he uses an any node to toggle flip-flop instead of tying the conditions directly to SET or RST. In his code, he always wants less-or-equal to SET the flip-flop and greater-or-equal to RST the flip-flop, so it makes sense to tie the condition outputs directly to those pins rather than using them to just TGL the flip-flop, which has the potential for the flip-flop to be in the wrong state. If it works as-is, it doesn’t NEED changed, but why add ambiguity/risk to the code by using TGL when SET/RST makes it explicit what you are trying to do?

They read the same after many cycles

Wow. You folks are awesome. Much appreciated.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.