How to Stepper Motor Home Position and Loop Control with Push Button

          


            CODE 

#include <Stepper.h>

const int stepsPerRevolution = 5;

#define HALL_SENSOR   3

Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);

Stepper stepper1(stepsPerRevolution, 10, 11, 12, 13);

// Direction Variable

boolean setdir = LOW;


void homefunction() {

  // Set motor speed pulse duration

  int pd = 4000;

  

  // Move motor until home position reached

  while (digitalRead(HALL_SENSOR) == 1) {


    // Step the motor through a sequence (full step sequence)

    if (setdir == LOW) {

      Serial.println("counterclockwise");

      myStepper.step(-stepsPerRevolution);

      delay(0);

      } else {

     

    }

    

    delayMicroseconds(pd);  // Delay to control motor speed

  }

}


void setup() {

   myStepper.setSpeed(5000);

   stepper1.setSpeed(5000);  

 // Setup the Hall Effect switch as an Input

  pinMode(HALL_SENSOR, INPUT);

  

  // Home the motor

  homefunction();

  pinMode(8,INPUT);

  digitalWrite(8, HIGH);

}


void loop() {

  // Loop, put code here (motor control or other logic)

  int data = digitalRead(8);

  if(data==LOW){

  // step one revolution  in one direction:

   Serial.println("clockwise");

   const int stepsPerRevolution = 300;

   stepper1.step(stepsPerRevolution);

   delay(500);

   // step one revolution in the other direction:

  Serial.println("counterclockwise");

  stepper1.step(-stepsPerRevolution);

  delay(500);

  }

  else{

  }

  }

 

Post a Comment

Previous Post Next Post

Contact Form