Running a DC motor with PWM

Jason2548:
I don't want it do go down on its own. I want it to go up and then stay up

This means it will slowly increase speed, and when it reaches top speed, it will stay there.

Jason2548:
I can't get it to stop once it reaches the 255

This would mean it will slowly increase speed, and once it reaches top speed, it would stop immediatly.
Those are 2 different things.

As the code you came up with keeps increasing but will increase by zero once it has reached the preset, i'm assuming you meant stop increasing instead of stop the motor.

I think this should also work and is a bit simpler as you don't need the help of an extra variable.
Haven't tested it, but it does compile.

int PWMpin = 13; 
int PWM_val = 0;
void setup()
{
}
void loop()
{
    PWM_val++;
    if (PWM_val == 256) PWM_val --;
      analogWrite(PWMpin, PWM_val);
      delay(10);
}

This way you will stay inside the loop, and also stay counting up (and down again), and you'll keep writing the same value to pin 13.

Last week i discovered the switch...case statement.
It is like if...then, but has some advantages which let me like it more.
For one it has the break.
That break will end the processing of the other cases.
The last case (default) defines what has to be done when neither of the cases are true.
Doing this will check the value that has been reached, but if you are already have set your maximum, it will not change the output of the pin (or increase the value):

int PWMpin = 13; 
int PWM_val = 0;
void setup()
{
}
void loop()
{
    
   switch (PWM_val)
   {
    case 256:
    break;  //This will skip out of the switch function
    default:
      {
        analogWrite(PWMpin, PWM_val);
      PWM_val++;
      delay(10);
      }
   }
}

Once more, not checked, but it will compile.

Check the language reference every now and then for some functions you haven't heard of before.

(edited: added the comment and {wrapped} the default: actions)