Here's a problem. Other things aside:
void analogWrite(uint8_t pin, int val)
{
// We need to make sure the PWM output is enabled for those pins
// that support it, as we turn it off when digitally reading or
// writing with them. Also, make sure the pin is in output mode
// for consistenty with Wiring, which doesn't require a pinMode
// call for the analog output pins.
pinMode(pin, OUTPUT);
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
First, analogWrite takes a 16-bit value. So far so good. But it has a specific test for 255. That's not so good. My testing confirms that if you try something like this:
analogWrite (9, 254);
analogWrite (9, 255); // fully on
analogWrite (9, 256);
Then you get the glitch on exactly 255. And that was after adding:
TCCR1A = 0x03;
This seems to work better:
OCR1A = timerVal; // set pwm duty
Further testing under way ...