I want to test a BPM counter as a part of larger project rewriting code for a groovesizer synth. In essence i want to read a pot for updating BPM tempo, and then have a LED blink that tempo, displaying BPM value on an LCD screen.
I am using the DueTimer library. The code for reading pot value, LED blinking and showing the BPM value in an LCD display works fine. However, i am stuck figuring out how to update the ISR timer frequency continously. I can only have the LED blink in the tempo value initialized in setup().
I tried adding the Timer1.setPeriod() to the function reading pot value, to no effect:
pot[4] = (analogRead(i) >> 2) + 30 ; // we use >> 2 to reduce the range from 0 - 1023 to 0 - 255 and add 30 for bpm value
bpm = pot[4];
Timer1.setPeriod(60000000 / bpm / 2);
Putting the same Timer1.setPeriod(bpm) in the main loop didn't work either:
void loop() {
checkSwitches(); // gets the current state of the buttons - defined in BUTTONS
getPots(); // update the pot values - defined in POTS
Timer1.setPeriod(60000000 / bpm / 2);
updateDisplay();
}
Not sure where to go from here. (I'm a total beginner in case it isn't obvious).
Timer1.stop()and replace yourTimer1.setPeriodcall withTimer1.start(60000000 / bpm / 2)? – Maximilian Gerhardt May 18 '18 at 17:25int oldBpm;, and only do aTImer1.setPeriod()with aoldBpm = bpm;ifoldBpm != bpm? (i.e. only update timer period if new BPM value is different from current one) – Maximilian Gerhardt May 18 '18 at 19:51millis()ormicros(). You could then very easily update the frequency in a phase-continuous manner. – Edgar Bonet May 19 '18 at 16:00