-1

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.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • 4
    There is no [tag:python] in this question. See [ask] and how to create a [mcve]. Edit the question. – Peter Wood May 15 '22 at 13:18
  • Does this answer your question? [How to toggle between two values?](https://stackoverflow.com/questions/10986970/how-to-toggle-between-two-values) – Thierry Lathuille Jul 26 '22 at 09:27

1 Answers1

2

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

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59