Making a number bigger when its small, and smaller when big

Question:

i have been coding in python for some time now, and am trying to make a robotic hand in combination with Arduino. I have 2 servo’s facing the opposite way (small electric motors) which means that when i rotate them by the same angle, the second one will rotate the opposite way. As the servo can’t handle negative integers, i have to come with a solution that basically "reverses" the integer. What i mean by that is that if the number is greater than the middle point (45 in this case) i want it to be smaller, so lets say we have 46 it should be 44 and 47 -> 43 so on. How would i go about creating this? Thanks for reading.

Asked By: Programmer_Tom

||

Answers:

This is just a tricky algorithm:

edge = 45
number = 44
result = edge + (edge - number)

result will be 46

edge = 45
number = 47
result = edge + (edge - number)

result will be 43

Answered By: Mehrdad Pedramfar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.