Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Stepper motor example - October 8, 2020
- To use this sketch:
- 1. Attach 5+v (from the Arduino) to the pins on the motor controller marked [ENA+], [PUL+] and [DIR+]
- 2. Attach the Arduino pins 2/3/4 to the pins on the motor controller.
- */
- int motor_1__enable_pin = 2;
- int motor_1__direction_pin = 3;
- int motor_1__step_pin = 4;
- 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!
- int motor_1__direction_setting = 1; // This is set to zero or 1
- int motor_1__steps_per_turn = 200; // This is the physical steps per turn of the motor itself.
- int motor_1__microstep_setting = 1; // This is the microstep setting being used. (value must be 1 or some power of 2)
- int motor_1__half_step_interval = 0; // This is a milliseconds time for a half-step to occur.
- int motor_1__step_pin_state = 1; // This is alternated between zero and 1.
- unsigned long motor_1__begin_time = 0;
- unsigned long motor_1__current_time = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(motor_1__enable_pin, OUTPUT);
- pinMode(motor_1__direction_pin, OUTPUT);
- pinMode(motor_1__step_pin, OUTPUT);
- set_motor_1_speed();
- set_motor_1_direction();
- Serial.println("Exiting setup()");
- }
- void loop() {
- check_motor_1();
- }
- void set_motor_1_speed() {
- // float types are used because these numbers can easily be too large for an int type.
- float motor_effective_steps_per_turn = motor_1__steps_per_turn * motor_1__microstep_setting;
- 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.
- float motor__total_pulses_per_minute = motor_effective_steps_per_turn * double_motor_speed;
- motor_1__half_step_interval = (int) 60000 / motor__total_pulses_per_minute;
- Serial.print("motor_1__half_step_interval = ");
- Serial.println(motor_1__half_step_interval);
- }
- void set_motor_1_direction() {
- digitalWrite(motor_1__direction_pin, motor_1__direction_setting);
- }
- void check_motor_1() {
- motor_1__current_time = millis();
- if (motor_1__current_time >= motor_1__begin_time) {
- if (motor_1__current_time >= (motor_1__begin_time + motor_1__half_step_interval)) {
- if (motor_1__step_pin_state == 1) {
- motor_1__step_pin_state = 0;
- }
- else {
- motor_1__step_pin_state = 1;
- }
- digitalWrite(motor_1__step_pin, motor_1__step_pin_state);
- motor_1__begin_time = millis();
- }
- }
- else {
- motor_1__begin_time = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment