On the Arduino Esplora, which is based on the Leonardo board, the PWM support of pin 5 which drives the Red component of the RGB LED looks like it shares a timer with the tone
function. This has the unintended consequence of rendering the Red component useless after playing a tone as its behavior becomes unpredictable.
Here's a simple setup
routine demonstrating the problem:
void setup()
{
//analogWrite(5, 255);
//delay(1000);
analogWrite(5, 1);
delay(2000);
Esplora.tone(440);
delay(1000);
Esplora.noTone();
}
This turns on the Red channel of the LED to its lowest setting and holds it for two seconds, then plays a 440 HZ tone for 1/4 second, but as soon as the tone starts, instead of remaining on low brightness, the LED turns completely off.
If you uncomment the first two lines, this time when the tone starts, instead of (incorrectly) turning off as before, it now (equally incorrectly) turns back to full-brightness.
I can't figure out how to restore proper functionality of the Red component (or more specifically PWM on pin 5) after calling tone
.
My guess is to generate the proper HZ for the sound, tone
changes the settings for the timer, which then affects the PWM function. If I can find out how to manually reset the timer back to properly support PWM for the LED again, that may be the solution. However I'm new to Arduino, timers and the like so this is pure speculation and I could be completely wrong with this approach or my understanding of it, but from what I've read, this does seem to be leading in the right direction.
So anyone know how to restore proper PWM functionality on that pin?
Mark