by on
Dorkbot

Here is a small example of using the arduino/wiring platform to control things in the real world.

A pretty good reference for the controlling stepper motors can be found at.

http://en.nanotec.com/steppermotor_animation.html

The following Circuit fleshes out the sketch at the site above.

And finally here is the sketch to make it go.

/* Rapha Roller Race Controller.
 * The Rapha Roller Race Controller is for a 4 bike 500 meter race The bikes are on rollers which
 * produce pulses. The pulses are used to drive a dial per bicycle which is controlled by
 * a unipolar stepper motor, the dials are geared such that two pulses two each one
 * motor half "step" results in 500 meters per complete revolution.
 *
 * This program uses wirings External Interrupt Mechanism. The program loop is used to
 * watch the control panel and move the dials to a starting position.
 *
 * NOTE: RABBID PROTOTYPE! This was done in a very make it work NOW mode.
 * I am certain there are more elegant solutions.
 *
 * Author Donald Delmar Davis, Tempus Dictum Inc.
 */

#define NMOTORS 4
#define NPINS 4
#define NSTATES 8
#define HALFSTEP 1
#define FULLSTEP 0
#define FORWARDSTEP  1
#define BACKWARDSTEP 0

/*------------------------------------------------------------------------------------
 * Pin Mapping
 */

#define SWITCH1PIN  34
#define SWITCH2PIN  35
#define SWITCH3APIN 49
#define SWITCH3BPIN 50
#define LEDPIN 48
#define BIKE1 36
#define BIKE2 37
#define BIKE3 38
#define BIKE4 39

int motorPins[NMOTORS][NPINS]= {
  { 11,  9, 10,  8  } , // ccw is forward
  { 15, 13, 14, 12  },
  { 20, 22, 21, 23  },
  { 16, 18, 17, 19  },
  //  { 8, 10,  9, 11}, // cw is forward
  //  {12, 14, 13, 15},
  //  {23, 21, 22, 20},
  //  {19, 17, 18, 16},
};

int pulsesPerStep=2;
volatile int pulseCount[4]={0,0,0,0};
volatile int pulseMod[4]={2,2,2,2};
int ledState = HIGH;
int switchState=LOW;
int delayTime = 10;

int motorState [NMOTORS] = {0,0,0,0};

int pinStates[NSTATES][NPINS]= {
  { HIGH,LOW ,LOW ,LOW   } ,
  { HIGH,HIGH,LOW ,LOW   },
  { LOW ,HIGH,LOW ,LOW   },
  { LOW ,HIGH,HIGH,LOW   },
  { LOW ,LOW ,HIGH,LOW   },
  { LOW ,LOW ,HIGH,HIGH  },
  { LOW ,LOW ,LOW ,HIGH  },
  { HIGH,LOW ,LOW ,HIGH  }
};

/* -------
*/
void stepit (int motorNum, int stepDir, int stepSize) {
  int stateSkip=2;
  int pin;

  if (stepSize==HALFSTEP) stateSkip=1;

  if (stepDir==FORWARDSTEP) {
    motorState[motorNum] += stateSkip;
    if (motorState[motorNum] >= NSTATES) {
      motorState[motorNum]=0;
    }
  }
  else {
    if (motorState[motorNum] < stateSkip){
      motorState[motorNum] = NSTATES-1;
    }
    else {
      motorState[motorNum] -= stateSkip ;
    }
  }

  for (pin=0 ; pin < NPINS ; pin++) {
    digitalWrite(motorPins[motorNum][pin], pinStates[motorState[motorNum]][pin]);
  }
}
/*---------------------------------------------------------------------------------setup()
 *
 *
 *
 */
void setup() {
  int motor;
  int pin;
  for (motor=0;motor<NMOTORS; motor++){
    for (pin=0; pin<NPINS; pin++){
      pinMode(motorPins[motor][pin],OUTPUT);
      digitalWrite(motorPins[motor][pin],pinStates[motorState[motor]][pin]);
    }
  }
  pinMode(LEDPIN,OUTPUT);
  pinMode(BIKE1,INPUT);
  pinMode(BIKE2,INPUT);
  pinMode(BIKE3,INPUT);
  pinMode(BIKE4,INPUT);
  pinMode(SWITCH1PIN,INPUT);
  pinMode(SWITCH2PIN,INPUT);
  pinMode(SWITCH3APIN,INPUT);
  pinMode(SWITCH3BPIN,INPUT);

  attachInterrupt(4, bike1, RISING); //
  attachInterrupt(5, bike2, RISING); //
  attachInterrupt(6, bike3, RISING); //
  attachInterrupt(7, bike4, RISING); //
//  Serial.begin(9600);             // Starts Serial to print data

}

/*-----------------------------------------------------------------------------------------
 ************************************* Interrupt Handlers *********************************

 */
/*----------------------------------------------------------------------------------bike1()
 */
void bike1() {
  //Serial.print("bike1 ");
  ++pulseCount[0];
  if ((--pulseMod[0]) == 0 ){
    stepit(0,FORWARDSTEP,HALFSTEP);
    pulseMod[0]=pulsesPerStep;
  }
}
/*-----------------------------------------------------------------------------------------
 */
void bike2() {
  //Serial.print("bike2 ");
  ++pulseCount[1];
  if ((--pulseMod[1]) == 0 ){
    stepit(1,FORWARDSTEP,HALFSTEP);
    pulseMod[1]=pulsesPerStep;
  }
}
/*-----------------------------------------------------------------------------------------
 */
void bike3() {
  //Serial.print("bike3 ");
  ++pulseCount[2];
  if ((--pulseMod[2]) == 0 ){
    stepit(2,FORWARDSTEP,HALFSTEP);
    pulseMod[2]=pulsesPerStep;
  }
}
/*-----------------------------------------------------------------------------------------
 */
void bike4() {
  //Serial.print("bike4 ");
  ++pulseCount[3];
  if ((--pulseMod[3]) == 0 ){
    stepit(3,FORWARDSTEP,HALFSTEP);
    pulseMod[3]=pulsesPerStep;
  }
}
/*-----------------------------------------------------------------------------------------
 ************************************* Main Loop ******************************************
 *---------------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------
 *
 */

void loop() {
  int currentMotor;
  currentMotor=(digitalRead(SWITCH1PIN)*2)+digitalRead(SWITCH2PIN) ;
  switchState=digitalRead(SWITCH3BPIN);
  if (switchState==HIGH) {
     stepit(currentMotor,FORWARDSTEP,HALFSTEP);
   }
   switchState=digitalRead(SWITCH3APIN);
   if (switchState==HIGH) {
     stepit(currentMotor,BACKWARDSTEP,HALFSTEP);
   }
   ledState=!ledState;
   digitalWrite(LEDPIN,ledState);
   delay(delayTime);
}

One Response to “Stepper Motor Control With Arduino/Wiring”

Leave a Reply

  • (will not be published)