Go Down

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

Drew Davis

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 posotion
base.write(s1); // write servo to position





 
}

Delta_G

http://arduino.cc/en/Reference/Map

TanHadron

#2
Aug 01, 2013, 10:31 am Last Edit: Aug 01, 2013, 10:34 am by TanHadron Reason: 1
s2 = 265 - s2;

Drew Davis

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

Code: [Select]


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


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

if (s2 > 170)

{  s2 = 170; }

Go Up