Hi
I have a problem as i want to have a Tone out of the Arduino MEGA 2560.
It has to go from around 10 HZ to 350 HZ
But it conflits width PWM on PIN 10 and tone on pin 46. (The tone jumps out of control when PWM is turned on and off).
Well then i tryed to change the code in TONE.CPP to use timer "5"
#define USE_TIMER5 //Here i wrote 5 instedt of 2
const uint8_t PROGMEM tone_pin_to_timer_PGM = { 5 /, 3, 4, 5, 1, 0 / }; //Here i wrote 5 instedt of 2
That made it work but it now and then falls out, no tone for around 100 mS (this only happens when i smooth change the tone up and down from 10 to 350 HZ)
int X = 0;
Int Xp;
void setup() {
}
void loop() {
X = analogRead(14)-300;
if( X < 33 ){ X = 33;}
if( X > 300 ){ X = 300;}
if ( X != Xp )
{
tone(46 , X);
Xp = X;
}
}
I think I know roughly what is going wrong, I can't put my finger on the fix. The timer is a 16-bit timer, so if it happens to overshoot the count (which would tend to happen when the frequency decreases) then it counts all the way up to 65535 before matching. This is confirmed by:
62.5e-9 * 65536 * 64 = 0.262144
So you might expect a gap of around 262 mS if this happens. And indeed I am measuring pauses along those lines.
For example, if it was previously counting up to 500, and it had reached 450, but you change the count to 300, it has already overshot 300, so it counts all the way up to 65535, giving a much longer gap.
So far my attempts to fix it have been spectacularly unsuccessful. Some of the gaps are gone, but the ones that remain blow out to 1.5 seconds! There must be more to this than meets the eye.
The theory is to do this, while the timer is stopped:
TCNT5H = 0;
TCNT5L = 0;
That should reset the counter so it doesn't overshoot.
Cool i was thinking of using the timer directly, but my programming skills, are not Close to yours
I will try to test this tomorrow.
Its getting late here on the other side of the Earth.
I will get back width result
Hi I have just implemented that in my code and now have a rock solid TACHO driver.
I think that this tread can be useful for many people. having problem width timers on the Mega
I must say i have newer expected to get that kind of help in a Forum, Thanks alot.
Just wanted to give proof that this is still useful after all those years. It really saved my day, going from 'What the hell?!?!' directly to 'Eureka'! ;-D
Two thumbs up!!!