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.
1 Answer 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);
}
-
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?Emerson– Emerson2019年02月08日 09:46:53 +00:00Commented Feb 8, 2019 at 9:46
-
In the wonderful world if integers dividing
2500
by1000
gives us2
. I thing I misunderstood your question. You want the system to show only values 1,10,20,30... Right?Filip Franik– Filip Franik2019年02月08日 09:47:57 +00:00Commented 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...Emerson– Emerson2019年02月08日 09:51:55 +00:00Commented Feb 8, 2019 at 9:51
-
Yes, 1, 10, 20, 30..that's it.Emerson– Emerson2019年02月08日 09:52:34 +00:00Commented Feb 8, 2019 at 9:52
-
that's great! it looks good now, I will have a go and let you know. Thanks.Emerson– Emerson2019年02月08日 09:55:54 +00:00Commented Feb 8, 2019 at 9:55
Explore related questions
See similar questions with these tags.