Output a specific voltage for a specific time

Hi. I'm trying to output a set voltage for a specific time interval. For example, I want to output a voltage of 3V for 30 seconds and then switch it off. Should I use PWM with a low pass filter for this?

I'm fairly new with arduino and this is the code I already have.

int muscle = 10; // the PWM pin the muscle is attached to
int writeInputVoltage = 127;

// the setup routine runs once when you press reset:
void setup() {

Serial.begin (9600);
// declare pin 10 to be an output:
pinMode(muscle, OUTPUT);

}

void loop() {

//turn muscle pin on:
analogWrite (muscle,writeInputVoltage);

}

So in this case I get a square output wave with a 50% duty cycle and the voltage will vary between 0-5V. Is that right?

But let's say I want an output voltage of 2.5V. Then what will analogWrite function look like. Will this code work:

int muscle = 10; // the PWM pin the muscle is attached to
int muscleHeatTime = 10000;
int muscleCoolTime = 5000;
int writeInputVoltage = 127 ;

// the setup routine runs once when you press reset:
void setup() {

Serial.begin (9600);
// declare pin 10 to be an output:
pinMode(muscle, OUTPUT);

Serial.println("What is the input voltage (0-255)? "); //Prompt User for Input
while (Serial.available()==0){ } //Wait for User Input
writeInputVoltage = Serial.parseInt(); //Read User Input
}

void loop() {

//turn muscle pin on:
analogWrite (muscle,writeInputVoltage);
delay (muscleHeatTime);

//turn the muscle pin off:
analogWrite (muscle,0);
delay (muscleCoolTime);

}

Yes. It does seem that you need a low-pass filter for this.

Or a dedicated DAC. (Digital to analog converter.)

Thanks MorganS.

Let's say I want to output a voltage of 3.5V and I'm using a low pass filter. When I'm prompted for a voltage will writing the corresponding analog value of 3.5V do the trick? And then how can I modulate the time for which this voltage is supplied?

When I'm prompted for a voltage will writing the corresponding analog value of 3.5V do the trick?

No. analogWrtite takes a value of 0 to 255 which corresponds to 0 to 5V if you got the low pass filter. So the way you have it written now you'd have to enter 0 to 255.