Limit switches for servos?

While searching the Internet Arduino code that use limit switches to stop servos before they break something and found it at https://gist.github.com/.../8da30ae5d1358c86681d54270f095b9f and I modified the code for my use:

#include <Servo.h>
#define TURN_TIME 1200
Servo toolheadServo;
int toolheadHome = 0;
boolean toolheadGoingHome = false;
int discContact = 0;
boolean toolheadGoingAway = false;
char receivedChar;
boolean newData = false;
//int potpin = A0;
//int val;

void setup() 
{
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  toolheadServo.attach(9);
  toolheadServo.writeMicroseconds(1500);
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}
void loop() 
{
  //val = analogRead(potpin);
  //val = map(val, 0, 1023, 1000, 2000);
  //toolheadServo.writeMicroseconds(val);
  //delay(15);
  toolheadHome = digitalRead(2);
  discContact = digitalRead(3);
  Serial.print(toolheadHome);
  Serial.println(discContact);
  digitalWrite(13, toolheadHome);
  digitalWrite(13, discContact);
  if (toolheadHome) 
  {
    toolheadServo.writeMicroseconds(1500);
     Serial.print("Welcome home, toolhead.");
  }
  if (discContact) 
  {
    toolheadServo.writeMicroseconds(1500);
     Serial.print("Toolhead arrived at destination DVD");
  }
  recvOneChar();
  showNewData();
}
/*
void toolheadGoHome() {
  // will default to HIGH with pullup (i.e if switch dies then motor should cut out safety)
  if (toolheadHome == 1) {
    // good.
    // stop!
    toolheadServo.detach();
    delay(120);
    toolheadGoingHome = false;
    Serial.print("Welcome home, toolhead.");
    
  } else{
    // send it home.
    
    toolheadServo.attach(9);
    delay(120);
    toolheadServo.write(0);

    toolheadGoingHome = true;
    Serial.print("Toolhead going home.");
  }
}

void  toolheadGoExtend() {
  // will default to HIGH with pullup (i.e if switch dies then motor should cut out safety)
  if (discContact == 0) {
    // drive out until disk contact is made
    toolheadServo.attach(9);
    delay(120);
    toolheadServo.write(180);
    toolheadGoingAway = true;
     Serial.print("Toolhead going away, goodbye");
    
  } else {
    // stop when we hit a disc
    toolheadServo.detach();
    delay(120);
    toolheadGoingAway = false;
     Serial.print("Welcome to the DVD surface, toolhead.");
  }
}

*/
void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}
void showNewData() {
 if (newData == true) {

 Serial.println("---");
 
 if (receivedChar == 'A') {
  Serial.println("toolheadGoHome");
  toolheadServo.writeMicroseconds(1000);
  //toolheadGoHome();
 }

 if (receivedChar == 'B') {
    Serial.println("toolheadGoExtend");
    toolheadServo.writeMicroseconds(2000);
    //toolheadGoExtend();
 }
 
 newData = false;
  
 
 }
}

I didn't install the switches on the robot yet. I thought that limit switches should be on elbow and shoulder joints. Using Serial Monitor to move servos worked but I couldn't get Knob code to work and I commented the code out for now. I have to figure out how to install limit switches on the robot. Any suggestions?

You haven't really said what is wrong, other than "couldn't get it to work". That isn't enough to go on.

Hello
Take a view here to gain the knowledge:

You can refer to this limit switches with stepper motor tutorial

Limit switches should not be needed with servo motors. Your code should limit the position/angle sent to the motor so that they are not damaged.

I have broken a few 3D-printed servo horns. It was too hard for me to limit the position/angle sent to the servos in code. The servos can get warm.

why? test once your maximum ratings, for the servo, for example 7 and 175 and than use a constrain()

  servo.write(constrain(newValue, 7, 175));

What's the question?

My guess is that the .writeMicroseconds(1500); is intended to stop a "continuous rotation servo" (one that spins forever if it is not positioned to 90°). Are your servos "continuous rotation servos"?

The servos were modified to be continuous rotation servos with potentiometers removed from within them and placed on joints on my robot.

Poor info to get 10 posts into the subject. Would have been excellent info to get if it was in post#1

Why not just use a motor?

Advantage: They can move more than 180°.
Disadvantage: You can't control their position, only roughly control their speed and direction.

Do you have potentiometers on the joints of the robot connected to the analog inputs of your Arduino?

I'm building an humanoid robot shown at InMoov – open-source 3D printed life-size robot Two joints in each arm that need continuous rotation servos are similar to linear actuators and two joints have worm gears. The potentiometers are still connected to the circuit boards inside the servos using long wires. Even after centering the servos, they try to break the 3D-printed servo horns because it's hard for me to find values for servo.write and servo.writeMicroseconds to limit the movement of the joints.

Is that because they're co-dependent, i.e. the position of one affects the limits of the other? Or are you trying to fine-tune the limit positions, and running into the resolution limitations of cheap servos?

What about multi-turn servos like the one shown at 2000 Series 5-Turn, Dual Mode Servo (25-2, Torque) - goBILDA ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.