skizziks_53

Double stepper motors v1.0

Jun 24th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. /*
  2.   Double stepper motor driver - June 24, 2019
  3.  
  4.   Board: any
  5.   Hardware: two normal (3-wire) stepper motor drivers and motors
  6.  
  7.   This sketch shows how to operate more than one stepper motor at the same time. It turns two stepper motors, each at its own fixed speed.
  8.  
  9.   This is plain code that is not as perfect as possible, but is pretty close.
  10.   When timing a single step interval, it is possible to obtain the amount of time over the target time, and subtract that from the next step.
  11.   That isn't necessary for a lot of uses however, as long as the processor isn't burdened with a lot of other tasks.
  12.  
  13.   If you want stepper motors to move precisely, then you need to write the sketch so that while the motors are being moved, the processor does not need to be doing ANYTHING else unrelated to moving the motors.
  14.   The only other tasks should be checking limit switches (using digitalRead) or checking an emergency shut-off button (also using digitalRead).
  15. */
  16.  
  17. bool step_motor_1__enabled = true; // This is a switch variable to turn each motor on or off, basically.
  18. int step_motor_1__enable_pin = 2;
  19. int step_motor_1__direction_pin = 3;
  20. int step_motor_1__step_pin = 4;
  21. long step_motor_1__step_interval = 30000; // This is the "speed", as it is the microseconds time for the step pin to be high or low.
  22. int step_motor_1__step_state = 1;
  23. unsigned long step_motor_1__begin_time = 0;
  24. unsigned long step_motor_1__current_time = 0;
  25.  
  26.  
  27. bool step_motor_2__enabled = true;
  28. int step_motor_2__enable_pin = 5;
  29. int step_motor_2__direction_pin = 6;
  30. int step_motor_2__step_pin = 7;
  31. long step_motor_2__step_interval = 40000; // This is the "speed", as it is the microseconds time for the step pin to be high or low.
  32. int step_motor_2__step_state = 1;
  33. unsigned long step_motor_2__begin_time = 0;
  34. unsigned long step_motor_2__current_time = 0;
  35.  
  36.  
  37. void setup() {
  38.  
  39.   pinMode(step_motor_1__enable_pin, OUTPUT);
  40.   pinMode(step_motor_1__direction_pin, OUTPUT);
  41.   pinMode(step_motor_1__step_pin, OUTPUT);
  42.  
  43.   digitalWrite(step_motor_1__enable_pin, HIGH); // [enable] pin must be high for motor to work.
  44.   digitalWrite(step_motor_1__direction_pin, HIGH); // [direction] pin can be set however you want.
  45.   digitalWrite(step_motor_1__step_pin, LOW); // [step] pin normally set to low, most drivers move on the low-to-high change.
  46.  
  47.   pinMode(step_motor_2__enable_pin, OUTPUT);
  48.   pinMode(step_motor_2__direction_pin, OUTPUT);
  49.   pinMode(step_motor_2__step_pin, OUTPUT);
  50.  
  51.   digitalWrite(step_motor_2__enable_pin, HIGH);
  52.   digitalWrite(step_motor_2__direction_pin, HIGH);
  53.   digitalWrite(step_motor_2__step_pin, LOW);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. void loop() {
  60.  
  61.   check_stepper_1();
  62.  
  63.   check_stepper_2();
  64.  
  65. }
  66.  
  67.  
  68. void check_stepper_1() {
  69.   if (step_motor_1__enabled == true) {
  70.     step_motor_1__current_time = micros();
  71.     if (step_motor_1__current_time >= step_motor_1__begin_time) {
  72.       if (step_motor_1__current_time >= (step_motor_1__begin_time + step_motor_1__step_interval)) {
  73.         if (step_motor_1__step_state == 0) {
  74.           step_motor_1__step_state = 1;
  75.         }
  76.         else {
  77.           step_motor_1__step_state = 0;
  78.         }
  79.         digitalWrite(step_motor_1__step_pin, step_motor_1__step_state);
  80.         step_motor_1__begin_time = micros();
  81.       }
  82.     }
  83.     else {
  84.       step_motor_1__begin_time = micros();
  85.     }
  86.   }
  87. }
  88.  
  89.  
  90.  
  91. void check_stepper_2() {
  92.   if (step_motor_2__enabled == true) {
  93.     step_motor_2__current_time = micros();
  94.     if (step_motor_2__current_time >= step_motor_2__begin_time) {
  95.       if (step_motor_2__current_time >= (step_motor_2__begin_time + step_motor_2__step_interval)) {
  96.         if (step_motor_2__step_state == 0) {
  97.           step_motor_2__step_state = 1;
  98.         }
  99.         else {
  100.           step_motor_2__step_state = 0;
  101.         }
  102.         digitalWrite(step_motor_2__step_pin, step_motor_2__step_state);
  103.         step_motor_2__begin_time = micros();
  104.       }
  105.     }
  106.     else {
  107.       step_motor_2__begin_time = micros();
  108.     }
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment