Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
- //The code belongs to the following tutorial video: https://youtu.be/AR0un3kg-iM
- /*
- Please consider buying the parts using the following links in order to support my work:
- Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
- TB6600 Stepper Driver: https://www.banggood.com/custlink/KvvvZr1Pmj
- Variable Power Supply: https://www.banggood.com/custlink/DGKvSpwFvQ
- NEMA17: https://www.banggood.com/custlink/vGK3ic1tm2
- */
- //This is a code snippet which can be added to the following PasteBin: https://pastebin.com/GF7ANksF
- //It moves between 0 and 3000 step positions with 5000 step/s speed.
- //It oscillates between 0 and 3000 until the user interrupts it.
- //The code is blocking, so it can be stopped only after one of the 3000 steps are completed.
- while(!Serial.available())
- {
- stepper.moveTo(3000); //Define a target position
- while(stepper.distanceToGo() != 0) //Move the motor until there are no steps left (this blocks the code)
- {
- stepper.runToPosition();
- }
- if(Serial.read() == 'n') break; //Dirty solution to break out from the while
- Serial.println(stepper.currentPosition()); //this should print 3000
- stepper.moveTo(0); //Define a target position
- while(stepper.distanceToGo() != 0) //Move the motor until there are no steps left (this blocks the code)
- {
- stepper.runToPosition();
- }
- Serial.println(stepper.currentPosition()); //This should print 0
- if(Serial.read() == 'n') break; //Dirty solution to break out from the while
- }
- //Notes: This is an ugly code and it is only for some demonstration. It works, but it could be improved.
- //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