-
Notifications
You must be signed in to change notification settings - Fork 3
/
stepper.c
93 lines (84 loc) · 1.78 KB
/
stepper.c
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
93
#include "stepper.h"
#include "Arduino.h"
void stepper_init(stepper_t *stepper, uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3)
{
stepper->pin0 = pin0;
stepper->pin1 = pin1;
stepper->pin2 = pin2;
stepper->pin3 = pin3;
stepper->step = 0;
stepper->delay = DELAY;
pinMode(stepper->pin0, OUTPUT);
pinMode(stepper->pin1, OUTPUT);
pinMode(stepper->pin2, OUTPUT);
pinMode(stepper->pin3, OUTPUT);
}
void stepper_step(stepper_t *stepper, uint8_t direction)
{
// Calculate the next step
if (direction == FORWARD)
{
if (stepper->step == 3)
{
stepper->step = 0;
}
else
{
stepper->step++;
}
}
else
{
if (stepper->step == 0)
{
stepper->step = 3;
}
else
{
stepper->step--;
}
}
// See what step we are on
switch (stepper->step)
{
case 0:
digitalWrite(stepper->pin0, HIGH);
digitalWrite(stepper->pin1, LOW);
digitalWrite(stepper->pin2, HIGH);
digitalWrite(stepper->pin3, LOW);
break;
case 1:
digitalWrite(stepper->pin0, LOW);
digitalWrite(stepper->pin1, HIGH);
digitalWrite(stepper->pin2, HIGH);
digitalWrite(stepper->pin3, LOW);
break;
case 2:
digitalWrite(stepper->pin0, LOW);
digitalWrite(stepper->pin1, HIGH);
digitalWrite(stepper->pin2, LOW);
digitalWrite(stepper->pin3, HIGH);
break;
case 3:
digitalWrite(stepper->pin0, HIGH);
digitalWrite(stepper->pin1, LOW);
digitalWrite(stepper->pin2, LOW);
digitalWrite(stepper->pin3, HIGH);
break;
}
delay(stepper->delay);
}
void stepper_multi_step(stepper_t *stepper, uint16_t steps, uint8_t direction)
{
for (uint16_t i = 0; i < steps; i++)
{
stepper_step(stepper, direction);
}
}
void stepper_release(stepper_t *stepper)
{
digitalWrite(stepper->pin0, HIGH);
digitalWrite(stepper->pin1, HIGH);
digitalWrite(stepper->pin2, HIGH);
digitalWrite(stepper->pin3, HIGH);
}