C - function to return curved result

sscode

I have a general programming challenge for you all. I've been scratching my head trying to figure out the best way to do this...

I'm working in C and I've got a "speed" value that is passed to my program. It ranges from 0-255. This speed value needs to correspond to a millisecond time delay built into the program.

    unsigned char currentSpeed; //this is passed into my function, range 0-255
    unsigned long SlowestSpeedMillis = 3000; //in milliseconds
    if (currentSpeed > 0)
    {
       unsigned long Interval = ((256 - currentSpeed)*SlowestSpeedMillis)/255;

    }else{ //currentSpeed = 0
       //When Interval is zero, the program will freeze
       Interval = 0;
    }

Here's some sample results:
currentSpeed of 1 results in 3000 milliseconds (slowest)
currentSpeed of 128 results in ~1500 milliseconds (exactly half)
currentSpeed of 255 results in ~11 milliseconds (fastest)

The problem is that I would like it to be a curved result where it stays in the lower numbers and then quickly goes to 3000 at the end... I want the user to have the choice to make the program go as slow as 3000, but most values leading up to it aren't as important. Such as this:
currentSpeed of 1 results in 3000 milliseconds (slowest)
currentSpeed of 128 results in ~750 milliseconds (1/4th of potential)
currentSpeed of 255 results in ~11 milliseconds (fastest, anything close to zero)

Any ideas how to do this programmatically? Perhaps with some kind of equation?

joeking

Normally in math you might do something like:

(currentSpeed / 255) * 3000

for linear, and to get a bit of a curve use a power:

((currentSpeed / 255) * (currentSpeed / 255)) * 3000

But, in integer math that doesn't work because (currentSpeed / 255) will almost always be zero.

To do this without floating point you have to scale up first before division.

((3000 * currentSpeed) * currentSpeed) / (255 * 255)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return powf result to main function C Programming

From Dev

Return result to parent function

From Dev

Return result of Action to Function

From Dev

Return mongoose result in a function

From Dev

wrapAsync return function not result

From Dev

How to return the result of a postgresql function in c#? Console output empty

From Dev

read() function in C with fixed size buffer fail to return the expected result

From Dev

C function returns correct result but no return statement is given, why?

From Dev

Return stored procedure result in function

From Dev

How to get function return result?

From Dev

excel VBA return result of function

From Dev

Return select * result in postgres function

From Dev

Creating a function to return boolean result?

From Dev

Where to return result of recursive function

From Dev

Return a array of result from function

From Dev

How to get function return result?

From Dev

return function result to output argument

From Dev

Is there any purpose to return the result of a function in an empty function?

From Dev

Return result by reference in C++

From Java

Unexpected result when C++ store element into std::vector from return value of function

From Dev

PHP function shows result but does not return it

From Dev

DCOUNT function doesn't return the correct result

From Dev

Bind a lambda to a function to get return result

From Dev

how to pass result of $.get to return of another function

From Dev

How to return dereferenced value as a result of a function in Clojure

From Dev

SAS Macro function return SQL result

From Dev

Return result disappears in Python recursive function

From Dev

How to return a failure inside .map function in a Result

From Dev

How to return the onclick function is result to href in javascript?

Related Related

  1. 1

    Return powf result to main function C Programming

  2. 2

    Return result to parent function

  3. 3

    Return result of Action to Function

  4. 4

    Return mongoose result in a function

  5. 5

    wrapAsync return function not result

  6. 6

    How to return the result of a postgresql function in c#? Console output empty

  7. 7

    read() function in C with fixed size buffer fail to return the expected result

  8. 8

    C function returns correct result but no return statement is given, why?

  9. 9

    Return stored procedure result in function

  10. 10

    How to get function return result?

  11. 11

    excel VBA return result of function

  12. 12

    Return select * result in postgres function

  13. 13

    Creating a function to return boolean result?

  14. 14

    Where to return result of recursive function

  15. 15

    Return a array of result from function

  16. 16

    How to get function return result?

  17. 17

    return function result to output argument

  18. 18

    Is there any purpose to return the result of a function in an empty function?

  19. 19

    Return result by reference in C++

  20. 20

    Unexpected result when C++ store element into std::vector from return value of function

  21. 21

    PHP function shows result but does not return it

  22. 22

    DCOUNT function doesn't return the correct result

  23. 23

    Bind a lambda to a function to get return result

  24. 24

    how to pass result of $.get to return of another function

  25. 25

    How to return dereferenced value as a result of a function in Clojure

  26. 26

    SAS Macro function return SQL result

  27. 27

    Return result disappears in Python recursive function

  28. 28

    How to return a failure inside .map function in a Result

  29. 29

    How to return the onclick function is result to href in javascript?

HotTag

Archive