Ping Pong with the AccelStepper library and two limit switches - Polling and interrupts

In this video I show you two ways of using 2 limit switches to bounce the stepper motor-moved linear carriage between some arbitrary positions. I show you how to set up everything and I show you and explain both software.



Arduino source code

Interrupt based code

#include <AccelStepper.h> //accelstepper library

const byte limitSwitch_1 = 2; //pin for the microswitch using attachInterrupt()
const byte limitSwitch_2 = 3; //pin for the microswitch using attachInterrupt()

bool switchFlipped = false; //stores the status for flipping
bool previousFlip = true; //stores the previous state for flipping - needed for the direction change

// direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 8, 9);

void setup()
{
  //Limit Switches
  pinMode(limitSwitch_1, INPUT_PULLUP); // internal pullup resistor (debouncing)
  pinMode(limitSwitch_2, INPUT_PULLUP); // internal pullup resistor (debouncing)
  
  attachInterrupt(digitalPinToInterrupt(limitSwitch_1), FlipDirection, FALLING);   //do not change it to 'CHANGE'
  attachInterrupt(digitalPinToInterrupt(limitSwitch_2), FlipDirection, FALLING); 
  //---------------------------------------------------------------------------

  //Serial Communication
  Serial.begin(9600); //defining some baud rate
  Serial.println("Testing Accelstepper"); //print a message
  //---------------------------------------------------------------------------

  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper.setSpeed(1500);
  delay(500);
  //---------------------------------------------------------------------------

}

void loop()
{
  
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  flipCheck();   //checking the flip in each loop
}

void flipCheck()
{
  if(switchFlipped == true)
  {    
     //Serial.println(previousFlip); //This was just a control flag for debugging
    
     if(previousFlip == true) //If the previous flip is 1, we have positive direction
     {    
        stepper.setSpeed(1500);       
     }
     if(previousFlip == false) //If the previous flip is 0, we have negative direction
     {  
       stepper.setSpeed(-1500);
     }
     switchFlipped = false; 
	//We have to reset this, so in the next iteration of the loop, the code will not enter this part, only when there was a click again
  }
 
}

void FlipDirection()
{    
  switchFlipped = true; //we change the status to true, so the code will enter the flipCheck() function 
  previousFlip = !previousFlip; //change the state to different from the previous - this controls the direction
 
}

Polling based code

#include <AccelStepper.h> //accelstepper library

const byte limitSwitch_1 = 2; //pin for the microswitch using attachInterrupt()
const byte limitSwitch_2 = 3; //pin for the microswitch using attachInterrupt()

bool switchFlipped = false; //stores the status for flipping
bool previousFlip = true; //stores the previous state for flipping - needed for the direction change

//I made these variables while recording the tutorial video, watch it to understand it
int switchCounter; 
int newSpeed;
//------------------------------------------------------------------------------------

// direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 8, 9);

void setup()
{
  //Limit Switches
  pinMode(limitSwitch_1, INPUT_PULLUP); // internal pullup resistor (debouncing)
  pinMode(limitSwitch_2, INPUT_PULLUP); // internal pullup resistor (debouncing) 
  //---------------------------------------------------------------------------

  //Serial communication
  Serial.begin(9600); //defining some baud rate
  Serial.println("Testing Accelstepper"); //print a message
  //---------------------------------------------------------------------------

  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper.setSpeed(2000);
  delay(500);
  //---------------------------------------------------------------------------

}

void loop()
{  
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  flipCheck();   //checking the flip at each loop
}

void flipCheck() //this function constantly polls the pins. It is not the most efficient practice...
{
  if(digitalRead(limitSwitch_1) == LOW)
  {  
	//I made this part while recording the tutorial. Watch the video to understand why
    //switchCounter++;
    //newSpeed = -1 * (2000 + (switchCounter * 200));
	//example: the newSpeed will be -2400 if the switchCounter is 2
    stepper.setSpeed(newSpeed);      
  }

  if(digitalRead(limitSwitch_2) == LOW)
  {     
    stepper.setSpeed(2000); 
  }      
}

Previous
Previous

MCP41100 digital potentiometer with Arduino/STM32

Next
Next

Let's discuss about Peltier coolers #3 - Cooling the hot side