Limit Switches and Servo Motor For Home Project

Hey guys!

I am currently building a platform to take my panoramic videos. I am using a clear path servo motor and attaching two limit switches to tell it to reverse the direction. Here is what I have so far. I cannot even get the motor to start. It's been quite some time since I have dabbled with arduino and coding so I apologize for the terrible code. (if it is bad)

cam_slider_1.1.ino (2.1 KB)

#include <PulseClearpath.h>
#include <StepController.h>
#include <StepClearpath.h>

StepClearpath X; // initialize a StepClearpath Motor

StepController machine(&X);  //initialize the controller and pass the reference to the motor we are controlling

boolean IsSDHPmodel=false;
boolean IsSDSKmodel=true;
int limitL = 11;
int limitR = 12;
int stage1 = 0;
int stage2 = 0;
int num1 = 0;

double TotalDistance=20;     // distance in Steps - check step/rev in ClearPath MSP config file (1,600 steps/rev)  

void setup(){      
    pinMode(2,OUTPUT);  //DEBUG declare pin 2 as output.  Abe, this shows when the arduino is in the ISR
    pinMode(limitL, INPUT_PULLUP);
    pinMode(limitR, INPUT_PULLUP);
    Serial.begin(9600);
    X.attach(8,9,6,4);      //Direction/A is pin 8, Step/B is pin 9, Enable is pin 6, HLFB is pin 4  //JWS: HFLB = high level feedback info coming from motor to arduino
    X.setMaxVel(500);  // Set max Velocity.  Parameter can be between 2 and 100,000
    X.setMaxAccel(5000);  // Set max Acceleration.  Parameter can be between 4000 and 2,000,000
    X.enable();  // Enable motor, reset the motor position to 0 //JWS: enable happens automatically per Clearpath (whenever enabled) - no actual arduino code required
  
    Serial.print("Homing...");      // wait until the command is finished and then 1 more second
    while(X.readHLFB()) { 
        Serial.print('z');
      }
    
    Serial.println();
    Serial.println("Homing Complete");
    delay(200);
  
    machine.Start(249);     // Set up the ISR to constantly check motor position.  PARAMETER MUST BE SET TO 249

  }

void loop (){
  

  if (limitR == HIGH && num1 == 0 ){
    stage1 = 1;
    Serial.println("limit2");
    num1 = 1;}
    
  if (limitL == HIGH && num1 == 0){
    stage2 = 1;
    Serial.println("limitL");
    num1 = 1;
    }

  if (stage2 = 1){
      X.move(TotalDistance);
      if (limitR, HIGH){
        stage1 = 1;
        stage2 = 0;
        Serial.println("stage2");
      }
  }
    if (stage1 = 1){
      X.move(-TotalDistance);
      if (limitL, HIGH){
        stage1 = 0;
        stage2 = 1;
        Serial.println("stage1");
      }
  }
}

OP code so that we don't have to down load it. Read the how to use this forum sticky to see how to post code.

What does the code do? How does that differ from what you want?

Include links to libraries that do not come with the IDE. And a schematic might be handy. Be sure to include all power supplies.

  if (stage2 = 1)

Whoops

double TotalDistance=20;     // distance in Steps - check step/rev in ClearPath MSP config file (1,600 steps/rev)

Read that comment carefully. Then, explain why you are using a double to hold an integral value.