 # Flipping values

Hello, I’m using some integers in my program but I have run into an issue that I can’t solve. I’m controlling servos and one of them needs to move between 100 & 165 degrees. I’m controlling them off of a joy stick like sensor that simply spits out the analog value of its position. I then take that value and divide by 180. If the out come is less then 100 the program sends to my servo to 165. That part works fine. The issue I have is that if the sensor outputs 101 I want my servo to be at 164. If the sensor out puts 164 I want my servo to be at 101. I hope that makes sense. Basically I need to flip the value. The only way I can think of doing this would be to right an if statement for each number… If(s2 == 164) {s2 = 101} Etc. That would obviously take up a lot of space and I was wondering if there was a better way to do that.

Thanks!

``````#include <Servo.h>

int s1 = 90;
int s2 = 90;

Servo base;
Servo arm1;

void setup() {

base.attach(3); //attach the servo to pin
arm1.attach(5); //attach the servo to pin
}

void loop() {

delay(200);

s1 = s1 * .1759530792; // math (180 /1023 = .1759530792)
s2 = s2 * .1759530792; // math (180 / 1023 = .1759530792)

if(s2 <= 100 )

{s2 = 165;} // make sure arm1 does not go past 165

arm1.write (s2); // write servo to posotion
base.write(s1); // write servo to position

}
``````

s2 = 265 - s2;

Thanks! I ended up using the map() function and it worked perfectly…

``````s2 = analogRead(A1); // read the sensor

s2 = map(s2, 0, 1023, 235, 100); //map

if (s2 > 170)

{  s2 = 170; }
``````