looking for a good way to check the greater of two analogs.

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have an 8bit mirco with firmware that needs to look at two values and use the greater from 128 (center). My range is 1 byte and center is 128 of each analog  value I have. So really I need the ABS value but I also need to subtract 128 so that my math is 0 based.

 

Thinking along theses lines but its not working the way Id thought.

 

To avoid byte promotion I cast them to unsigned chars.

if ( (unsigned char) (reportBuffer[X_MAIN_STICK]-128) > (unsigned char) (reportBuffer[X_SECONDARY_STICK]-128) )
{
    mouseX_now = reportBuffer[X_MAIN_STICK];   //xmain is further from center use it.  
}
else 
{
    mouseX_now = reportBuffer[X_SECONDARY_STICK]; //x Secondary is further from center use it. 
}
                

Is there another way to do this?

This topic has a solution.
Last Edited: Mon. May 2, 2022 - 11:34 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Compare the values ...the larger one will be further from the center than the smaller.

 

or do you need to go both sides of 128   (sounds like you do).

 

you don't mention...what if one is at 126 and the other is at 130? what if both are 202?  maybe don't care?

 

Are you trying to avoid abs function?

 

why not if ABS(A -128) > ABS(B-128) 

     A wins!!!

  else 

      B wins!

 

watchout for unsigned variables

 

how about, no worries about sign:

If A < 128  A = 255-A

If B < 128  B = 255-B  ;put both on same positive side

If A > B

   A wins!!!

else

   B wins!!!

 

if the exact center point is a concern  (hard to have a center in an even number of possibilities), you can improve

generically:

if A > 127 A = A - 128  else A = A + 128  

if B > 127 B = B - 128  else B = B + 128  

If A > B

   A wins!!!

else

   B wins!!!

 

   

 

note also you can roll your own:

int myABS(int N)
{
    return ((N<0)?(-N):(N));
}

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. May 2, 2022 - 08:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what if one is at 126 and the other is at 130? 130 should be the greater.

 

 

what if both are 202?  maybe don't care? was going to make it > or = but no does not matter.

 

I'd like not to use to costly of a function, not sure how much f a memory hit ABS is.

 

this l looks neat I will try it.

If A < 128  A = 255-A

If B < 128  B = 255-B  ;put both on same positive side

If A > B

   A wins!!!

else

   B wins!!!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this l looks neat I will try it.

note that 128 gives 128 and so will 127 (255-127=128)  This prevents byte rollover at A=zero (if used 256-A).  So a small price is paid for the simplicity. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

just curious...

 

if 128 = center then if you have one value at 130 ( being +2 ) and one value being at 120 ( being -8 )

is the -8 then still considered bigger than the +2 ( in absolute numbers it is....) ?

 

when both are equal you have a delta of 0. So first check could be on them being equal.....

 

Next check is that you need to see if any or both of the values is/are ' negative'  so lower than 128.

in that case you need to do "128- value" to get the absolute number.

Otherwise you can do "value - 128" to get the absolute number.

Then you have the absolutel number on both values and know if they are positive or negative( in a different register) and then you can go from that.