Byte value from a function

I would like to return a value from a function for use in other functions ran in void loop().

void Vspd()                                        //Vehicle speed sensor input, outputs MPH
  //Used in determining shift timing, downshift speed limiter, TCC operation  
  unsigned int vsspulse = pulseIn(vssIn, LOW);
  float vssval = (vsspulse * time1) ;
  //if(vssval > 
  byte MPH = ((1 / vssval) / time1 * 2 / FPM * TIRCIR / PPR * DIV * 4 * 1.7 );
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = MPH; 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;

I want to be able to use MPH without having to include the code within Vspd. I have tried including Vspd(); in another function but MPH remains zero so it is not being used outside of Vspd function.

When using Vspd() in void loop() by itself, the correct MPH is displayed via LCD and serial monitor so I don't need help figuring how to make my function work, just how to integrate it for use with other functions.

Constructive criticism is always welcome.


Change the definition to

byte Vspd() {

and as the last thing inside the function have

return MPH;

Then you can call the function with

newMPH = Vspd();



As usual it’s simple enough, just over thinking as usual. :slight_smile:

I’ll give your suggestion a try. Thanks for the quick response. Have a great day.


Worked great! Yay, and the natives rejoiced. :-)