-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstepper_encoder_test_simple.ino
44 lines (40 loc) · 1.08 KB
/
stepper_encoder_test_simple.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Goal: Return motor to initial position after allowing it to be turned by hand
#include <AccelStepper.h>
#define DIR_PIN 5
#define STEP_PIN 6
#define SLEEP_PIN 12
#define STEPS 200
#define MICROSTEPS 8
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
#include <Encoder.h>
#define ENCODER1 2
#define ENCODER2 3
#define ENCODER_STEPS 1200
Encoder encoder(ENCODER1, ENCODER2);
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(500);
stepper.setAcceleration(500);
stepper.setEnablePin(SLEEP_PIN);
stepper.disableOutputs();
}
void loop()
{
float encoder_position;
float microsteps;
encoder_position = encoder.read();
microsteps = encoder_position * ((((float)STEPS * (float)MICROSTEPS) / (float)ENCODER_STEPS));
Serial.print("encoder position = ");
Serial.println(encoder_position);
Serial.print("microsteps = ");
Serial.println(microsteps);
if (encoder_position > 15.0 || encoder_position < -15.0)
{
stepper.enableOutputs();
stepper.runToNewPosition(microsteps);
stepper.setCurrentPosition(0);
stepper.disableOutputs();
}
delay(1000);
}