Advertisement
CuriousScientist

Code Snippet for AccelStepper Oscillator

Apr 4th, 2020 (edited)
831
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/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial video: https://youtu.be/AR0un3kg-iM
  3.  
  4. /*
  5. Please consider buying the parts using the following links in order to support my work:
  6. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  7. TB6600 Stepper Driver: https://www.banggood.com/custlink/KvvvZr1Pmj
  8. Variable Power Supply: https://www.banggood.com/custlink/DGKvSpwFvQ
  9. NEMA17: https://www.banggood.com/custlink/vGK3ic1tm2
  10. */
  11. //This is a code snippet which can be added to the following PasteBin: https://pastebin.com/GF7ANksF
  12. //It moves between 0 and 3000 step positions with 5000 step/s speed.
  13. //It oscillates between 0 and 3000 until the user interrupts it.
  14. //The code is blocking, so it can be stopped only after one of the 3000 steps are completed.
  15.  
  16.  
  17. while(!Serial.available())
  18.         {
  19.         stepper.moveTo(3000); //Define a target position
  20.        
  21.         while(stepper.distanceToGo() != 0) //Move the motor until there are no steps left (this blocks the code)
  22.         {          
  23.           stepper.runToPosition();          
  24.         }
  25.        
  26.         if(Serial.read() == 'n') break; //Dirty solution to break out from the while
  27.         Serial.println(stepper.currentPosition()); //this should print 3000
  28.      
  29.         stepper.moveTo(0); //Define a target position
  30.        
  31.         while(stepper.distanceToGo() != 0) //Move the motor until there are no steps left (this blocks the code)
  32.         {          
  33.           stepper.runToPosition();        
  34.         }
  35.         Serial.println(stepper.currentPosition()); //This should print 0        
  36.  
  37.         if(Serial.read() == 'n') break; //Dirty solution to break out from the while
  38.         }
  39.  
  40. //Notes: This is an ugly code and it is only for some demonstration. It works, but it could be improved.
  41. //The best practice would be to avoid the main while loop and let the Arduino code's loop() iterate.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement