Easydriver board library

I've hacked the stepper library to use the EasyDriver board from sparkfun.com. I just wanted it to be drop-in compatible w/ the stepper lib so I could take any .pde using the stepper lib, change a couple of #defines and be good to go. Its kind of sloppy there could be probs but it seems to work for me:
EasyDriver.h:

/*
  EasyDriver.h - Arduino Library for EasyDriver 3.0 stepper driver board from Sparkfun.com
  
  Based on:
  Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
  Original library     (0.1) by Tom Igoe.
  Two-wire modifications   (0.2) by Sebastian Gassner
  Combination version   (0.3) by Tom Igoe and David Mellis
  Bug fix for four-wire   (0.4) by Tom Igoe, bug fix from Noah Shibley


      I wanted This library to be a simple drop-in replacement for the standard stepper lib.
      just change some defines and go!!

                                                Makenb "at" gmail.com      8/28/09


*/

// ensure this library description is only included once
#ifndef EasyDriver_h
#define EasyDriver_h

// library interface description
class Stepper {
  public:
    // constructors:
    Stepper(int number_of_steps, int dir_pin, int step_pin);

    // speed setter method:
    void setSpeed(long whatSpeed);

    // mover method:
    void step(int number_of_steps);

    int version(void);

  private:
    void stepMotor(int this_step);
    
    int direction;        // Direction of rotation
    int speed;          // Speed in RPMs
    unsigned long step_delay;    // delay between steps, in ms, based on speed
    int number_of_steps;      // total number of steps this motor can take
//    int pin_count;        // whether you're driving the motor with 2 or 4 pins
    int step_number;        // which step the motor is on
    
    // motor pin numbers:
    int dir_pin;
    int step_pin;
    
    long last_step_time;      // time stamp in ms of when the last step was taken
};

#endif

EasyDiver.cpp

/*
  EasyDiver.cpp - Arduino Library for EasyDriver 3.0 stepper driver board from Sparkfun.com  
  
  Based on:
  Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
  Original library     (0.1) by Tom Igoe.
  Two-wire modifications   (0.2) by Sebastian Gassner
  Combination version   (0.3) by Tom Igoe and David Mellis
  Bug fix for four-wire   (0.4) by Tom Igoe, bug fix from Noah Shibley  

   I wanted This library to be a simple drop-in replacement for the standard stepper lib.
      just change some defines and go!!

                                                Maken "at" maken.org      8/28/09
                                                
*/


#include "WProgram.h"
#include "EasyDriver.h"

/*
 * EasyDriver constructor.
 * Sets Direction and Step Pins.
 */
Stepper::Stepper(int number_of_steps, int dir_pin, int step_pin)
{
  this->step_number = 0;      // which step the motor is on
  this->speed = 0;        // the motor speed, in revolutions per minute
  this->direction = 0;      // motor direction
  this->last_step_time = 0;    // time stamp in ms of the last step taken
  this->number_of_steps = number_of_steps;    // total number of steps for this motor
  
  // Arduino pins for the motor control connection:
  this->dir_pin = dir_pin;
  this->step_pin = step_pin;

  // setup the pins on the microcontroller:
  pinMode(this->dir_pin, OUTPUT);
  pinMode(this->step_pin, OUTPUT);
  
  // When there are only 2 pins, set the other two to 0:
  //this->motor_pin_3 = 0;
  //this->motor_pin_4 = 0;
  
  // pin_count is used by the stepMotor() method:
  //this->pin_count = 2;
}


/*
  Sets the speed in revs per minute

*/
void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
}

/*
  Moves the motor steps_to_move steps.  If the number is negative, 
   the motor moves in the reverse direction.
 */
void Stepper::step(int steps_to_move)
{  
  int steps_left = abs(steps_to_move);  // how many steps to take
  
  // determine direction based on whether steps_to_mode is + or -:
  if (steps_to_move > 0) {this->direction = 1;}
  if (steps_to_move < 0) {this->direction = 0;}
    
    
  // decrement the number of steps, moving one step each time:
  while(steps_left > 0) {
  // move only if the appropriate delay has passed:
  if (millis() - this->last_step_time >= this->step_delay) {
      // get the timeStamp of when you stepped:
      this->last_step_time = millis();
      // increment or decrement the step number,
      // depending on direction:
      if (this->direction == 1) {
        this->step_number++;
        if (this->step_number == this->number_of_steps) {
          this->step_number = 0;
        }
      } 
      else { 
        if (this->step_number == 0) {
          this->step_number = this->number_of_steps;
        }
        this->step_number--;
      }
      // decrement the steps left:
      steps_left--;
      // step the motor to step number 0, 1, 2, or 3:
      stepMotor(this->direction);
    }
  }
}

/*
 * Moves the motor forward or backwards.
 */
void Stepper::stepMotor(int thisDir)
{
      digitalWrite(dir_pin, thisDir);
        delayMicroseconds(100);
        digitalWrite(step_pin, LOW);
      delayMicroseconds(100);
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(100);
 }

/*
  version() returns the version of the library:
*/
int Stepper::version(void)
{
  return .01;
}

Thanks to Tom Igoe and others :wink:

Maken

I've used it just as a library and works fine. :wink:
Thanks for sharing!

which modifications are required for this to work with an easydriver version 4 ?
can you include an example code ?

how do i change direction ?
how do i stop the motor from turning ?

#include <Easydriver.h>



void setup(){}
void loop(){
Stepper step1(1, 2, 3);
step1.setSpeed(100);
step1.step(1);

}

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1277820563
please help me contribute !!!!

I used the EasyDriver library to drive a Stepper with Potentiometer. In the example I am using 1.8 degree stepper with both the MS1 and MS2 pins set to HIGH (1/8 step resolution). I am pasting my code here for others:

Best, Jason
http://www.future-cities-lab.net/index/

// Drive one Stepper Motor _ Potentiometer using the  EasyDriver v4.3 by Sparkfun _ info: http://schmalzhaus.com/EasyDriver/ 

#include <EasyDriver.h> // download the library here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1251509480
                        // copy txt to your Arduino > Libraries folder; then rename them EasyDriver.h and Easydriver.cp
                        // Built upon the MotorKnob example: http://www.arduino.cc/en/Reference/Stepper
                        
Stepper stepper (200, 3, 2); //Stepper(int number_of_steps, int dir_pin, int step_pin)
int pot = 1;  // initialize pot
int steppos = 1; // initialize steppos
int previous = 0;  // the previous reading from the analog input

void setup()
{
  Serial.begin(9600);     // open the serial connection at 9600bps
  pinMode(2, OUTPUT);  // set pin 2 to output
  pinMode(3, OUTPUT);   // set pin 3 to output
}

void loop() {

  int pot = analogRead(0);  //read pot (dimmer) connected to Analog In 0
  int steppos = map(pot, 0, 1023, 0, 1600); //map pot value to # of steps need for 360 revolution

  stepper.setSpeed(600);  //set speed to x rpms
  stepper.step(steppos - previous);  // move a # of steps = to the change in the sensor reading
  previous = steppos;  // remember the previous val of the sensor  

  Serial.print("position = "); //print to Serial Monitor
  Serial.println(steppos);

}