How can I change the value of a Float smoothly?

Its might be kinda a silly, but I can’t seem to get it to work. What I want is for my float to go to its new value over time, check:

var Speed : float = 0;

function Called(){

Speed  = 100; 

//I would like this to happen smoothly, however, I would also like to change back to say 40 if I call it later.

yield WaitForSeconds(2);
Speed = 40;

// So the first time its changed from 0 tot 100, and the second time from 100, 40, how can I make this smooooothhh?
}

Thanks!

IEnumerator ChangeSpeed( float v_start, float v_end, float duration )
{
float elapsed = 0.0f;
while (elapsed < duration )
{
speed = Mathf.Lerp( v_start, v_end, elapsed / duration );
elapsed += Time.deltaTime;
yield return null;
}
speed = v_end;
}

Call it with:

StartCoroutine( ChangeSpeed( 100f, 40f, 2f ) );

I know this is old, but if you just want to adjust the value of a float smoothly inside an Update function, you can do something like this:


float current = 0.0f; 

void Update(){
        float target = 1.0f;

        float delta = target - current;
        delta *= Time.deltaTime;

        current += delta;
}