I found here code for increasing PWM resolution to 10bit. It works. But I have one problem with it.
#define potPin 0
#define ledPin 9
#define delayTime 500
int potVal;
int i;
void setup()
{
/**********************************************************************************/
// Set pwm clock divider
/**********************************************************************************/
TCCR1B &= ~(1 << CS12);
TCCR1B |= (1 << CS11);
TCCR1B &= ~(1 << CS10);
/**********************************************************************************/
// Set pwm resolution to mode 7 (10 bit)
/**********************************************************************************/
TCCR1B &= ~(1 << WGM13); // Timer B clear bit 4
TCCR1B |= (1 << WGM12); // set bit 3
TCCR1A |= (1 << WGM11); // Timer A set bit 1
TCCR1A |= (1 << WGM10); // set bit 0
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// potVal = analogRead(potPin);
for( int i = 250 ; i < 260 ; i++ ){
analogWrite( ledPin, i );
Serial.println(i);
delay( delayTime );
}
}
I have LED connected on ledPin. Everything works fine, but when there is value 255 in i LED is fully on. So if I will connect also pot and turn it from 0 to max on position 255 LED will blink to full brightness.
How to solve this without solution like inserting this:
if i=255 than i=256(or254) ...
So without skiping this step.
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
...
I did it and now LED do what I expect. And also full range sweep works fine without flash on 255 like before. 0 to 1023 no problem. It was no problem also before, problem was only 255 value, because on this value LED was on full brightness. Now 250, 255, 260 looks like cca 25% brightness. Like it should if 1023 is full.