-
Notifications
You must be signed in to change notification settings - Fork 2
/
stepper_motor_example.ino
92 lines (81 loc) · 2.49 KB
/
stepper_motor_example.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Stepper driver pin definitions
#define PIN_STEP_ENABLE 14
#define PIN_STEP1_DIRECTION 13
#define PIN_STEP1_STEP 12
//Button pin definitions
#define PIN_BUTTON_1 21
#define PIN_BUTTON_2 22
//Stepper driver variables
int stepsPerSecond = 0;
int currentPosition = 0;
void setup()
{
//Set both buttons as INPUT_PULLUP so we can detect when they are pressed
pinMode(PIN_BUTTON_1, INPUT_PULLUP);
pinMode(PIN_BUTTON_2, INPUT_PULLUP);
//Condifure stepper driver pins as OUTPUTs
pinMode(PIN_STEP1_DIRECTION, OUTPUT);
pinMode(PIN_STEP1_STEP, OUTPUT);
pinMode(PIN_STEP_ENABLE, OUTPUT);
//ENABLE pin has to be pulled LOW for TCM2209 used in this example to enable the driver
digitalWrite(PIN_STEP_ENABLE, LOW);
}
void loop()
{
//Check if the button is pressed
if (digitalRead(PIN_BUTTON_1) == LOW) {
//Forward direction
stepsPerSecond = 5000;
} else if (digitalRead(PIN_BUTTON_2) == LOW) {
//Backward direction
stepsPerSecond = -5000;
} else {
//If non of the buttons is pressed, set stepsPerSecond to 0
stepsPerSecond = 0;
}
static unsigned long nextChange = 0;
static uint8_t currentState = LOW;
if (stepsPerSecond == 0)
{
//if speed is 0, set the step pin to LOW to keep current position
currentState = LOW;
digitalWrite(PIN_STEP1_STEP, LOW);
}
else
{
//if stepsPerSecond is not 0, then we need to calculate the next time to change the state of the driver
if (micros() > nextChange)
{
//Generate steps
if (currentState == LOW)
{
currentState = HIGH;
nextChange = micros() + 30;
if (stepsPerSecond > 0)
{
currentPosition++;
}
else if (stepsPerSecond < 0)
{
currentPosition--;
}
}
else
{
currentState = LOW;
nextChange = micros() + (1000 * abs(1000.0f / stepsPerSecond)) - 30;
}
//Set direction based on the sign of stepsPerSecond
if (stepsPerSecond > 0)
{
digitalWrite(PIN_STEP1_DIRECTION, LOW);
}
else
{
digitalWrite(PIN_STEP1_DIRECTION, HIGH);
}
//Write out the step pin
digitalWrite(PIN_STEP1_STEP, currentState);
}
}
}