//////////////////////////////////////// //Arduino + Optical Encoder + Stepper // //////////////////////////////////////// // Adi Soffer 2013 // // for more info visit // // http://adisoffer.tumblr.com // #include <AccelStepper.h> //encoder/motor/driver setup int easyDriverMicroSteps = 4; //2,4,8 int rotaryEncoderSteps = 75; //setps per rev on your encoder int motorStepsPerRev = 200; //steps per rev on the motor int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down int easyDriverDirPin = 16; int easyDriverStepPin = 15; int enablePin = 17; //Sleep Function - to diable ED when not active long previousMillis = 0; int sleepTimer = 5000; int val; int encoder0PinA = 3; int encoder0PinB = 4; int encoderValue = 0; long lastencoderValue = 0; int encoder0PinALast = LOW; int n = LOW; AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin); void setup() { pinMode (encoder0PinA,INPUT); pinMode (encoder0PinB,INPUT); pinMode(enablePin, OUTPUT); stepper.setMinPulseWidth(MinPulseWidth); //may need to play with this, but I think this is good. stepper.setMaxSpeed(50000); stepper.setAcceleration(1000000000); //try 100, or 1000 and see if it works stepper.setSpeed(50000); //play with this to see if it makes a diff (1 to 1000) } void loop() { int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps; stepper.moveTo(encoderValue * stepsPerRotaryStep); stepper.run(); if(encoderValue != lastencoderValue) { lastencoderValue = encoderValue; digitalWrite (enablePin, LOW); previousMillis = millis(); } else { //Stepper sleep after 5sec of no data unsigned long currentMillis = millis (); if (currentMillis - previousMillis>sleepTimer) digitalWrite (enablePin, HIGH); } n = digitalRead(encoder0PinA); if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder0PinB) == LOW) { encoderValue--; } else { encoderValue++; } } encoder0PinALast = n; }
Encoder increments per revolution 2000
//////////////////////////////////////////Arduino + Optical Encoder + Stepper ////////////////////////////////////////// // Adi Soffer 2013 //// for more info visit //
#include <AccelStepper.h>#include <digitalWriteFast.h>
//encoder/motor/driver setupint easyDriverMicroSteps = 16; //2,4,8int rotaryEncoderSteps = 2000; //setps per rev on your encoderint motorStepsPerRev = 200; //steps per rev on the motor
int easyDriverStepPin = 5;int enablePin = 17;
//Sleep Function - to diable ED when not activelong previousMillis = 0;int sleepTimer = 5000;
int val; int encoder0PinA = 2;int encoder0PinB = 3;int encoderValue = 0;long lastencoderValue = 0;
int encoder0PinALast = LOW;int n = LOW;AccelStepper stepper(1, easyDriverStepPin);
void setup() { pinMode (encoder0PinA,INPUT); pinMode (encoder0PinB,INPUT); pinMode(enablePin, OUTPUT);
stepper.setMaxSpeed(200000); stepper.setAcceleration(1555555555); //try 100, or 1000 and see if it works stepper.setSpeed(200000); //play with this to see if it makes a diff (1 to 1000)}
void loop() { int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps; stepper.moveTo(encoderValue * stepsPerRotaryStep); stepper.run(); if(encoderValue != lastencoderValue) { lastencoderValue = encoderValue; digitalWrite (enablePin, LOW); previousMillis = millis(); } else { //Stepper sleep after 5sec of no data unsigned long currentMillis = millis (); if (currentMillis - previousMillis>sleepTimer) digitalWrite (enablePin, HIGH); } n = digitalReadFast(encoder0PinA); if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalReadFast(encoder0PinB) == LOW) {Try this but without changes
#define encoder0PinA 2#define encoder0PinB 3int encoderValue = 0;
n = digitalReadFast(2);
bool destination_encoder;
bool timeout_active;
long stop_time;
void loop() {
int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
stepper.moveTo(encoderValue * stepsPerRotaryStep);
bool moving = stepper.run(); //moving will be true when the stepper is moving towards a target.
if(encoderValue != lastencoderValue)
{
lastencoderValue = encoderValue;
digitalWrite (enablePin, LOW);
previousMillis = millis();
destination_encoder = true; //destination set by encoder.
}
else
{
//Stepper sleep after 5sec of no data
unsigned long currentMillis = millis ();
if (currentMillis - previousMillis>sleepTimer)
digitalWrite (enablePin, HIGH);
}
n = digitalReadFast(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalReadFast(encoder0PinB) == LOW) {
encoderValue--;
}
else {
encoderValue++;
}
}
encoder0PinALast = n;
if (moving) timeout_active = false;
//memorize the time when the stepper has stopped moving.
if (!moving && !timeout_active)
{
timeout_active = true;
stop_time = millis();
}
//when the stepper has stopped, the timeout (in this case: 5000ms) has expired,
//and the last destination was set by the encoder, set a new stepper target.
if (timeout_active && (millis() - stop_time() >= sleepTimer) && destination_encoder)
{
stepper.move(500);
destination_encoder = false; //destination set by timeout code.
}
}
Try this but show error
Arduino: 1.6.1 (Windows 7), Board: "Arduino Uno"arduino_varbuut_driiz.ino: In function 'void loop()':arduino_varbuut_driiz.ino:62:5: error: 'destination_encoder' was not declared in this scopearduino_varbuut_driiz.ino:85:15: error: 'timeout_active' was not declared in this scopearduino_varbuut_driiz.ino:88:19: error: 'timeout_active' was not declared in this scopearduino_varbuut_driiz.ino:91:5: error: 'stop_time' was not declared in this scopearduino_varbuut_driiz.ino:96:7: error: 'timeout_active' was not declared in this scopearduino_varbuut_driiz.ino:96:47: error: 'stop_time' was not declared in this scopearduino_varbuut_driiz.ino:96:67: error: 'destination_encoder' was not declared in this scopeError compiling.This report would have more information with"Show verbose output during compilation"enabled in File > Preferences.
//////////////////////////////////////////Arduino + Optical Encoder + Stepper ////////////////////////////////////////// // Adi Soffer 2013 //// for more info visit //
#include <AccelStepper.h>
//encoder/motor/driver setupint easyDriverMicroSteps = 16; //2,4,8int rotaryEncoderSteps = 2800; //setps per rev on your encoderint motorStepsPerRev = 200; //steps per rev on the motor
int easyDriverDirPin = 16;int easyDriverStepPin = 5;int enablePin = 9;
//Sleep Function - to diable ED when not activelong previousMillis = 0;int sleepTimer = 5000;
int val; int encoder0PinA = 2;int encoder0PinB = 3;int encoderValue = 0;long lastencoderValue = 0;
int encoder0PinALast = LOW;int n = LOW;AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);
void setup() { pinMode (encoder0PinA,INPUT); pinMode (encoder0PinB,INPUT); pinMode(enablePin, OUTPUT);
stepper.setMaxSpeed(50000); stepper.setAcceleration(1000000000); //try 100, or 1000 and see if it works
bool destination_encoder;bool timeout_active;long stop_time;
}void loop() { int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps; stepper.moveTo(encoderValue * stepsPerRotaryStep);
bool moving = stepper.run(); //moving will be true when the stepper is moving towards a target.
if(encoderValue != lastencoderValue) { lastencoderValue = encoderValue; digitalWrite (enablePin, LOW); previousMillis = millis(); destination_encoder = true; //destination set by encoder. } else
{ //Stepper sleep after 5sec of no data unsigned long currentMillis = millis (); if (currentMillis - previousMillis>sleepTimer) digitalWrite (enablePin, HIGH); } n = digitalRead(encoder0PinA); if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder0PinB) == LOW) { encoderValue--; } else { encoderValue++; } } encoder0PinALast = n;
if (timeout_active && (millis() - stop_time >= sleepTimer) && destination_encoder)
{
stepper.move(500);
destination_encoder = false; //destination set by timeout code.
}
}bool destination_encoder;bool timeout_active;long stop_time;//////////////////////////////////////////Arduino + Optical Encoder + Stepper ////////////////////////////////////////// // Adi Soffer 2013 //// for more info visit //
#include <AccelStepper.h>
//encoder/motor/driver setupint easyDriverMicroSteps = 16; //2,4,8int rotaryEncoderSteps = 2800; //setps per rev on your encoderint motorStepsPerRev = 200; //steps per rev on the motor
int easyDriverDirPin = 16;int easyDriverStepPin = 5;int enablePin = 12;
//Sleep Function - to diable ED when not activelong previousMillis = 0;int sleepTimer = 2000;
int val; int encoder0PinA = 2;int encoder0PinB = 3;int encoderValue = 0;long lastencoderValue = 0;
bool destination_encoder;bool timeout_active;long stop_time;
int encoder0PinALast = LOW;int n = LOW;AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);
void setup() { pinMode (encoder0PinA,INPUT); pinMode (encoder0PinB,INPUT); pinMode(enablePin, OUTPUT);
stepper.setMaxSpeed(50000); stepper.setAcceleration(1000000000); //try 100, or 1000 and see if it works
}void loop() { int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps; stepper.moveTo(encoderValue * stepsPerRotaryStep);
bool moving = stepper.run(); //moving will be true when the stepper is moving towards a target.
if(encoderValue != lastencoderValue) { lastencoderValue = encoderValue; digitalWrite (enablePin, LOW); previousMillis = millis(); destination_encoder = true; //destination set by encoder. } else
{ //Stepper sleep after 5sec of no data unsigned long currentMillis = millis (); if (currentMillis - previousMillis>sleepTimer) digitalWrite (enablePin, HIGH); } n = digitalRead(encoder0PinA); if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder0PinB) == LOW) { encoderValue--; } else { encoderValue++; } } encoder0PinALast = n;
if (timeout_active && (millis() - stop_time >= sleepTimer) && destination_encoder)
{
stepper.move(500);
destination_encoder = false; //destination set by timeout code.
previousMillis = millis();
digitalWrite(enablePin, LOW);
} if (timeout_active && (millis() - stop_time >= sleepTimer) && destination_encoder)