 # Need to create second function using same variables as first function

Hey guys and gals,

I am trying to finish a homework assignment and the goal is to being able to calculate the rate of acceleration for a bicycle and how long it will take to come to a rest at that rate of acceleration. Assume acceleration is constant. Acceleration in the homeowrk assignment is negative. The user chooses which numbers to input. I have it properly calculating the acceleration, but I need to figure out how to return the time it takes to stop moving. I’m lost! I have tried making a second function for the time, but it doesn’t like me using the same variables as the first function.

``````void setup(){
Serial.begin(9600);

}
void loop(){

float iv,fv,time,R,mins,T;

while(Serial.available()<=0);

iv = Serial.parseFloat();

Serial.println("Please enter a value for the final velocity");

while(Serial.available()<=0);

fv = Serial.parseFloat();

while(Serial.available()<=0);

time = Serial.parseFloat();

R = acceleration(iv,fv, time, mins);

DisplayResult(R);Serial.print(T);

}
float acceleration( float iv, float fv, float time,float mins){

mins = 60;
float acceleration = ((fv-iv)/(time/mins));
return acceleration;
}

void DisplayResult(float R){
Serial.print("The cyclist is accelerating at a rate of");
Serial.print(R);
Serial.print("mi/hr^2 and will come to a rest in");
}
``````

There is no need for the parameter mins for two reasons. Firstly it is a constant and should be declared as such. Secondly you never use the passed value since you are assigning a value inside the function. Is is questionable if that construct is a good one since you have to enter the time in decimal minutes that is 1min 30 sec will have to be entered as 1.5. Why not prompt the user for a value in seconds (which is the SI unit for time) or alternativeley write a parsing function that accepts the min:sec format. Also this line makes no sense:

``````float acceleration = ((fv-iv)/(time/mins));
``````

if you want to store the immediate result in a local variable fine, but don't use the function identifier for that. Thats something you do in Basic, not in arduino/java/C/C++.

I would write the code like this (with time given in seconds):

``````float acceleration( float iv, float fv, float time){
return (fv-iv)/time;
}
``````

The total stopping time would be initial velocity/retardation. You can check this by making a dimensional analysis: Velocity is m/s and acceleration is m/(s*s). (m/s)/(m/(s*s) = s which is what you want. So the stopping time function is

``````float stopTime(float iv, float ret){
return iv/ret;
}//stopTime()
``````

Do yourself a favour and put some analysis into your assignments instead of just come up with a solution with the least effort. Otherwise you will be wasting not only your own time but your teachers, your classmates and the schools general resources. Your'e in school to learn, not to learn to fake.