Need help with PWM Motor

I want to change this code so I don't have to change the speed in the Serial Monitor. I want to put in one speed and it to run that on start up.

int motorPin = 3;

void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}

void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}

Ok, then get rid of all the unneeded things like

int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{

and just have analogWrite(motorPin, /* Pick a number between 0 - 255 */);

THANK YOU!!!!

Im using a button to turn the motor on, is there a way i can make it so when i push the button it stays on?

Yes, but will you be using that same button to then turn it off too?

To use the same button to turn it on/off, you need a latch.

Example pseudo code:

boolean latch = LOW; // declaired at top of code

// inside the loop() or a function
if(digitalRead(2) == HIGH) 
// you will need another variable to block this IF statement if the button is held down ie. Prev_state. 
// Also debouncing may be needed, but you have an example code under Examples with the Arduino software
{
  latch = ! latch;
}

To do it with two different buttons.

if(digitalRead(2) == HIGH) 
{
  latch = true; // digital/analogWrite( motor pin, set speed)
}
else if(digitalRead(3) == HIGH)
{
  latch = false; // digital/analogWrite( motor pin, 0 or LOW)
}

its for a game so no just to turn it on!

so i can just use the code - latch = true; // befor the motor command?

If button 2 is pressed and nothing else changes "latch", then yes, latch will always be true.

if(digitalRead(2) == HIGH)
{
latch = true;
}

This is what i have so far but I know im missing a bit, do i have to state it in the setup?

int motorPin = 3;
const int switchPin = 2;
int switchState = 0;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop()
{
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
latch = true;
analogWrite(motorPin, 200);
}
else {
analogWrite(motorPin, 0);
}
}

You need to add boolean latch = false; at the top with the rest of your pins. Another thing, if the motor is to stay on forever when the button is pressed, then why have an else statement that makes it turn off?

To answer your problem, you need another if statement that looks to see if latch is true, then if it is, motor goes on.

Actually, if the motor is to be on forever when the button is pressed, then all you need is.

if(digitalRead(2) ) analogWrite(motor pin, 200); //no brackets needed for one line of code.
The motor will now always be on until commanded otherwise.

Later on i will need it to be turned off but im still awhile away from getting there