Setting Min and Max rotation servo motor

Hi, I have a project for tracking objects using servo motors ( Arduino ) and python.
I want to set a rotation limit for my 2 servos between ( 60 to 120 ) degree anlge
Minimum 60 degree
maximum 120 degree

Any help will be appreciated. Thanks

What have You achieved so far regarding circuitry and code?

#define AngleMax 120
#define AngleMin 60

So is that 120-60 = 60 degrees overall from 60 degree position to 120 degree position.

120 is the maximum overall in many servos anyhow.
Cannot follow your requirement.
Perhaps a drawing of what you are trying to achieve.

Oops2

Corrected now..... Anyway, checking that people are not sleeping.....

1 Like

You might consider const byte (or int) instead of #define too.

1 Like

No. Declaring an int occupies memory. #declare just instructs the compiler and no memory is occupied after compilation.

Maybe, maybe not.
(anyway, it's got to occupy some memory)

Don't try to outguess the compiler.
("const byte" != "int")

Even if that were true (not near a computer to test), I would not give up compile time checking to do premature optimization, it's the root of all evil :wink:

1 Like

I am (for once)

Enjoy

#include <Servo.h>

#if 1
  #if 0
    #define MaxServo 160
    #define MinServo 20
  #else
    int MaxServo = 160;
    int MinServo = 20;
  #endif
#else
  #if 0
    const byte MaxServo = 160;
    const byte MinServo = 20;
  #else
    const int MaxServo = 160;
    const int MinServo = 20;
  #endif
#endif

Servo foobar;

void setup() 
{
  foobar.attach (2);
}

void loop() 
{
  foobar.write (MaxServo);
  delay (500);
  foobar.write (MinServo);
  delay (500);
}

Yes, too quick to reply.

Hey! You're talking about microseconds. That argument is below Your standard.

I'm not talking about PC memory, I'm talking about an immediate literal as part of an instruction (program memory) vs. RAM or even a non-immediate constant.

I'm talking about checks that happen during compilation, not elapsed time.

Use of the pre processor in this case deprives the compiler of information it could use to help highlight mistakes.

In one way a buy that but wherever that define, or constant is used, it's needed in the code, program memory. What's the most common shortage, program memory or RAM? You likely know more than me.

Me too.

Can You elaborate, giving an example? I want to write code that will not make the compiler issue warnings or worse.

@TheMemberFormerlyKnownAsAWOL

It took me some time to catch some kind of a headline suggesting I don't like to be corrected..... Was it my mistake ending the #defines ending with ;? That shows quickly in a compilation, but, yes, I was mistaken.

@dulyta

If you want to limit servo travel, the simplest method is to never tell it to go beyond some limits set in the software.

Place a check on all writing to the servo if your other code can’t be fixed to stop telling the servo to go beyond the limits you want to impose.

It is fashionable and advised to make those limits obvious by placing the numbers that inform them once, at the top of your code, using any of the techniques being discussed here.

So you can refer to them anywhere in your code symbolically.

a7

Not having seen any code:
pos = constrain(pos,60,120);

You certainly do not want to try using end-stops! :astonished:

1 Like

I confess that I have struggled to find an example of #define/const issues for such a simple case. I can find plenty of stuff saying why #define can hurt you but nothing directly applicable to your example. Here's a stack exchange example, even if it isn't really parallel to your posted code:

#include <iostream>

#define x 500
#define y x + 5

int z = y * 2;

int main()
{
    std::cout << "y is " << y;
    std::cout << "\nz is " << z;
}
y is 505
z is 510
#include <iostream>

const int x = 500;
const int y = x + 5;

int z = y * 2;

int main()
{
    std::cout << "y is " << y;
    std::cout << "\nz is " << z;
}
y is 505
z is 1010

Honestly, all it's really saying is "This is why you put parentheses around all the things!".