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?

gauge-for-temperature-md.png

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);

|500x167

Thank you for the screenshot in reply #5

Similar to @ wildbill proposal but different equation! :wink:

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 = map(cload,16,100,0,225);
else if(cload==0)
angle = 315;
else
angle = map(cload,1,15,316,360);

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%
  angle = 315-(4.5*cload);
else 
  angle = (2.7*cload)-45;

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

  myNex.writeNum("ctem.val", ctem);
   myNex.writeNum("ctem2.val", ctem);
      myNex.writeNum("cload2.val", cload);
   myNex.writeNum("cload.val", angle);
   myNex.writeNum("gtem.val", gtem);
   myNex.writeNum("gtem2.val", gtem);
      myNex.writeNum("gload2.val", gload);
   myNex.writeNum("gload.val", gload);
   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


ctem.val=41⸮⸮⸮ctem2.val=41⸮⸮⸮cload2.val=8⸮⸮⸮cload.val=313⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=40⸮⸮⸮ctem2.val=40⸮⸮⸮cload2.val=3⸮⸮⸮cload.val=314⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=40⸮⸮⸮ctem2.val=40⸮⸮⸮cload2.val=5⸮⸮⸮cload.val=314⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=7⸮⸮⸮gload.val=7⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=40⸮⸮⸮ctem2.val=40⸮⸮⸮cload2.val=7⸮⸮⸮cload.val=313⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=53⸮⸮⸮ctem2.val=53⸮⸮⸮cload2.val=11⸮⸮⸮cload.val=312⸮⸮⸮gtem.val=31⸮⸮⸮gtem2.val=31⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=1500⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=60⸮⸮⸮ctem2.val=60⸮⸮⸮cload2.val=28⸮⸮⸮cload.val=36⸮⸮⸮gtem.val=30⸮⸮⸮gtem2.val=30⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=67⸮⸮⸮ctem2.val=67⸮⸮⸮cload2.val=82⸮⸮⸮cload.val=200⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=68⸮⸮⸮ctem2.val=68⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=255⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=67⸮⸮⸮ctem2.val=67⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=255⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=68⸮⸮⸮ctem2.val=68⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=255⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=50⸮⸮⸮ctem2.val=50⸮⸮⸮cload2.val=93⸮⸮⸮cload.val=233⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=44⸮⸮⸮ctem2.val=44⸮⸮⸮cload2.val=5⸮⸮⸮cload.val=314⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=43⸮⸮⸮ctem2.val=43⸮⸮⸮cload2.val=9⸮⸮⸮cload.val=312⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=3⸮⸮⸮gload.val=3⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮







sherzaad



ctem.val=43⸮⸮⸮ctem2.val=43⸮⸮⸮cload2.val=9⸮⸮⸮cload.val=274⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=2⸮⸮⸮gload.val=2⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=42⸮⸮⸮ctem2.val=42⸮⸮⸮cload2.val=3⸮⸮⸮cload.val=301⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=41⸮⸮⸮ctem2.val=41⸮⸮⸮cload2.val=3⸮⸮⸮cload.val=301⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=42⸮⸮⸮ctem2.val=42⸮⸮⸮cload2.val=5⸮⸮⸮cload.val=292⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=6⸮⸮⸮gload.val=6⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=43⸮⸮⸮ctem2.val=43⸮⸮⸮cload2.val=4⸮⸮⸮cload.val=297⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=4⸮⸮⸮gload.val=4⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=45⸮⸮⸮ctem2.val=45⸮⸮⸮cload2.val=9⸮⸮⸮cload.val=274⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=4⸮⸮⸮gload.val=4⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=55⸮⸮⸮ctem2.val=55⸮⸮⸮cload2.val=8⸮⸮⸮cload.val=279⸮⸮⸮gtem.val=31⸮⸮⸮gtem2.val=31⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=1500⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=57⸮⸮⸮ctem2.val=57⸮⸮⸮cload2.val=33⸮⸮⸮cload.val=166⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=68⸮⸮⸮ctem2.val=68⸮⸮⸮cload2.val=82⸮⸮⸮cload.val=176⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=66⸮⸮⸮ctem2.val=66⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=225⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=68⸮⸮⸮ctem2.val=68⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=225⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=67⸮⸮⸮ctem2.val=67⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=225⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=68⸮⸮⸮ctem2.val=68⸮⸮⸮cload2.val=100⸮⸮⸮cload.val=225⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=1⸮⸮⸮gload.val=1⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮ctem.val=44⸮⸮⸮ctem2.val=44⸮⸮⸮cload2.val=63⸮⸮⸮cload.val=125⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=5⸮⸮⸮gload.val=5⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮
ctem.val=42⸮⸮⸮ctem2.val=42⸮⸮⸮cload2.val=3⸮⸮⸮cload.val=301⸮⸮⸮gtem.val=29⸮⸮⸮gtem2.val=29⸮⸮⸮gload2.val=0⸮⸮⸮gload.val=0⸮⸮⸮gclock.val=300⸮⸮⸮cclock.val=4900⸮⸮⸮

so when cload2 is 100 cload should be 225
and for cload2 is 50 should be 90
i think bill is closer… dividing at 16 and doing map on either one

thanks for all the help guys, here is the final result.... just waiting on response from nextion about the second needle being buggy but im 99% there now

|500x333