This tutorial sets up a bunch of weird clock divisions/triggers on the Ardcore expansion's digital outputs.
* [Download sketch for nw2s::B](https://github.com/nw2s/b/blob/master/sketches/nw2s/bDemoTriggers/bDemoTriggers.ino)
* [Download sketch for Ardcore](https://github.com/nw2s/b/blob/master/sketches/nw2s/ArdcoreDemoTriggers/ArdcoreDemoTriggers.ino)
## Event Manager
The first thing that every nw2s::b sketch needs is an EventManager.
EventManager is the source of timing for all devices and encapsulates a lot of the orchestration code that is generally placed in the
loop() portion of other sketches. This keeps your code clean and focused only on what you want to do - control modular synthesizers.
We will now create a clock and attach the clock to the event manager.
Clock* democlock = FixedClock::create(75, 16);
A clock takes timing signals from the event manager and converts those into regular intervals which you can use to control devices such as sequencers and triggers - devices that want to perform some task on a regular beat.
This specifc clock
democlock is a fixed tempo clock running at 75 BPM with 16 beats per phrase. The beats per phrase value is only used to drive the timing display on the nw2s::b hardware. It does nothing on the Ardcore device.
### Variable Clock
Another clock is the variable clock:
Clock* democlock = VariableClock::create(25, 125, ARDCORE_IN_A0, 16);
The variable clock operates over a range of tempos specified by a minimum and maximum whose value is set by a CV input. In this case, we've indicated that we want to be able to adjust the tempo from 25 to 125 BPM using the value set by the Ardcore's A0 input. If you are using the nw2s::b hardware, then you can replace that value with any of the input names for that device.
### Slave Clock
The final clock is the Slave Clock. It will follow the rising signal on a digital input of the device and calculate the tempo.
Clock* democlock = SlaveClock::create(ARDCORE_CLOCK_IN, 16);
As with the earlier examples, the parameters to the clock factory method should be reasonably straightforward. This clock will use the Ardcore CLK input and have 16 beats per phrase.
There are some limiations to the slave clock:
* The slave clock is meant to chase a reasonably stable signal. It's not meant to be able to adjust to a wildly frequency modulated clock signal. Keep your expectations in check!
* On the Ardcore, the only interrupt driven input is labled CLK, you must use this input, and technically, the input parameter does nothing on the AVR devices. You may use any digital input on the SAM-based devices.
* Only one slave clock may be used at a time due to the static nature of the microcontroller interrupts.
* The slave clock will not calculate a tempo until it's received at least two rising edges of a clock signal.
* If the input signal stops, then the clock will continue with the last tempo that was calculated.
Technically, you could use the slave clock as a tap-tempo clock if you have something that will convert taps to rising-edge clock signals.
### Random Clocks?
Yes, Random Clocks. If you want things to happen randomly in time, then it makes sense that we'd set up a clock that is capable of triggering random time events.
Clock* democlock = RandomTempoClock::create(75, 80, 16);
This random clock randomly picks tempos from 75 BPM to 80 BPM with 16 beats per phrase.
Something that's important to remember about this clock is that it does not fire events randomly as if it were emulating a Geiger counter (we'll get to that later). The random clock generates a random tempo value at each beat. The timing of all subdivisions within that beat are consistent with the tempo defined for that beat. Next beat, a new tempo is calculated. Ad nauseum.
This clock can be subtle or extreme. Picking a min and max tempo one or two BPM either side of your target will generate some human feel, increasing that to 10 or so BPM either side will generate a drunken walk, and far outside that range will create a much more unpredictable randomness that's closer to noise.
### More Clocks
Note that it's perfectly reasonable to run multiple clocks at the same time on different tempos.
Clock* clock1 = FixedClock::create(120, 16);
Clock* clock2 = FixedClock::create(75, 16);
Clock* clock3 = VariableClock::create(25, 125, DUE_IN_A0, 16);
Now that we understand the Event Manager and the clocks that are available, let's hook something up to a clock and make it do some work. Any beat-based device may be connected to a clock. The simplest beat-based device is a trigger.
Trigger* trigger0 = Trigger::create(ARDCORE_OUT_EX_D0, DIV_QUARTER);
The trigger is simply a device that regularly pulses a digital output on a given clock division. This clock pulses out quarter notes (1:1 with the tempo) on the D0 output pin of the Ardcore Expander.
You can set up a few triggers to have your own custom clock divider like so:
Trigger* trigger1 = Trigger::create(ARDCORE_OUT_EX_D1, DIV_HALF);
Trigger* trigger2 = Trigger::create(ARDCORE_OUT_EX_D2, DIV_EIGHTH_DOT);
Trigger* trigger3 = Trigger::create(ARDCORE_OUT_EX_D3, DIV_QUARTER_TRIPLET);
Trigger* trigger4 = Trigger::create(ARDCORE_OUT_EX_D4, DIV_SIXTEENTH);
For more information about some of the values used in the tutorial like the clock divisions and the I/O names, see [Constants](https://github.com/nw2s/b/wiki/99-Reference:-Constants).
If you are ready to move on to some more devices, then go to [Sequences](https://github.com/nw2s/b/wiki/04-Tutorial:-Sequences)