1

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).

Erik
  • 261
  • 1
  • 12
  • Do you maybe have to stop the timer peripheral to be able to update it? Can you call Timer1.stop() and replace your Timer1.setPeriod call with Timer1.start(60000000 / bpm / 2)? – Maximilian Gerhardt May 18 '18 at 17:25
  • Good idea! Tried it, but nothing changed... – Erik May 18 '18 at 17:59
  • I think that each time you go through loop(), the Timer1.setPeriod() calls Timer1.setFrequency() and re-initializes the timer. Does loop() happen more often than 1/285th of a second? – Dave X May 18 '18 at 19:03
  • @DaveX so essentially, the problem could be that the timer is constantly reinitialized within the loop function? Then i suppose a solution would be to keep the timer clock constant and recalculate bpm in a separate function counting microseconds? – Erik May 18 '18 at 19:15
  • Can you try to maybe create a global variable int oldBpm;, and only do a TImer1.setPeriod() with a oldBpm = bpm; if oldBpm != bpm? (i.e. only update timer period if new BPM value is different from current one) – Maximilian Gerhardt May 18 '18 at 19:51
  • 2
    Given the slow time scale, you could just handle the timing in software, with either millis() or micros(). You could then very easily update the frequency in a phase-continuous manner. – Edgar Bonet May 19 '18 at 16:00
  • @EdgarBonet Thanks a lot, you're right. I believe i was overcomplicating things! It's working as intended now. – Erik May 19 '18 at 19:54

0 Answers0