Controlling stepper motor with push button and limit switches

Hello everyone , merry christmas and happy new year.

I'm working on a very little project with little knowledge and now I'm stuck.
I want to control a stepper motor within two limit switches and get start command by a button. I've found a example code from here (Using BIG Stepper Motors with Arduino | DroneBot Workshop) and do the wiring and changed the code a bit.

Here is the code I'm using

/*
  Stepper Motor Test
  stepper-test01.ino
  Uses MA860H or similar Stepper Driver Unit
  Has speed control & reverse switch
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/

// Defin pins

int reverseSwitch = 2;  // Limitswitch Home
int reverseSwitch2 = 3;  // Limitswitch END
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer

// Variables

int pd = 500;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

  setdir = !setdir;
  
}


void setup() {

  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}

void loop() {
  
    if (digitalRead(reverseSwitch) == LOW && digitalRead(reverseSwitch2) == HIGH)    
    {
    action1();
    }
    if (digitalRead(reverseSwitch) == HIGH && digitalRead(reverseSwitch2) == LOW)    
    {
      digitalWrite(reverseSwitch, HIGH);
    action2();
    }
  
    else
 {
  digitalWrite(driverPUL, LOW); // stop the motor
 }
}
 void action1() {
  pd = map((analogRead(spd)),0,1023,2000,50);
    digitalWrite(driverDIR,HIGH);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 }
 void action2() {
  
    digitalWrite(driverDIR,LOW);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(500);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(500);
 }

The code works but not the way I want. When I press one switch the motor rotates one way while I keep pressing. If I let go the switch It stops.

I want my code to work like this

-Press start button
-Check limit switch, if machine at the end switch, move to home switch at a constant speed and stop when hit at home switch. Wait for button press to start again

-If machine at home switch, move to end switch at speed from potentiometer and stop when hit at home switch. Wait for button press to start again

Thanks for all the help in advance.

Did you mean "and stop when hit end switch" there?

Have you thought about what should happen if the start button is pressed when the "machine" (whatever that is) is not at home switch or end switch?

Except for those 2 things, what you want sounds ok. Make an attempt and if you are still stuck, post your attempt.

yes, stop when hit the switch.

if the machine is somewhere between switches, machine should move to end stop at potentiometer speed.

  1. Get rid of the interrupt. You aren't using it.
  2. How are your limit switches wired? Do you have a pullup or pulldown resistor? You should explicitly configure those inputs in setup. Recommend using INPUT_PULLUP and wiring limit switches between pin and GND.
  3. What is the purpose of digitalWrite(reverseSwitch, HIGH)? For inputs that enables the internal pullup resistor. Probably not doing what you think.
  4. Recommend renaming action1() and action2() functions to something more meaningful.

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