You are currently browsing the monthly archive for December 2007.
// Variables for calculating PERIOD and SEX pointers
//float speedPointer();
int encoder[108];
//PROGMEM prog_uchar encoder[106];
//prog_uchar encoder[106] PROGMEM;
//unsigned char lastEncoder0Pos;
int displayRead;
int lastEncoder0Pos;
unsigned char encoder0Pos;
int steps;
int timePeriod=60;
int passedDays=15;
int cycleCount;
int gear20=18;
int gear24=96;
int delayRate;
int pointerPos=0;
//int sexPoint=0;
//int reading[107];
int initialSteps;
int delayMillis;
int savedMillis;
int ellapsedMillis;
int displayEncoder;
//int lastEncoder0Pos;
int lastChange;
//Variables for communicating to Processing
int firstByte; // value for Period
int secondByte; // value for Initial Date
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received
// 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 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // per revolution of your motor
// Pins 4 to 7 for Motor 1
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
//Pins 8-11 for Motor 2
#define motorPin5 4
#define motorPin6 5
#define motorPin7 6
#define motorPin8 7
// 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(10);
myStepper2.setSpeed(60);
// Initialize the Serial port:
Serial.begin(9600);
// myStepper.step(10);
//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
savedMillis=millis();
initialSteps=-106.67*passedDays/timePeriod;
delayMillis=9.375*timePeriod;
myStepper2.step(int(initialSteps));
Serial.println(initialSteps);
}
void loop() {
/*
if (Serial.available() > 0) {
// read the most recent byte (which will be from 0 to 255)
if(Serial.read()==65){
timePeriod = Serial.read();
passedDays = Serial.read();
}
}
*/
/*
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for PERIOD
secondByte = serialInArray[2]; // get value for INITIAL DATE
serialCount = 0;
}
*/
// delayMillis=0;
// Serial.println(delayMillis);
ellapsedMillis=millis()-savedMillis;
// Serial.println(ellapsedMillis);
if(ellapsedMillis>=delayMillis){
// save the reading
encoder[pointerPos]=encoder0Pos;
Serial.println(”writingwhenmillis-lastChange>=delayMillis”);
Serial.println(pointerPos);
Serial.println(encoder0Pos, DEC);
//Serial.println(ellapsedMillis);
myStepper2.step(-1);
savedMillis=millis();
pointerPos+=1;
// lastChange = millis();
// if (encoder[pointerPos]>0) {
if(cycleCount>0){
Serial.println(”encoder[pointerPos]”);
Serial.println(encoder[pointerPos],DEC);
Serial.println(encoder0Pos, DEC);
steps=int(0.375*encoder[pointerPos]-0.375*encoder0Pos);
Serial.println(steps);
myStepper.step(steps);
}
/*
// if (millis() - lastChange >= delayMillis*0.8) {
if (millis() - savedMillis >= delayMillis*0.8) {
// save the reading
encoder[pointerPos]=encoder0Pos;
Serial.println(”writingwhenmillis-lastChange>=delayMillis”);
Serial.println(pointerPos);
Serial.println(encoder0Pos, DEC);
//prog_uchar encoder[pointerPos]=lastEncoder0Pos;
}
}
*/
if (pointerPos == 106){
cycleCount+=1;
pointerPos=0;
if (cycleCount==3){
myStepper2.step(3);
}
Serial.println(”cycleCount”);
}
}
}
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
}
}
if(encoder0Pos>64){
encoder0Pos=1;
}
if(encoder0Pos<1){
encoder0Pos=64;
}
// Serial.println (encoder0Pos, DEC); // debug - remember to comment out
}
2, wiring the 2 stepper motors. Both of them areunipolar.
The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole. (source: http://www.tigoe.net/pcomp/code/category/code/arduinowiring/51 )
3, connecting the encoder
http://www.nubotics.com/support/index.html
4, soldering onto little piece of perfboard to make the plug-in modula on arduino
5, test the encoder, and we got accurate readings: every rotation of the steppers, the encoder increments “62″
6, first version of housing gears, motors and encoder.
6, final version of housing
7, standing up (here is the tricky thing.the gravity made the gears works a little bit different, when moving towards up, like 9 o clock, the pointer stepping may loose steps, so we did re-housing, making it more secure and stable, and checking motors again)
8, spray the face and mount onto the clock.
// Variables for calculating PERIOD and SEX pointers
//float speedPointer();
//#include <avr/pgmspace.h>
int encoder[108];
//PROGMEM prog_uchar encoder[106];
//prog_uchar encoder[106] PROGMEM;
//unsigned char lastEncoder0Pos;
int displayRead;
int lastEncoder0Pos;
unsigned char encoder0Pos;
int steps;
int timePeriod=120;
int passedDays=15;
int cycleCount;
int gear20=18;
int gear24=96;
int delayRate;
int pointerPos=0;
//int sexPoint=0;
//int reading[107];
int initialSteps;
int delayMillis;
int savedMillis;
int ellapsedMillis;
int displayEncoder;
//int lastEncoder0Pos;
int lastChange;
//Variables for communicating to Processing
int firstByte; // value for Period
int secondByte; // value for Initial Date
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received
// 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 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // per revolution of your motor
// Pins 4 to 7 for Motor 1
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
//Pins 8-11 for Motor 2
#define motorPin5 4
#define motorPin6 5
#define motorPin7 6
#define motorPin8 7
// 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(10);
myStepper2.setSpeed(60);
// Initialize the Serial port:
Serial.begin(9600);
// myStepper.step(10);
//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
savedMillis=millis();
initialSteps=-106.67*passedDays/timePeriod;
delayMillis=9.375*timePeriod;
myStepper2.step(int(initialSteps));
Serial.println(initialSteps);
}
void loop() {
lastChange = millis();
/*
if (Serial.available() > 0) {
// read the most recent byte (which will be from 0 to 255)
if(Serial.read()==65){
timePeriod = Serial.read();
passedDays = Serial.read();
}
}
*/
/*
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for PERIOD
secondByte = serialInArray[2]; // get value for INITIAL DATE
serialCount = 0;
}
*/
//delayMillis=1000*gear20*timePeriod/(gear24*motor2Steps);
// delayMillis=0;
// Serial.println(delayMillis);
//+” “+delayMillis+” “+savedMillis);
ellapsedMillis=millis()-savedMillis;
if(ellapsedMillis>=delayMillis){
// Serial.println(ellapsedMillis);
myStepper2.step(-1);
savedMillis=millis();
pointerPos+=1;
// if(pointerPos==30){
// Serial.println(”pointerPos”);
// Serial.println(pointerPos);
// }
//encoder[pointerPos]=encoder0Pos;
//encoder0Pos=lastEncoder0Pos;
//Serial.println(pointerPos);
// displayEncoder = pgm_read_byte_near(lastEncoder0Pos);
// Serial.println(displayEncoder);
///////// STEPPER1.STEP()
// lastEncoder0Pos = encoder0Pos;
// note the last time the encoder changed:
//lastEncoder0Pos=encoder[pointerPos];
// if (encoder0Pos != encoder[pointerPos] && encoder[pointerPos]>0) {
if (encoder[pointerPos]>0) {
// save the last change in the encoder:
// if(pointerPos<30){
Serial.println(”encoder[pointerPos]” );
Serial.println(encoder[pointerPos]);
// }
// Serial.println(”encoder0Pos”);
// Serial.println(encoder0Pos, DEC);
// Serial.println(”lastEncoder0Pos”);
// Serial.println(lastEncoder0Pos);
steps=int(.375*(-encoder0Pos+encoder[pointerPos]));
Serial.println(steps);
myStepper.step(steps);
}
/*
if(encoder0Pos>lastEncoder0Pos){
steps=int(.375*(64-encoder0Pos+lastEncoder0Pos));
Serial.println(”steps1″);
Serial.println(steps);
// Serial.println(.375*(64-encoder0Pos+encoder[pointerPos]));
}else{
steps=int(.375*(lastEncoder0Pos-encoder0Pos));
Serial.println(”steps2″);
Serial.println(steps);
// Serial.println(((encoder[pointerPos]-encoder0Pos));
}
}
*/
if (millis() - lastChange >= delayMillis*0.8) {
// if (millis() - lastChange >= 1000) {
// save the reading
encoder[pointerPos]=encoder0Pos;
// Serial.println(”writingingto”);
// Serial.println(lastEncoder0Pos);
//prog_uchar encoder[pointerPos]=lastEncoder0Pos;
}
}
if (pointerPos == 106){
cycleCount+=1;
pointerPos=0;
if (cycleCount==3){
myStepper2.step(3);
}
Serial.println(”cycleCount”);
}
/*
for (int i=0; i<pointerPos; i++){
encoder[i]=displayRead;
Serial.println(displayRead);
if (displayRead>1 && i==pointerPos){
if (displayRead != encoder0Pos)
myStepper.step(1);
}
}
*/
/*
for( int i=0; i<pointerPos; i++){
encoder[i]=lastEncoder0Pos;
if (lastEncoder0Pos >0){
if (encoder0Pos != lastEncoder0Pos)
{
myStepper.step(1);
}
}
}
*/
}
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
}
}
if(encoder0Pos>64){
encoder0Pos=1;
}
if(encoder0Pos<1){
encoder0Pos=64;
}
// Serial.println (encoder0Pos, DEC); // debug - remember to comment out
}
// Variables for calculating PERIOD and SEX pointers
//float speedPointer();
//#include <avr/pgmspace.h>
int encoder[106];
//PROGMEM prog_uchar encoder[106];
//prog_uchar encoder[106] PROGMEM;
unsigned char lastEncoder0Pos;
int displayRead;
unsigned char encoder0Pos;
int timePeriod=60;
int passedDays=15;
int cycleCount;
int gear20=18;
int gear24=96;
int delayRate;
int pointerPos=0;
//int sexPoint=0;
//int reading[107];
int initialSteps;
int delayMillis;
int savedMillis;
int ellapsedMillis;
int displayEncoder;
//int lastEncoder0Pos;
int lastChange;
//Variables for communicating to Processing
int firstByte; // value for Period
int secondByte; // value for Initial Date
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received
// 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 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // 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(10);
myStepper2.setSpeed(60);
// 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
savedMillis=millis();
initialSteps=106.67*passedDays/timePeriod;
myStepper2.step(int(initialSteps));
Serial.println(initialSteps);
}
void loop() {
/*
if (Serial.available() > 0) {
// read the most recent byte (which will be from 0 to 255)
if(Serial.read()==65){
timePeriod = Serial.read();
passedDays = Serial.read();
}
}
*/
/*
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for PERIOD
secondByte = serialInArray[2]; // get value for INITIAL DATE
serialCount = 0;
}
*/
//delayMillis=1000*gear20*timePeriod/(gear24*motor2Steps);
delayMillis=9.375*timePeriod;
// delayMillis=0;
// Serial.println(delayMillis);
//+" "+delayMillis+" "+savedMillis);
ellapsedMillis=millis()-savedMillis;
if(ellapsedMillis>=delayMillis){
// Serial.println(ellapsedMillis);
myStepper2.step(1);
savedMillis=millis();
pointerPos+=1;
//encoder[pointerPos]=encoder0Pos;
//encoder0Pos=lastEncoder0Pos;
Serial.println(pointerPos);
// displayEncoder = pgm_read_byte_near(lastEncoder0Pos);
// Serial.println(displayEncoder);
///////// STEPPER1.STEP()
// getPeriod(savedMillis);
}
if (pointerPos == 106){
cycleCount+=1;
pointerPos=0;
if (cycleCount==3){
myStepper2.step(3);
}
Serial.println("cycleCount");
}
// Serial.println(cycleCount);
for (int i=0; i<pointerPos; i++){
encoder[i]=displayRead;
Serial.println(displayRead);
if (displayRead>1 && i==pointerPos){
if (displayRead != encoder0Pos)
myStepper.step(1);
}
}
}
void doEncoder(){
encoder[pointerPos]=encoder0Pos;
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
}
}
if(encoder0Pos>64){
encoder0Pos=1;
}
if(encoder0Pos<1){
encoder0Pos=64;
}
/*
if (encoder0Pos != lastEncoder0Pos) {
// save the last change in the encoder:
lastEncoder0Pos = encoder0Pos;
// note the last time the encoder changed:
lastChange = millis();
}
if (millis() - lastChange >= 1) {
// save the reading
encoder[pointerPos]=encoder0Pos;
// Serial.println(lastEncoder0Pos);
//prog_uchar encoder[pointerPos]=lastEncoder0Pos;
}
*/
// Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
}
// Variables for calculating PERIOD and SEX pointers
float speedPointer();
int gear20=18;
int gear24=96;
int delayRate;
int pointerPos=0;
int sexPoint=0;
int reading[107];
int initialSteps;
//Variables for communicating to Processing
int firstByte; // value for Period
int secondByte; // value for Initial Date
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received
// ENCODER Variables
#define encoder0PinA 2
#define encoder0PinB 3
volatile int encoder0Pos = 0;
// Stepper Motor Variables
int counter=0;
#include
//Motor Steps for Motor 1
#define motor1Steps 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // 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(10);
myStepper2.setSpeed(60);
// 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() {
if (Serial.available() > 0) {
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for PERIOD
secondByte = serialInArray[2]; // get value for INITIAL DATE
serialCount = 0;
}
}
delayRate=24*60*60*1000*gear20*firstByte/gear24/motor2Steps;
}
void getPeriod(){
int cycleCount;
delay(delayRate);
myStepper2.step(1);
pointerPos+=1
if (pointerPos == 106){
cycleCount+=1;
pointerPos=0
if (cycleCount==3){
myStepper2.step(3);
}
}
}
void setInitialPosforPeriodPointer(int timePeriod, int passedDays){
intialSteps[equal tongue]assedDays*gear24*motor2Steps/timePeriod/gear20;
myStepper2.step(initialSteps);
}
void getRead(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
for (int i=0, i1 && i=PointerPos){
if (displayRead != enconder0Pos)
myStepper2.step(1);
}
}
}
/*
Stepper Motor Controller
language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.
The motor moves 100 steps in one direction, then 100 in the other.
Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe
*/
// define the pins that the motor is attached to. You can use
// any digital I/O pins.
int inByte = 0;
#include
#define motorSteps 48 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define ledPin 13
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);
void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(120);
// Initialize the Serial port:
Serial.begin(9600);
// set up the LED pin:
}
void loop() {
// Step forward 100 steps:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// Serial.println("Forward");
// Serial.print(BYTE);
}
myStepper.step(inByte);
delay(500);
}
/*
Stepper Motor Controller
language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.
The motor moves 100 steps in one direction, then 100 in the other.
Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe
*/
// define the pins that the motor is attached to. You can use
// any digital I/O pins.
#include
#define motorSteps 200 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define ledPin 13
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);
void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);
// Initialize the Serial port:
Serial.begin(9600);
// set up the LED pin:
pinMode(ledPin, OUTPUT);
// blink the LED:
blink(3);
}
}
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 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // 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 120/10 RPMS:
myStepper.setSpeed(10);
myStepper2.setSpeed(60);
// 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).
*/








