Pages: [1]   Go Down
Author Topic: Flipping values  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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





 
}
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

s2 = 265 - s2;
« Last Edit: August 01, 2013, 03:34:49 am by TanHadron » Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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


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

if (s2 > 170)

{  s2 = 170; }
Logged

Pages: [1]   Go Up
Jump to: