Servo Limit

Hi All,

I'm making a camera tilt using a servo and an IR receiver... The code I'm using is working well, but I would like to limit the servo form going past certain degrees. For example I need it to operate between 75-110 degrees. I would be very grateful if someone could help with this. Thanks!

Her is my code

/* Version 1.0  Jan 2015 - Credit to groundfungus
 * "IR_Servo" Control a servo using an IR remote
 */
#include <IRremote.h>
#include <Servo.h> 

const byte RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);
Servo myservo; 

byte position = 110; // range = 75-110
boolean newIRdata = false;
unsigned long IRinput;

decode_results results;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results))
  {
    IRinput = results.value;
    irrecv.resume(); // Receive the next value
    newIRdata = true;
  }
  if(newIRdata == true)
  {
   Serial.println(IRinput, HEX);
      switch(IRinput)
    {
     // move right 5 degrees per button push
     case(0xA3C8EDDB): // right arrow
     position-=5;
     Serial.println(position);
     break;
     // move left 5 degrees per button push
     case(0x511DBB): // left arrow
     position+=5;
     Serial.println(position);
     break;
     // go to 110
     case(0x20FE4DBB): // up arrow
     position = 110;
     Serial.println(position);
     break;
     // go to 75
     case(0x52A3D41F): // down arrow
     position = 75;
     Serial.println(position);
     break;     
    }
     IRinput = 0;
     newIRdata = false;   
  }
  myservo.write(position);
  }

You may want to look at the reference documentation for the constrain(...) function.

vaj4088:
You may want to look at the reference documentation for the constrain(...) function.

Or, since it is so easy...

   if(position > 110)
      position = 110;
   if(position < 75)
      position = 75;

in the appropriate places.

Thank you very much!