Advertisement
CuriousScientist

Arduino with TB6600 using AccelStepper library

Jul 26th, 2019
2,991
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.  
  12. #include <AccelStepper.h>
  13.  
  14. long receivedMMdistance = 0; //distance in mm from the computer
  15. long receivedDelay = 0; //delay between two steps, received from the computer
  16. long receivedAcceleration = 0; //acceleration value from computer
  17. char receivedCommand; //character for commands
  18. /* s = Start (CCW) // needs steps and speed values
  19.  * o = open (CCW) // needs steps and speed values
  20.  * c = close (CW) //needs steps and speed values
  21.  * a = set acceleration // needs acceleration value
  22.  * n = stop right now! // just the 'n' is needed
  23.  */
  24.  
  25. bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
  26.  
  27.  
  28.  
  29. // direction Digital 9 (CCW), pulses Digital 8 (CLK)
  30. AccelStepper stepper(1, 8, 9);
  31.  
  32.  
  33. void setup()
  34. {
  35.   Serial.begin(9600); //define baud rate
  36.   Serial.println("Testing Accelstepper"); //print a message
  37.  
  38.   //setting up some default values for maximum speed and maximum acceleration
  39.   stepper.setMaxSpeed(2000); //SPEED = Steps / second
  40.   stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
  41.  
  42.   stepper.disableOutputs(); //disable outputs, so the motor is not getting warm (no current)
  43.  
  44.  
  45. }
  46.  
  47. void loop()
  48. {
  49.  
  50.   checkSerial(); //check serial port for new commands
  51.  
  52.   continuousRun2(); //method to handle the motor
  53.  
  54. }
  55.  
  56.  
  57. void continuousRun2() //method for the motor
  58. {
  59.   if (runallowed == true)
  60.   {
  61.     if (abs(stepper.currentPosition()) < receivedMMdistance) //abs() is needed because of the '<'
  62.     {
  63.       stepper.enableOutputs(); //enable pins
  64.       stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
  65.     }
  66.     else //program enters this part if the required distance is completed
  67.     {
  68.      
  69.       runallowed = false; //disable running -> the program will not try to enter this if-else anymore
  70.       stepper.disableOutputs(); // disable power
  71.       Serial.print("POS: ");
  72.       Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps
  73.       stepper.setCurrentPosition(0); //reset the position to zero
  74.       Serial.print("POS: ");
  75.       Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps; we check here if it is zero for real
  76.     }
  77.  
  78.  
  79.   }
  80.   else //program enters this part if the runallowed is FALSE, we do not do anything
  81.   {
  82.     return;
  83.  
  84.   }
  85. }
  86.  
  87. void checkSerial() //method for receiving the commands
  88. {  
  89.   //switch-case would also work, and maybe more elegant
  90.  
  91.   if (Serial.available() > 0) //if something comes
  92.   {
  93.     receivedCommand = Serial.read(); // this will read the command character
  94.     newData = true; //this creates a flag
  95.   }
  96.  
  97.   if (newData == true) //if we received something (see above)
  98.   {
  99.     //START - MEASURE
  100.     if (receivedCommand == 's') //this is the measure part
  101.     {
  102.       //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
  103.       runallowed = true; //allow running
  104.      
  105.  
  106.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  107.       receivedDelay = Serial.parseFloat(); //value for the speed
  108.  
  109.       Serial.print(receivedMMdistance); //print the values for checking
  110.       Serial.print(receivedDelay);
  111.       Serial.println("Measure "); //print the action
  112.       stepper.setMaxSpeed(receivedDelay); //set speed
  113.       stepper.move(receivedMMdistance); //set distance
  114.  
  115.     }
  116.     //START - OPEN
  117.     if (receivedCommand == 'o') //OPENING
  118.     {
  119.       //example o 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
  120.       runallowed = true; //allow running
  121.      
  122.  
  123.  
  124.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  125.       receivedDelay = Serial.parseFloat(); //value for the speed
  126.  
  127.       Serial.print(receivedMMdistance); //print the values for checking
  128.       Serial.print(receivedDelay);
  129.       Serial.println("OPEN "); //print the action
  130.       stepper.setMaxSpeed(receivedDelay); //set speed
  131.       stepper.move(receivedMMdistance); //set distance
  132.  
  133.     }
  134.  
  135.     //START - CLOSE
  136.     if (receivedCommand == 'c') //CLOSING - Rotates the motor in the opposite direction as opening
  137.     {
  138.       //example c 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
  139.       runallowed = true; //allow running
  140.      
  141.  
  142.  
  143.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  144.       receivedDelay = Serial.parseFloat(); //value for the speed
  145.  
  146.       Serial.print(receivedMMdistance);  //print the values for checking
  147.       Serial.print(receivedDelay);
  148.       Serial.println("CLOSE "); //print action
  149.       stepper.setMaxSpeed(receivedDelay); //set speed
  150.       stepper.move(-1 * receivedMMdistance); ////set distance - negative value flips the direction
  151.  
  152.     }
  153.  
  154.     //STOP - STOP
  155.     if (receivedCommand == 'n') //immediately stops the motor
  156.     {
  157.       runallowed = false; //disable running
  158.        
  159.       stepper.setCurrentPosition(0); // reset position
  160.       Serial.println("STOP "); //print action
  161.       stepper.stop(); //stop motor
  162.       stepper.disableOutputs(); //disable power
  163.  
  164.     }
  165.  
  166.     //SET ACCELERATION
  167.     if (receivedCommand == 'a') //Setting up a new acceleration value
  168.     {
  169.       runallowed = false; //we still keep running disabled, since we just update a variable
  170.      
  171.       receivedAcceleration = Serial.parseFloat(); //receive the acceleration from serial
  172.  
  173.       stepper.setAcceleration(receivedAcceleration); //update the value of the variable
  174.  
  175.       Serial.println("ACC Updated "); //confirm update by message
  176.  
  177.     }
  178.  
  179.   }
  180.   //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.
  181.   newData = false;
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement