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.

asked Feb 8, 2019 at 9:25

1 Answer 1

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);
}
answered Feb 8, 2019 at 9:34
9
  • I'm not sure that's what I'm looking for....for example, if the value (x) is 2500, dividing by 1000 is 2.5...the result I'm expecting is 1000. How is this going to round the values? Commented Feb 8, 2019 at 9:46
  • In the wonderful world if integers dividing 2500 by 1000 gives us 2. I thing I misunderstood your question. You want the system to show only values 1,10,20,30... Right? Commented Feb 8, 2019 at 9:47
  • right, but still I need it to be 1000 when smaller than 5000...but in this case I can just have an IF. But when higher than 5000, for example 8000, it should result in 10000...(rounds up 8 to 10, etc)..17000 to 20000... Commented Feb 8, 2019 at 9:51
  • Yes, 1, 10, 20, 30..that's it. Commented Feb 8, 2019 at 9:52
  • that's great! it looks good now, I will have a go and let you know. Thanks. Commented Feb 8, 2019 at 9:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.