Sorry dont know how to upload code :o
#include "Stepper_Motor_Class.h" //class for stepper motor
const char MOTOR_PIN1 = 6;
const char MOTOR_PIN2 = 5;
Stepper_Motor stepper (MOTOR_PIN1, MOTOR_PIN2);
//Initialize objects with begin method
void setup() {
Serial.begin(9600);
stepper.begin();
stepper.stepsconstantspeed(50,1000);//put this in setup to debug
}
void loop() {
}
Header file:
#include "Arduino.h"
class Stepper_Motor
{
public:
Stepper_Motor (int pin, int pin2);
Stepper_Motor (unsigned long RPM, double steps);
void begin();
void singlestep();
void stepsconstantspeed (unsigned long _RPM, double _steps);
private:
int _pin_status;
int _pin2_status;
int _done;
double _steps;
int _timer;
unsigned long _RPM;
int _counter;
int _pin;
int _pin2;
unsigned long _startpoint;
};
//Constructor
Stepper_Motor::Stepper_Motor (int pin, int pin2)
{
_pin = pin;
_pin2 = pin2;
}
Stepper_Motor::Stepper_Motor (unsigned long RPM, double steps)
{
_RPM = RPM;
_steps = steps;
}
//**********************************************************************************
//**********************************************************************************
void Stepper_Motor::begin()//this method initializes pins and variables for stepper function
{
pinMode(_pin, OUTPUT);//set pin to output
pinMode(_pin2, OUTPUT);
digitalWrite(_pin, LOW);
digitalWrite(_pin2, LOW);
_pin_status = 0;
_pin2_status = 0;
_counter = 0;
_done = 0;
}
void Stepper_Motor::singlestep()//this method moves stepper forward according to sequence
{
if (_pin_status == 0 && _pin2_status == 0 && _done == 0)
{
digitalWrite(_pin, LOW);
digitalWrite(_pin2, HIGH);
_pin_status = 0;
_pin2_status = 1;
_done = 1;
}
if (_pin_status == 0 && _pin2_status == 1 && _done == 0)
{
digitalWrite(_pin, HIGH);
digitalWrite(_pin2, HIGH);
_pin_status = 1;
_pin2_status = 1;
_done = 1;
}
if (_pin_status == 1 && _pin2_status == 1 && _done == 0)
{
digitalWrite(_pin, HIGH);
digitalWrite(_pin2, LOW);
_pin_status = 1;
_pin2_status = 0;
_done = 1;
}
if (_pin_status == 1 && _pin2_status == 0 && _done == 0)
{
digitalWrite(_pin, LOW);
digitalWrite(_pin2, LOW);
_pin_status = 0;
_pin2_status = 0;
_done = 1;
}
_done = 0;
}
void Stepper_Motor::stepsconstantspeed (unsigned long _RPM, double _steps)//this method moves motor x steps at constant speed
{
_startpoint = millis();
Serial.print (_startpoint);
while (millis() <= _startpoint + _RPM)//constant speed loop
{
if (millis() == _startpoint + _RPM)//will execute step every _RPM milliseconds
{
if (_counter <= _steps)//exits while loop when counter = steps
{
singlestep();
_startpoint = millis();
Serial.print (_startpoint);
_counter ++;
Serial.print ("Step");
Serial.print (_counter);
}
}
}
_counter = 0;
Serial.print ("Im done");
Serial.println();
}