Control servo motor using limit switch

Hi everyone

I need to control my servo motor using limit switch
when the limit switch is pressed the motor should stop. The limit switch is connected to CN

Can anyone help me with that please?

So what is the problem?

Jomana_A:
connected to CN

CN?

But since a servo's position can be controlled, why don't you just let it approach the finish position one degree at a time, and make a note of the correct number. Then code that number into the sketch and tell it to go there.

arduino_new:
So what is the problem?

I need help with the code
I tried several codes but non of them worked

Jomana_A:
I need help with the code
I tried several codes but non of them worked

What code have you tried? "Doesn't work" is not a problem.

neiklot:
CN?

But since a servo's position can be controlled, why don't you just let it approach the finish position one degree at a time, and make a note of the correct number. Then code that number into the sketch and tell it to go there.

yes that can be done but I should use a limit switch

CN = normally opened

Jomana_A:
I need to control my servo motor using limit switch
when the limit switch is pressed the motor should stop. The limit switch is connected to CN

Well something like this should work, assuming myServo is attach()-ed and you have a variable for the position, say myServoPos...

First put the normally open switch from a pin say limitSwitchPin to ground and:

pinMode(limitSwitchPin, INPUT_PULLUP);

Then:

while (digitalRead(limitSwitchPin)== HIGH) //unpressed
  {
      myServoPos++;
      if (myServoPos > 175) myServoPos=175; //in case it's running right near the end of its travel
      myServo.write(myServoPos);
  }

Btw, what's supposed to happen next? Presumably something will happen that encourages the servo to go back the other way?

Jomana_A:
I need to control my servo motor using limit switch

What sort of servo motor?

In the Arduino world servo usually means one of these

However there are also industrial servos that behave similarly to stepper motors.

...R