/*
=== Clock Multiplier for low Frequencies (i.E. BPM-Clocks for Music Sequencers, e.t.c.) =================
Peter Hostermann 2020-10-20
https://www.hostermann.de
peter@hostermann.de
Code under GNU General Public License
Uses 'attachInterrupt' for external trigger events and TimerOne lib for internal controlled timer events.
Rising clock-impulse on pin D3 will cause interrupt -> jump to subroutine 'calculate' -> calculate interval
in microseconds between two jumps -> re-initialize TimerOne period with interval divided by multiplier (i.E.
divide by factor 2 halves the period time = doubles frequence -> TimerOne then triggers subroutine 'multiple'
with doubled clock speed)
Works well with Arduino Nano. Protect the input with clipping diodes and the outputs with 1 KOhm resistor.
Tested with clock source max. 240 BPM = 4 Hertz.
Library -> https://github.com/PaulStoffregen/TimerOne
Clock Input: Pin D3
Clock Output: Pin D7
Multiplied Clock Ouptut: Pin D8
*/
#include <TimerOne.h>
uint32_t start = micros();
uint32_t end;
uint32_t inter;
byte multiplier = 4;
byte out = 7;
byte m_out = 8;
#define trigger(pin) {digitalWrite(pin, HIGH); delay(20); digitalWrite(pin, LOW); }
void setup() {
pinMode(out, OUTPUT);
pinMode(m_out, OUTPUT);
attachInterrupt(digitalPinToInterrupt(3), calculate, RISING);
Timer1.initialize(500000);
Timer1.attachInterrupt(multiple);
}
void calculate() {
end = micros();
inter = end - start;
start = end;
trigger(out);
Timer1.stop();
Timer1.setPeriod(inter/ multiplier);
Timer1.restart();
}
void multiple() {
trigger(m_out);
}
void loop() {
}