Servo motor and limit switch

Hello everyone, im new to coding so i have a lot of questions. Im doing a work where i need to do a sliding gate with servo motor, 1 button and the limit switches.
My servo is a continuous servo and i want to put 2 limit switches at 2 ends of the gate. I already have code where the servo works when i click a button. If anyone can help me on the code for the 2 limit switches i would be gratefull.

My code is this:

#include <Servo.h>
Servo motor1;
int cont = 1; // a variable that counts the button clicks
void setup() {
  pinMode(4, INPUT); // button in pin 4
  pinMode(5, INPUT); //limit switch 1
  pinMode(6, INPUT); //limit switch 2
  motor1.attach(9); // motor on pin 9
  motor1.write(96); // this number, 96 is where my servo is stopped, remember this is continuous 
}

void loop() {
  if (digitalRead(4) == 1) { // if button is presses
    cont++; //  +1 variable cont
    delay(300); // 
  }
  if (cont == 5) {
    cont = 1; // 
  }

  if (cont == 1) {
    motor1.write(96); //servo starts stopped
    delay(50);
  }
  else if (cont == 2) {
    motor1.write(180);//servo rotates clockwise when i click the button
    delay(50);
  }
  else if (cont == 3) { 
    motor1.write(96); //servo stops again when i click the button
    delay(50);
  }
  else if (cont == 4) {
    motor1.write(0); //servo rotates anticlockwise when i click the button
    delay(50);

//after this its a loop when i click again it stops and starts again from cont == 1

No one can help with the switch coding until you show the type of switch and how each is wired.

Yo. Welcome to the forum.

You have the beginnings of the code you need, and so far it shouldn't be too hard to start adding stuff to it.

Like when you have conted up to 4, and the servo is making something head towards a limit switch,
you can put in that same if statement a test to see if the limit switch got hit, and just by setting cont to one stop the mechanism.

And the other limit switch the same. That might be fun for awhile, but you will see limitations to your approach. But have some fun and see for yourself.

As for exact code, @Paul_KD7HB points out that without knowing how you've wired the buttons, no one can begin helping.

I would also like to know more about your mechanical concept. What is the servo motor moving, is a real gate or a little toy gate or what? Snap a picture and show us what you up to.

It wouldn't be too soon to say what all in your wildest dreams or maybe just regular dreams you hope to accomplish with this project.

a7

My post went on the wrong thread. Sorry.

look this over

#include <Servo.h>

const byte PinBut       = 4;
const byte PinLmtOpened = 5;
const byte PinLmtClosed = 6;

Servo motor1;

#define Open    100
#define Stop    96
#define Close   0

// -----------------------------------------------------------------------------
void loop ()
{
    if (LOW == digitalRead (PinBut))  {
        Serial.println ("start");
        if (LOW == digitalRead (PinLmtClosed)) {        // currently closed
            motor1.write (Open);                        // open
            while (HIGH == digitalRead (PinLmtOpened))  // wait until opened
                ;
            motor1.write (Stop);                        // stop
        }

        else if (LOW == digitalRead (PinLmtOpened)) {   // currently open
            motor1.write (Close);                       // close
            while (HIGH == digitalRead (PinLmtClosed))  // wait until closed
                ;
            motor1.write (Stop);                        // stop
        }
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (PinBut,       INPUT_PULLUP);
    pinMode (PinLmtOpened, INPUT_PULLUP);
    pinMode (PinLmtClosed, INPUT_PULLUP);

    motor1.attach (9); // motor on pin 9
    motor1.write (96); // this number, 96 is where my servo is stopped, remember this is continuous
}

Thanks for the reply!
My limit switch is normally open, i will put a print of the example, i use the common and NO, i just dont know how to put it on the code

image

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

if the limit switch is wired as i describe, it will be HIGH until depressed

Take a view here to gain the knowledge:

i put this code after the last else if, when the gate touch the limit switch, the servo stopped, but now i want to when i click the button, it starts moving to the other side

while (digitalRead(5) == HIGH) {
    motor1.write(96);
  }

what about the above part?

a common problem is that pressing the button to restart the motor doesn't do anything because the limit switch is depressed. so there need to be separate limit switches and they need to be checked to determine in which direction to restart the motor

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