Hi, I'm trying to convert this sketch into a library to make things more manageable...
/* Defines the vales passed to the function vStepMotor()
to specify which direction you would like the motor to
turn in. */
#define FORWARD true
#define REVERSE false
/* Defines the digital IO pins used to control each of
the stepper motors coils. */
#define MOTOR_COIL_1_DIO 8
#define MOTOR_COIL_2_DIO 9
#define MOTOR_COIL_3_DIO 10
#define MOTOR_COIL_4_DIO 11
/* The number of steps for 1 revolution of this stepper
motor is 360 degrees divided by the motor step angle
of 5.625 multiplied by the gear ratio 1/64 multiplied
by the number of coils 4 divided by two.
Therefore: 360 / (5.625 * (1 / 64)) = 512 */
#define STEPSPER1REV 512
/* Set the 4 DIO pins used to drive the stepper motor to outputs */
void setup()
{
pinMode(MOTOR_COIL_1_DIO, OUTPUT);
pinMode(MOTOR_COIL_2_DIO, OUTPUT);
pinMode(MOTOR_COIL_3_DIO, OUTPUT);
pinMode(MOTOR_COIL_4_DIO, OUTPUT);
}
/* MAIN PROGRAM */
void loop()
{
/* Used for counting the number of steps the motor has made */
word u16StepCounter;
/* Rotate the stepper motor one full forward revolution */
for (u16StepCounter = 0; u16StepCounter < STEPSPER1REV; u16StepCounter++)
{
vStepMotor(FORWARD, 3);
}
/* Rotate the stepper motor one full reverse revolution */
for (u16StepCounter = 0; u16StepCounter < STEPSPER1REV; u16StepCounter++)
{
vStepMotor(REVERSE, 3);
}
}
/* Function will pulse all four coils of the sterrper motor in either the forward
or reverse directions. The function accepts to inputs, a true or a false denoting
the direction the motor should turn in (true = forward, false = reverse), and a step
delay time in milliseconds between each step. */
void vStepMotor(boolean bDirection, word u16StepDelay)
{
/* Holds which DIO pin is currently associated with which motor coil */
byte bMotorCoil_1;
byte bMotorCoil_2;
byte bMotorCoil_3;
byte bMotorCoil_4;
/* Set the order of the DIO pins depending on the direction the
motor is requires to turn in. */
if (bDirection == true)
{
bMotorCoil_1 = MOTOR_COIL_1_DIO;
bMotorCoil_2 = MOTOR_COIL_2_DIO;
bMotorCoil_3 = MOTOR_COIL_3_DIO;
bMotorCoil_4 = MOTOR_COIL_4_DIO;
}else
{
bMotorCoil_1 = MOTOR_COIL_4_DIO;
bMotorCoil_2 = MOTOR_COIL_3_DIO;
bMotorCoil_3 = MOTOR_COIL_2_DIO;
bMotorCoil_4 = MOTOR_COIL_1_DIO;
}
/* Pulse each of the stepper motors coils in a sequential order */
digitalWrite(bMotorCoil_1, HIGH);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, HIGH);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, HIGH);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, HIGH);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
}
I've managed to create a header file and a cpp file for it i wanted to make sure I've done it correctly.
Header...
//header
/** Motor control library... **/
#ifndef Motor_h
#define Motor_h
#include "Arduino.h"
class Motor
{
public:
Motor(int MOTOR_COIL_1_DIO, int MOTOR_COIL_2_DIO, int MOTOR_COIL_3_DIO, int MOTOR_COIL_4_DIO);
void vStepMotor(boolean bDirection, word u16StepDelay);
private:
int _pin1;
int _pin2;
int _pin3;
int _pin4;
};
#endif
CPP
/*
Motor.cpp
*/
#include "Arduino.h"
#include "Motor.h"
Motor::Motor(int MOTOR_COIL_1_DIO, int MOTOR_COIL_2_DIO, int MOTOR_COIL_3_DIO, int MOTOR_COIL_4_DIO);
{
pinMode(MOTOR_COIL_1_DIO, OUTPUT,MOTOR_COIL_2_DIO, OUTPUT,MOTOR_COIL_3_DIO, OUTPUT,MOTOR_COIL_4_DIO, OUTPUT);
_pin1 = MOTOR_COIL_1_DIO;
_pin2 = MOTOR_COIL_2_DIO;
_pin3 = MOTOR_COIL_3_DIO;
_pin4 = MOTOR_COIL_4_DIO;
}
void Motor::(boolean bDirection, word u16StepDelay)
{
/* Holds which DIO pin is currently associated with which motor coil */
byte bMotorCoil_1;
byte bMotorCoil_2;
byte bMotorCoil_3;
byte bMotorCoil_4;
/* Set the order of the DIO pins depending on the direction the
motor is requires to turn in. */
if (bDirection == true)
{
bMotorCoil_1 = MOTOR_COIL_1_DIO;
bMotorCoil_2 = MOTOR_COIL_2_DIO;
bMotorCoil_3 = MOTOR_COIL_3_DIO;
bMotorCoil_4 = MOTOR_COIL_4_DIO;
}else
{
bMotorCoil_1 = MOTOR_COIL_4_DIO;
bMotorCoil_2 = MOTOR_COIL_3_DIO;
bMotorCoil_3 = MOTOR_COIL_2_DIO;
bMotorCoil_4 = MOTOR_COIL_1_DIO;
}
/* Pulse each of the stepper motors coils in a sequential order */
digitalWrite(bMotorCoil_1, HIGH);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, HIGH);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, HIGH);
digitalWrite(bMotorCoil_4, LOW);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, HIGH);
delay(u16StepDelay);
digitalWrite(bMotorCoil_1, LOW);
digitalWrite(bMotorCoil_2, LOW);
digitalWrite(bMotorCoil_3, LOW);
digitalWrite(bMotorCoil_4, LOW);
}