fade LED to specific brightness

Hello everyone, can anyone give me hint on how to fade an LED to a specific brightness, i.e. go from 0-x rather than 0-255?

I've been working with the stock code:

void loop()
{
   for (int i=0; i <= 255; i++){
      analogWrite(PWMpin, i);
      delay(10);
   } 
}

But you can't simply change 255 to something else, or the fade speed will change.

I'm using an uno to program an ATTiny85 at 8mhz using MIT's code.

Thank you very much.

jetjaguar:
But you can't simply change 255 to something else, or the fade speed will change.

You'll have to adjust the delay amount if you want it fade to a different amount in the same amount of time. If I told you to walk half a mile in the same time it takes you to walk a mile, you would need to walk slower.

Well that was fast, thank you very much.

I'm trying different combinations of ...<=X and delay(y), however the brightness seems to always go up to the max 255.

jetjaguar:
I'm trying different combinations of ...<=X and delay(y), however the brightness seems to always go up to the max 255.

Then post what you have. Keep in mind that the human eye may not be able to differentiate between the 255 and something like 240. The be absolutely certain, you can print the value of i to the Serial monitor.

I would expect this to fade the led up to half-brightness.

void setup() {}
void loop()
{
   for (int i=0; i <= 127; i++){
      analogWrite(1, i);
      delay(5);
   } 
}

(I haven't experimented with attiny + serial monitor yet.)

Maybe I know the problem.

I'm using a common ANODE rgb led (just experimenting with one color for now).

Is my proposition maybe not possible because of this?

jetjaguar:
Is my proposition maybe not possible because of this?

No clue. I can tell you that with that code, for approximately 5 ms, it goes to 50% duty cycle. How it looks to you is outside of my purview. Try setting up two another one of the pins. Set one to 0 and one to 127. Then see if there is a difference in color if the values are swapped.

OK, I got it.

Because it's common anode, 0 = maximum brightness and 255 = off. So with that in mind here is the arduino tutorial fade code adjusted so that it fades in and out but never gets very bright.

Thank you both very much for your help.

int ledPin = 1;    // LED connected to digital pin 9

void setup()  { 
  // nothing happens in setup 
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 240 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 240 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 240; fadeValue -=5) { 
    // sets the value (range from 240 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 200 milliseconds to see the dimming effect    
    delay(200);                            
  } 
}