Go Down

### Topic: Flipping values (Read 525 times)previous topic - next topic

#### drewdavis

##### Aug 01, 2013, 05:02 am
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!

Code: [Select]
`#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 = analogRead(A0); // read the sensor for the base   s2 = analogRead(A1); // read the sensor for the arm   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 posotionbase.write(s1); // write servo to position  }`

#### Delta_G

#1
##### Aug 01, 2013, 05:37 am
http://arduino.cc/en/Reference/Map
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#2
s2 = 265 - s2;

#### drewdavis

#3
##### Aug 01, 2013, 03:08 pm
Thanks! I ended up using the map() function and it worked perfectlyâ€¦

Code: [Select]
`s2 = analogRead(A1); // read the sensors2 = map(s2, 0, 1023, 235, 100); //mapif (s2 > 170) {  s2 = 170; }`

Go Up