D:/DRISSI/arduino-0022/arduino-0022/libraries/Stepper/Stepper.cpp
00001 /*
00002   Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
00003   
00004   Original library     (0.1) by Tom Igoe.
00005   Two-wire modifications   (0.2) by Sebastian Gassner
00006   Combination version   (0.3) by Tom Igoe and David Mellis
00007   Bug fix for four-wire   (0.4) by Tom Igoe, bug fix from Noah Shibley  
00008 
00009   Drives a unipolar or bipolar stepper motor using  2 wires or 4 wires
00010 
00011   When wiring multiple stepper motors to a microcontroller,
00012   you quickly run out of output pins, with each motor requiring 4 connections. 
00013 
00014   By making use of the fact that at any time two of the four motor
00015   coils are the inverse  of the other two, the number of
00016   control connections can be reduced from 4 to 2. 
00017 
00018   A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
00019   connects to only 2 microcontroler pins, inverts the signals received,
00020   and delivers the 4 (2 plus 2 inverted ones) output signals required
00021   for driving a stepper motor.
00022 
00023   The sequence of control signals for 4 control wires is as follows:
00024 
00025   Step C0 C1 C2 C3
00026      1  1  0  1  0
00027      2  0  1  1  0
00028      3  0  1  0  1
00029      4  1  0  0  1
00030 
00031   The sequence of controls signals for 2 control wires is as follows
00032   (columns C1 and C2 from above):
00033 
00034   Step C0 C1
00035      1  0  1
00036      2  1  1
00037      3  1  0
00038      4  0  0
00039 
00040   The circuits can be found at 
00041  
00042 http://www.arduino.cc/en/Tutorial/Stepper
00043  
00044  
00045  */
00046 
00047 
00048 #include "WProgram.h"
00049 #include "Stepper.h"
00050 
00051 /*
00052  * two-wire constructor.
00053  * Sets which wires should control the motor.
00054  */
00055 Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
00056 {
00057   this->step_number = 0;      // which step the motor is on
00058   this->speed = 0;        // the motor speed, in revolutions per minute
00059   this->direction = 0;      // motor direction
00060   this->last_step_time = 0;    // time stamp in ms of the last step taken
00061   this->number_of_steps = number_of_steps;    // total number of steps for this motor
00062   
00063   // Arduino pins for the motor control connection:
00064   this->motor_pin_1 = motor_pin_1;
00065   this->motor_pin_2 = motor_pin_2;
00066 
00067   // setup the pins on the microcontroller:
00068   pinMode(this->motor_pin_1, OUTPUT);
00069   pinMode(this->motor_pin_2, OUTPUT);
00070   
00071   // When there are only 2 pins, set the other two to 0:
00072   this->motor_pin_3 = 0;
00073   this->motor_pin_4 = 0;
00074   
00075   // pin_count is used by the stepMotor() method:
00076   this->pin_count = 2;
00077 }
00078 
00079 
00080 /*
00081  *   constructor for four-pin version
00082  *   Sets which wires should control the motor.
00083  */
00084 
00085 Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
00086 {
00087   this->step_number = 0;      // which step the motor is on
00088   this->speed = 0;        // the motor speed, in revolutions per minute
00089   this->direction = 0;      // motor direction
00090   this->last_step_time = 0;    // time stamp in ms of the last step taken
00091   this->number_of_steps = number_of_steps;    // total number of steps for this motor
00092   
00093   // Arduino pins for the motor control connection:
00094   this->motor_pin_1 = motor_pin_1;
00095   this->motor_pin_2 = motor_pin_2;
00096   this->motor_pin_3 = motor_pin_3;
00097   this->motor_pin_4 = motor_pin_4;
00098 
00099   // setup the pins on the microcontroller:
00100   pinMode(this->motor_pin_1, OUTPUT);
00101   pinMode(this->motor_pin_2, OUTPUT);
00102   pinMode(this->motor_pin_3, OUTPUT);
00103   pinMode(this->motor_pin_4, OUTPUT);
00104 
00105   // pin_count is used by the stepMotor() method:  
00106   this->pin_count = 4;  
00107 }
00108 
00109 /*
00110   Sets the speed in revs per minute
00111 
00112 */
00113 void Stepper::setSpeed(long whatSpeed)
00114 {
00115   this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
00116 }
00117 
00118 /*
00119   Moves the motor steps_to_move steps.  If the number is negative, 
00120    the motor moves in the reverse direction.
00121  */
00122 void Stepper::step(int steps_to_move)
00123 {  
00124   int steps_left = abs(steps_to_move);  // how many steps to take
00125   
00126   // determine direction based on whether steps_to_mode is + or -:
00127   if (steps_to_move > 0) {this->direction = 1;}
00128   if (steps_to_move < 0) {this->direction = 0;}
00129     
00130     
00131   // decrement the number of steps, moving one step each time:
00132   while(steps_left > 0) {
00133   // move only if the appropriate delay has passed:
00134   if (millis() - this->last_step_time >= this->step_delay) {
00135       // get the timeStamp of when you stepped:
00136       this->last_step_time = millis();
00137       // increment or decrement the step number,
00138       // depending on direction:
00139       if (this->direction == 1) {
00140         this->step_number++;
00141         if (this->step_number == this->number_of_steps) {
00142           this->step_number = 0;
00143         }
00144       } 
00145       else { 
00146         if (this->step_number == 0) {
00147           this->step_number = this->number_of_steps;
00148         }
00149         this->step_number--;
00150       }
00151       // decrement the steps left:
00152       steps_left--;
00153       // step the motor to step number 0, 1, 2, or 3:
00154       stepMotor(this->step_number % 4);
00155     }
00156   }
00157 }
00158 
00159 /*
00160  * Moves the motor forward or backwards.
00161  */
00162 void Stepper::stepMotor(int thisStep)
00163 {
00164   if (this->pin_count == 2) {
00165     switch (thisStep) {
00166       case 0: /* 01 */
00167       digitalWrite(motor_pin_1, LOW);
00168       digitalWrite(motor_pin_2, HIGH);
00169       break;
00170       case 1: /* 11 */
00171       digitalWrite(motor_pin_1, HIGH);
00172       digitalWrite(motor_pin_2, HIGH);
00173       break;
00174       case 2: /* 10 */
00175       digitalWrite(motor_pin_1, HIGH);
00176       digitalWrite(motor_pin_2, LOW);
00177       break;
00178       case 3: /* 00 */
00179       digitalWrite(motor_pin_1, LOW);
00180       digitalWrite(motor_pin_2, LOW);
00181       break;
00182     } 
00183   }
00184   if (this->pin_count == 4) {
00185     switch (thisStep) {
00186       case 0:    // 1010
00187       digitalWrite(motor_pin_1, HIGH);
00188       digitalWrite(motor_pin_2, LOW);
00189       digitalWrite(motor_pin_3, HIGH);
00190       digitalWrite(motor_pin_4, LOW);
00191       break;
00192       case 1:    // 0110
00193       digitalWrite(motor_pin_1, LOW);
00194       digitalWrite(motor_pin_2, HIGH);
00195       digitalWrite(motor_pin_3, HIGH);
00196       digitalWrite(motor_pin_4, LOW);
00197       break;
00198       case 2:    //0101
00199       digitalWrite(motor_pin_1, LOW);
00200       digitalWrite(motor_pin_2, HIGH);
00201       digitalWrite(motor_pin_3, LOW);
00202       digitalWrite(motor_pin_4, HIGH);
00203       break;
00204       case 3:    //1001
00205       digitalWrite(motor_pin_1, HIGH);
00206       digitalWrite(motor_pin_2, LOW);
00207       digitalWrite(motor_pin_3, LOW);
00208       digitalWrite(motor_pin_4, HIGH);
00209       break;
00210     } 
00211   }
00212 }
00213 
00214 /*
00215   version() returns the version of the library:
00216 */
00217 int Stepper::version(void)
00218 {
00219   return 4;
00220 }