CuriousScientist

Accelstepper and 2 limit switches with interrupts

Jul 12th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to this tutorial video: https://youtu.be/usYdMPWf7xw
  3.  
  4. #include <AccelStepper.h> //accelstepper library
  5.  
  6. const byte limitSwitch_1 = 2; //pin for the microswitch using attachInterrupt()
  7. const byte limitSwitch_2 = 3; //pin for the microswitch using attachInterrupt()
  8.  
  9. bool switchFlipped = false; //stores the status for flipping
  10. bool previousFlip = true; //stores the previous state for flipping - needed for the direction change
  11.  
  12. // direction Digital 9 (CCW), pulses Digital 8 (CLK)
  13. AccelStepper stepper(1, 8, 9);
  14.  
  15. void setup()
  16. {
  17.   //Limit Switches
  18.   pinMode(limitSwitch_1, INPUT_PULLUP); // internal pullup resistor (debouncing)
  19.   pinMode(limitSwitch_2, INPUT_PULLUP); // internal pullup resistor (debouncing)
  20.  
  21.   attachInterrupt(digitalPinToInterrupt(limitSwitch_1), FlipDirection, FALLING);   //do not change it to 'CHANGE'
  22.   attachInterrupt(digitalPinToInterrupt(limitSwitch_2), FlipDirection, FALLING);
  23.   //---------------------------------------------------------------------------
  24.  
  25.   //Serial Communication
  26.   Serial.begin(9600); //defining some baud rate
  27.   Serial.println("Testing Accelstepper"); //print a message
  28.   //---------------------------------------------------------------------------
  29.  
  30.   //Stepper parameters
  31.   //setting up some default values for maximum speed and maximum acceleration
  32.   stepper.setMaxSpeed(5000); //SPEED = Steps / second  
  33.   stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  34.   stepper.setSpeed(1500);
  35.   delay(500);
  36.   //---------------------------------------------------------------------------
  37.  
  38. }
  39.  
  40. void loop()
  41. {
  42.  
  43.   stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  44.   flipCheck();   //checking the flip in each loop
  45. }
  46.  
  47. void flipCheck()
  48. {
  49.   if(switchFlipped == true)
  50.   {    
  51.      //Serial.println(previousFlip); //This was just a control flag for debugging
  52.    
  53.      if(previousFlip == true) //If the previous flip is 1, we have positive direction
  54.      {    
  55.         stepper.setSpeed(1500);      
  56.      }
  57.      if(previousFlip == false) //If the previous flip is 0, we have negative direction
  58.      {  
  59.        stepper.setSpeed(-1500);
  60.      }
  61.      switchFlipped = false;
  62.     //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
  63.   }
  64.  
  65. }
  66.  
  67. void FlipDirection()
  68. {    
  69.   switchFlipped = true; //we change the status to true, so the code will enter the flipCheck() function
  70.   previousFlip = !previousFlip; //change the state to different from the previous - this controls the direction
  71.  
  72. }
  73.  
  74. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  75. //The code belongs to this tutorial video: https://youtu.be/usYdMPWf7xw
Add Comment
Please, Sign In to add comment