Need to add 3 buttons to this code help!!!


I Need to add start and enable to this code.

  Runs a Teknic Clearpath motor in step and direction mode, back and forth 
  This example code is in the public domain.
 ** all step/dir pins have to be inserted into pins 8-13 (not including ~enable - enable can go anywhere)!

#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;

double TotalDistance=75000;     // 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
    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(95000);  // Set max Velocity.  Parameter can be between 2 and 100,000
    X.setMaxAccel(1500000);  // 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.println("Homing Complete");
    machine.Start(249);     // Set up the ISR to constantly check motor position.  PARAMETER MUST BE SET TO 249


void loop(){
    Serial.print("Full Move...");
    Serial.println("Move Done");

    // Move the motor back to home
    Serial.print("Return Move...");
    Serial.println("Move Done"); 

In the question title you mention 3 buttons, but in your question you mention start and enable, which sound like the same thing. What exactly do you want to accomplish? Maybe you only want a single button?

Sorry for the confusion.

Basically I have a enable toggle switch and a start push button.

Clearpath needs enable to be high and the start button is only to start the program.

Also the third button is a estop which will be hooked up to the VIN of the arduino.

You might need a bit more design thought than that. What are your buttons enabling and starting? Is any stopping involved at any time. What sort of buttons are you using and how will you connect them?

When you have made your first attempt to add them feel free to post that code and tell us what works and what exactly you want it to do that isn't working.

The Button and Debounce examples in the IDE might help you to get started.


Sounds like the enable switch is a physical power switch, so we don't have to worry about it in the code.

So with start and estop, the code looks something like this?

const int startButton = 2;
const int estopButton = 3;

void setup() {
  pinMode(startButton, INPUT_PULLUP); // No need for external pullup
  pinMode(estopButton, INPUT_PULLUP);

void loop() {
  if (!digitalRead(startButton)) { // If the button was pushed
    delay(50); // Might need some debounce. Can use hardware debounce instead

  if (!digitalRead(estopButton)) {

Keep in mind that for this to work, your code has to be non-blocking. That is, the code in loop() has to be very quick to run, otherwise it may never register your button press, or be delayed.