skizziks_53

Stepper tester Oct 8 2020 v01

Oct 8th, 2020
1,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. /*
  2.   Stepper motor example - October 8, 2020
  3.  
  4.   To use this sketch:
  5.  
  6.   1. Attach 5+v (from the Arduino) to the pins on the motor controller marked [ENA+], [PUL+] and [DIR+]
  7.   2. Attach the Arduino pins 2/3/4 to the pins on the motor controller.
  8.  
  9. */
  10.  
  11. int motor_1__enable_pin = 2;
  12. int motor_1__direction_pin = 3;
  13. int motor_1__step_pin = 4;
  14.  
  15. int motor_1__rotation_speed_rpms = 30; // This is where you put the RPMs that you want the motor to spin at. Note the motor speed limits above!
  16. int motor_1__direction_setting = 1; // This is set to zero or 1
  17. int motor_1__steps_per_turn = 200; // This is the physical steps per turn of the motor itself.
  18. int motor_1__microstep_setting = 1; // This is the microstep setting being used. (value must be 1 or some power of 2)
  19.  
  20. int motor_1__half_step_interval = 0; // This is a milliseconds time for a half-step to occur.
  21. int motor_1__step_pin_state = 1; // This is alternated between zero and 1.
  22. unsigned long motor_1__begin_time = 0;
  23. unsigned long motor_1__current_time = 0;
  24.  
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.  
  29.   pinMode(motor_1__enable_pin, OUTPUT);
  30.   pinMode(motor_1__direction_pin, OUTPUT);
  31.   pinMode(motor_1__step_pin, OUTPUT);
  32.  
  33.   set_motor_1_speed();
  34.   set_motor_1_direction();
  35.  
  36.   Serial.println("Exiting setup()");
  37. }
  38.  
  39. void loop() {
  40.  
  41.   check_motor_1();
  42.  
  43. }
  44.  
  45.  
  46. void set_motor_1_speed() {
  47.   // float types are used because these numbers can easily be too large for an int type.
  48.   float motor_effective_steps_per_turn = motor_1__steps_per_turn * motor_1__microstep_setting;
  49.   float double_motor_speed = 2 * motor_1__rotation_speed_rpms; // Since the 'on' and 'off' steps both must be timed, we need to count both.
  50.   float motor__total_pulses_per_minute = motor_effective_steps_per_turn * double_motor_speed;
  51.   motor_1__half_step_interval = (int) 60000 / motor__total_pulses_per_minute;
  52.   Serial.print("motor_1__half_step_interval = ");
  53.   Serial.println(motor_1__half_step_interval);
  54. }
  55.  
  56. void set_motor_1_direction() {
  57.   digitalWrite(motor_1__direction_pin, motor_1__direction_setting);
  58. }
  59.  
  60. void check_motor_1() {
  61.   motor_1__current_time = millis();
  62.   if (motor_1__current_time >= motor_1__begin_time) {
  63.     if (motor_1__current_time >= (motor_1__begin_time + motor_1__half_step_interval)) {
  64.       if (motor_1__step_pin_state == 1) {
  65.         motor_1__step_pin_state = 0;
  66.       }
  67.       else {
  68.         motor_1__step_pin_state = 1;
  69.       }
  70.       digitalWrite(motor_1__step_pin, motor_1__step_pin_state);
  71.       motor_1__begin_time = millis();
  72.     }
  73.   }
  74.   else {
  75.     motor_1__begin_time = millis();
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment