0

I'm mapping a potentiometer from 0-1023 to 1000-120000 (1 sec to 2 minutes) for a timer, like:

maxDelay = map (pot2, 0, 1023, 1000, 120000);

Both variables are integers. Now I would like to convert/round values to counts of 10 seconds (a count of thousands), so if I have anything from 1001-5000 this would be rounded to 1000 (1sec), and if I have 5001-10000 it would be rounded to 10000 (10 secs), then 20sec, 30sec, etc...until it reaches 120secs. I know I could have a few "if" statements but I'm trying to figure out the right way of doing these maths. Thanks.

Emerson
  • 105
  • 1
  • 7

1 Answers1

1

I didn't yet compiled it, but something like this should work:

#include <Math.h>

int converter(int x)
{
    if(x <= 5000)
        return 1000;
    else 
        return (int)(round(x / 10000.0) * 10000.0);
}
Filip Franik
  • 1,312
  • 1
  • 8
  • 22