/*
Two stepper Motor and Rotary Encoder.
This program drives two unipolar (6 wires) stepper motor one stepper with Encoder.
This code is based in Tom Igoe's code in http://tigoe.net
The part of rotary Encoder Code is based on http://www.arduino.cc/playground/Main/RotaryEncoders
Created 26 Nov. 2007
by Sandra Davila, and Meng Li.*/
// ENCODER Variables
#define encoder0PinA 2
#define encoder0PinB 3
volatile int encoder0Pos = 0;
// Stepper Motor Variables
int counter=0;
#include <Stepper.h>
//Motor Steps for Motor 1
#define motor1Steps 48 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 24 // per revolution of your motor
// Pins 4 to 7 for Motor 1
#define motorPin1 4
#define motorPin2 5
#define motorPin3 6
#define motorPin4 7
//Pins 8-11 for Motor 2
#define motorPin5 8
#define motorPin6 9
#define motorPin7 10
#define motorPin8 11
// initialize of the Stepper library:
Stepper myStepper(motor1Steps, motorPin1,motorPin2,motorPin3,motorPin4);
Stepper myStepper2(motor2Steps,motorPin5,motorPin6,motorPin7,motorPin8);
void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(120);
myStepper2.setSpeed(10);
// Initialize the Serial port:
Serial.begin(9600);
//SETUP FOR ENCODER
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop() {
/* To increment the amount of steps and send as a byte to processing:
// for (int i; i=0; i ++){
// counter+i;
//Serial.println("Forward");
//if (counter=26){
//counter=0;
//}
counter=counter+1;*/
myStepper.step(motor1Steps);
myStepper2.step(motor2Steps);
// Serial.print(counter,BYTE); //to send serial info
delay(3000);
// if (counter=26){
// counter=0;
}
void doEncoder(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
}
/* to read the other two transitions - just use another attachInterrupt()
in the setup and duplicate the doEncoder function into say,
doEncoderA and doEncoderB.
You also need to move the other encoder wire over to pin 3 (interrupt 1).
*/