so im trying to make a closed loop program i already have the code i attached on this post, hope i can get some help, i can move the motor in both directions with an encoder.
stepnuevo.ino (4.02 KB)
so im trying to make a closed loop program i already have the code i attached on this post, hope i can get some help, i can move the motor in both directions with an encoder.
stepnuevo.ino (4.02 KB)
How your sketch should have been posted:
// EasyDriver connections
#define step_pin 4 // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 5 // Pin 8 connected to Direction pin
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
// Rotary Encoder Module connections
const int PinCLK = 2; // Generating interrupts using CLK signal
const int PinDT = 3; // Reading DT signal
const int PinSW = 6; // Reading Push Button switch
int long encoder0PinA = 2;
int long encoder0PinB = 3;
int long encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
float grados = 0;
int StepperPosition = 0; // To store Stepper Motor Position
int StepsToTake = 86; // Controls the speed of the Stepper per Rotary click
int direction; // Variable to set Rotation (CW-CCW) of stepper
// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect ()
{
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection = digitalRead(PinDT);
else
rotationdirection = !digitalRead(PinDT);
TurnDetected = true;
}
void setup ()
{
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(PinCLK, INPUT); // Set Pin to Input
pinMode(PinDT, INPUT);
pinMode(PinSW, INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0, rotarydetect, FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}
void loop ()
{
if (!(digitalRead(PinSW))) // check if button is pressed
{
if (StepperPosition == 0) // check if button was already pressed
{
}
else
{
if (StepperPosition > 0) // Stepper was moved CW
{
while (StepperPosition != 0) // Do until Motor position is back to ZERO
{
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++)
{
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition = StepperPosition - StepsToTake;
}
}
else
{
while (StepperPosition != 0)
{
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++)
{
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition = StepperPosition + StepsToTake;
}
}
StepperPosition = 0;
grados = 0; // Reset position to ZERO after moving motor back
}
}
// Runs if rotation was detected
if (TurnDetected)
{
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
// Which direction to move Stepper motor
if (rotationdirection) // Move motor CCW
{
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++)
{
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition = StepperPosition - StepsToTake;
}
if (!rotationdirection) // Move motor CW
{
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++)
{
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition = StepperPosition + StepsToTake;
}
}
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH))
{
if (digitalRead(encoder0PinB) == LOW)
{
encoder0Pos--;
grados = ((encoder0Pos * 360 / 20));
if (encoder0Pos == -20)
{
(encoder0Pos = 0);
}
}
else
{
encoder0Pos++;
grados = ((encoder0Pos * 360 / 20));
if (encoder0Pos == 20)
{
(encoder0Pos = 0);
}
}
Serial.print(" GRADOS = ");
Serial.println (grados);
}
encoder0PinALast = n;
}
ebenezer2301:
so im trying to make a closed loop program i already have the code i attached on this post, hope i can get some help, i can move the motor in both directions with an encoder.
What do you need help with?
What sort of encoder are you using? Is it being moved by the motor or the user's hand?
Post a link to the encoder datasheet.
...R