Programming Stop and limit switches, servo motors

Hi everyone,

I am currently trying to program a servo motor to rotate clockwise, counter clockwise and stop when it hits a limit switch. I have got it to rotate clockwise and counter clockwise but cannot figure out how to stop it when it hits the limit switch. I also have to program a stop button but can only seem to get the servo to stop at 58 rather than stop it in its current position. I have attached my code below.
Any help would be much appreciated.

Thanks

CODE:
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int servoPin = 9;// this pin must be of those with PWM ~

#define STOPpin 3 // push button pin for STOP
#define CWpin 2 // push button for CW
#define CCWpin 4 // push button for CCW

int sc[]={180, 58, 0};// servo commands are in order
//CCW, STOP,CW

String scText[]={"CCW","Stop","CW"};// define texts for 3 action
int statusText;
int CWBS, CCWBS, SBS;
//CW button status (CWBS)
//CCW button status (CCWBS)
//stop button status (SBS)

void setup() {
Serial.begin(9600);
pinMode(STOPpin,INPUT_PULLUP);// set pin for push button STOP
pinMode(CCWpin,INPUT_PULLUP);// set pin for push button CCW
pinMode(CWpin,INPUT_PULLUP);// set pin for push button CW

myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
myservo.write(sc[1]);// send STOP command
statusText=1;// initial value is STOP
}

void loop() {

CCWBS = digitalRead(CCWpin);// read status of button CCW
SBS = digitalRead(STOPpin);// read status of button STOP
CWBS = digitalRead(CWpin);// read status of button CW
    
if(CCWBS ==LOW)
{
  servoCommand(0);
}else if(SBS ==LOW)
{
  servoCommand(1);
}else if(CWBS ==LOW)
{
  servoCommand(2);
}

Serial.println(scText[statusText]);
delay(50);
}// loop

void servoCommand(int n)
{
statusText = n;
myservo.write(sc[n]);
Serial.print("Going to ");
Serial.print(scText[n]);
Serial.print( "(");
Serial.print(sc[n]);
Serial.println(")");
}

intresting

Help us help you.

  • what triggers any motion when stopped? isn't something needed (a start button) to override the limit switch?
  • in general, shouldn't each switch select the next position? when it hits the CCW limit switch, shouldn't the next position be the center position?

if you want a "stop" button that immediately stops the servo, the servo needs to be advanced one position at a time allowing the "stop" button to be monitored between positions.

Apologies for the poor description of the problem, hopefully I have explained it better below.
I am currently using ARDUINO UNO. yes this is a simulator code. I have attached photo below.

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

A servo is sent a value and will try to torque to that value. A limit switch can send a signal to the MCU that the servo has reach such and such a position, but the servo will still attempt to keep driving to the programmed position. How are the limit switches expected to work?

Will the limit switches remove servo power? If servo power is removed how will the servo then be driven back to it's 'normal' postion?

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Those servos are getting their power from the Uno?

This is what i am unsure of, i am new to arduino and was looking some assistance what would be the best way to program this.

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You are using 12V servo's?

No, 5V

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

What's the goal?

Servos go to a commanded position and then stop, don't send the servo to a position beyond where you want it to go.

Your image from post#11 shows motors being run from 12 V. Not the way to do a servo.


What I do to setup a servo is setup a servo test program to find the actual hardware limits of the servo. For instance send the servo to 1500uS to find the center. Then send the servo 500uS (0 degrees) to find one end and 2500uS (180 degrees) to find the other end. Now find the actual limits by sending the servo 500uS, wait, then send the servo 600uS. Did it move from its position? If not 0 degrees has not been found. Up the uS number a bit till an actual number for 0 degrees is found. Do the same with 180 degrees. When you find the 0 degree and 180 position, you've found the servo's limits. In software don't command the servo to go beyond the servo's established limits; simple.

Don't forget to mark the center position.

would you mind responding to my queries in post #4

the limit switches are supposed to act as a safety feature if the servo goes over its end position

I see your not getting it. Sorry, I was not more of a help. Anyways Good luck.

  • what triggers any motion when stopped? isn't something needed (a start button) to override the limit switch? : The clockwise and counter clock wise buttons are used to start the servo when stopped im assuming this would work?

  • in general, shouldn't each switch select the next position? when it hits the CCW limit switch, shouldn't the next position be the center position? : could the next position not be the CW limit switch too? it seems that that is the route i will have to go.

Shannon I think i get wat you are saying, i will needed to set the limit switches either side before the servo reaches its limit as a safety mechanism.