Counting the laps: GPS triangle racing

# Creating and using counters

## Introduction to counters

Counters are useful in many situations - anything from counting laps, to playing tracks in a playlist. Implementing counters in Ethos does, however, require some advanced programming techniques, which we'll explore in this article. Let's get started!

## Example 10-step counter

By way of an example, we'll create a 10-step counter, with auto and manual reset. Here's the full specification:

• Count up to 10 steps
• Press SI to increment.
• Press SJ to reset.
• The counter also resets automatically after 10 counts.

We'll use CH10 to store the counter value. Note that the counter increment is 5 (using a large increment avoids issues with internal rounding errors).

First, we set up two logical switches LSW1 and LSW2. LSW1 goes true if the counter has exceeded the maximum. LSW2 goes true if the counter has exceeded the limit, or if SJ is pressed:

LSW1: CH10 > 47

LSW2: LSW1 OR SJ↓

Logical switches

Next, we create three free mixes to do the counting and reset.

Mixers

======

FreeMix1: src=CH10, output=CH10

FreeMix2: src=special[0], offset=5%, active-condition=Edge(SI↓), output=CH10

FreeMix3: src=special[0], active-condition=LSW2, function=Replace, output=CH10

Free mix 1. The single output is CH10 (not shown).

Free mix 2

Free mix 3

### How it works

To understand how the mixers work, remember that Ethos executes a processing cycle several times a second. At the start of each cycle, all channels, including CH10, are reset to zero. Ethos then steps through the mixers one at a time, in the order they appear in the Mixers menu.

Considering our three mixers in isolation:

1. FreeMix1 says "update CH10 with its value from the previous cycle", in other words, hold the last value. Without this mix, changes to CH10 in subsequent mixes would last only while those mixers were active.
2. FreeMix2 is active when SI is pressed. While active, it increments the value of CH10 by 5. The 'edge' option ensures that the mix is active for one mixer cycle only.
3. FreeMix3 is active when SJ is pressed, or when 10 steps have been counted (LSW2). It forces CH10 to zero (the Replace function means 'ignore all mixers above this one').
Notes:
• All fields at their default values except where noted.
• In FreeMix2, the active condition uses an 'edge' modifier. To reveal the edge option, long press on the active condition field.
• As of 1.0.10, the edge option doesn't work properly with logical switches or trim sources (due to be fixed in 1.0.12).

## Variations

A 'decrement' function can be added, by inserting a mix immediately after FreeMix2.

FreeMix: src=special[0], offset=−5%, Active condition= edge(SH↓), output=CH10

The logical switches must be modified to detect negative values:

LSW1: not in range [-1 to 46]

LSW2: SJ↓ or LSW1

## Example: manoeuvre callout system

Here's an example application, for aerobatic flyers. At the end of each manoeuvre, the pilot presses button SI, and the transmitter calls out the next manoeuvre.

We can use the counter code already defined above – all remains is to test the counter values, and to play the tracks.

### Testing the counter

The counter can be tested using a bunch of logical switches:

LSW3: CH10 ~= 0

LSW4: CH10 ~= 5

LSW5: CH10 ~= 10

[…]

LSW12: CH10 ~= 45

Note the use of the ~= operator. This means 'approximately equal' and is necessary to avoid issues with internal rounding errors.

### Playing the tracks

Finally, special functions are used to play the tracks, each triggered by a logical switch:

SF1: switch=LSW3 function=PlayTrack (track 1)

SF2: switch=LSW4 function=PlayTrack (track 2)

[…]

SF10: switch=LSW12 function=PlayTrack (track 10)