# SOLVED mapping a guage to a percentage for nextion

hey all, im using a nextion display to show a guage for cpu usage
ive attached a picture of the guage im using for reference

my issue is in nextion a value of 0 sets the needle to 9 oclock on the guage

so i need to figure a way of mapping 0%-100% to a range of 270 degrees(the total desired arc) 315(0%) - 225(100%)
keeping in mind that as the percentage rises to 360 it will have to pass through 360 then 0 then 1 etc

im not smart enough to work this out haha ... my first thought was oh thats easy, ill just subtract the offset from the value untill i hit 360 degrees then add another offset, but that wont work as each scale has a different amount of points... then i though ill write down every value and do a buttload of if statements to replace each possible value one by one ... something tells me this is retarded.
i spose i really need to know how to bind say 0%-30% to like 270-360 and then like 30%-100% being 0-225
theres a better way? this might not even be a programming question but more one of maths?

What are the numbers to set the gauge to 0% CPU, 50% and 100%?

so 0% is 315degrees 50% is 90degrees and 100% is 225degrees

Spl1nt3rm4n:
so 0% is 315degrees 50% is 90degrees and 100% is 225degrees

assuming a linear relationship, then its would be a simple matter of doing a y=mx+c but to determine the value of c, we would need to know where does the needle like when it 0 degrees.

if the needle is horizontal pointing towards the left,
then 100% is -45degrees

50% is still 90degrees

0% is 225degrees

``````if (cpu > 16)
angle = map(cpu,16,100,0,255);
else if(cpu==0)
angle = 90
else
angle = 315-(cpu*16/45);
``````

Thank you for the screenshot in reply #5

Similar to @ wildbill proposal but different equation!

``````if (cpu <50)//0<=cpu < 50%
angle = 315-(4.5*cpu);
else  //50<=cpu <=100%
angle = (2.7*cpu)-45;
``````

hope that helps...

edit ur a legend bill
got it going.. works great
couldnt have got there without help
ended up using

``````if (cload > 16)
angle = 315;
else
``````

hmm.. looks like the right direction... i never saw the map function when researching this.
but i get unexpected results..
this is how i implemented your solutions..

``````/sherzaad
if (cload <50)//if less that 50%
else

//wild bill
//  angle = 90;
//else

myNex.writeNum("ctem.val", ctem);
myNex.writeNum("ctem2.val", ctem);
myNex.writeNum("gtem.val", gtem);
myNex.writeNum("gtem2.val", gtem);
myNex.writeNum("gclock.val", gclock);
//  myNex.writeNum("gmem.val", gmem);
//  myNex.writeNum("sclock.val", sclock);
myNex.writeNum("cclock.val", cclock);
``````

output the following

note cload2.val is the numerical cpu usage percent
and cload.val is the numerical degrees value going to the gauge

``````wild bill