//Learning how to run a bipolar stepper motor.
// Version 0.02 basada en [youtube]https://www.youtube.com/watch?v=PrV-SLxIIFE[/youtube]
//
//Setup:
//-Power source: 10 V / max 750mA (old mobile phone charger)
//-Motor: 17HS1001-05 stepper motor, bipolar, 4-wire, R/coil 10 ohm, ratings unknown
//-Microcontroller: Arduino MEGA
//-Driver: Pololu A4988 stepper motor driver carrier
//
//Pololu current limiter adjusted with the screw and ref pin value measured to be 0.24 V, which corresponds to 2.5 A/V * 0.24 V = 750 mA (look at the Pololu manual). With this setting, I measured about 500 mA drawn from the power source.
//
//MS1 pin set high (connected to 5 V) to enable half stepping, disconnected results in full-step mode
//
//As suggested by Pololu manual, a capacitor (220uF) is added to the power input.
//
//Rest of the pin setups follow the ones on the Pololu manual page:
//http://www.pololu.com/product/2128 and https://www.pololu.com/product/2133
//
#ifndef ARDUINO_BOARDS_H // macro-identificador no está definido?
//#then
#define ARDUINO_BOARDS_H // definalo
#define Arduino_UNO 0
#define Arduino_MEGA_2560 10
#define Genuino_MEGA_2560 Arduino_MEGA_2560
//#define SelectBoard(board) (ARDUINO_BOARD==BOARD_##board)
#endif //BOARDS_H
// The number of steps in one full motor rotation
const int stepsInFullRound = 200; // full-step mode
// const int stepsInFullRound = 400; // half stepping
int statusLED = LOW;
void setup() { // put your setup code here, to run once:
#define ARDUINO_BOARD Arduino_MEGA_2560
#if ARDUINO_BOARD == Arduino_MEGA_2560
// DRIVER8825 pin conexion to arduino
#define DRIVER8825_RESET_PIN 52 // Pin 52 connected to RESET pin of DRIVER8825
#define DRIVER8825_STEP_PIN 50 // Pin 50 connected to STEP pin of DRIVER8825
#define DRIVER8825_DIRECTION_PIN 48 // Pin 48 connected to DIRECTION pin of DRIVER8825
#define DRIVER8825_MODE0_PIN 30
#define DRIVER8825_MODE1_PIN 32
#define DRIVER8825_MODE2_PIN 34
//
#define LED_PIN 46
#define LIMIT_SWITCH_PIN 44 // final de carrera
#else
#endif //BOARDS_H
// Set pins
//pinMode(DRIVER8825_RESET_PIN, OUTPUT);
pinMode(DRIVER8825_STEP_PIN, OUTPUT);
pinMode(DRIVER8825_DIRECTION_PIN, OUTPUT);
pinMode(DRIVER8825_MODE0_PIN, OUTPUT);
pinMode(DRIVER8825_MODE1_PIN, OUTPUT);
pinMode(DRIVER8825_MODE2_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(DRIVER8825_STEP_PIN, LOW);
digitalWrite(DRIVER8825_DIRECTION_PIN, LOW);
// Microstep Resolution
// Full step: low status or disconnected results in full-step mode
digitalWrite(DRIVER8825_MODE0_PIN, LOW);
digitalWrite(DRIVER8825_MODE1_PIN, LOW);
digitalWrite(DRIVER8825_MODE2_PIN, LOW);
// Another
// MODE0 1 2
// High Low Low Half step
// Low High Low 1/4 step
// High High Low 1/8 step
// Low Low High 1/16 step
// High Low High 1/32 step
// Low High High 1/32 step
// High High High 1/32 step
digitalWrite(LED_PIN, LOW);
}
// Runs the motor according to a chosen direction, speed (rounds per seconds) and the number of steps
void run(boolean runForward, double speedRPS, int stepCount) {
digitalWrite(DRIVER8825_DIRECTION_PIN, runForward);
for (int i = 0; i < stepCount; i++) {
digitalWrite(DRIVER8825_STEP_PIN, HIGH);
holdHalfCylce(speedRPS);
digitalWrite(DRIVER8825_STEP_PIN, LOW);
holdHalfCylce(speedRPS);
}
}
// A custom delay function used in the run()-method
void holdHalfCylce(double speedRPS) {
long holdTime_us = (long)(1.0 / (double) stepsInFullRound / speedRPS / 2.0 * 1E6);
int overflowCount = holdTime_us / 65535;
for (int i = 0; i < overflowCount; i++) {
delayMicroseconds(65535);
}
delayMicroseconds((unsigned int) holdTime_us);
}
// Runs the motor once in forward direction and once to the opposite direction.
// Holds the motor still for 1 sec after both movements.
void runBackAndForth(double speedRPS, int rounds) { // funcion ir y venir
run(true, speedRPS, stepsInFullRound * rounds);
delay(1000);
run(false, speedRPS, stepsInFullRound * rounds);
delay(1000);
}
void blinkLED(void) {
statusLED = !statusLED;
digitalWrite(LED_PIN, statusLED);
}
// Tests various speeds 2,3 & 4, with 1 to 3 rotations
void loop(){
blinkLED();
runBackAndForth(2, 1); // speed 2, 1 rotations
blinkLED();
runBackAndForth(3, 2); // speed 3, 2 rotations
blinkLED();
runBackAndForth(4, 3); // speed 4, 3 rotations
}