-
Notifications
You must be signed in to change notification settings - Fork 4
/
arduino_code
97 lines (72 loc) · 2.13 KB
/
arduino_code
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
94
95
96
97
// include stepper library
#include <Stepper.h>
const int stepsPerRevolution = 200; // Nema17 motor has 200 steps in 360 degrees rotation
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // pins 8,9 go to PULL+ & PULL- , pins 10,11 go to DIR+ & DIR-
int tracknumber = 2; // SET HOW MANY TRACKS YOU WANT UP AND BACK
int trackdistance = 20000;
int actualtracknumber = 0; // current track number
int motiondirection = 2; // set initial forward motor increments.
int forwardsincrement = 2; // forward
int backwardsincrement = -2; // backwards
int moves = 0; // current move number
int goforit = 1; // start or stop the move ?
int topspeed = 3201; // TOP SPEED OF TRACKING
int bottomspeed = 1; // start and end speed of RAMP up/down
int benspeed = 1; // current speed
void setup() {
// do some setup here
}
void loop() {
if (goforit == 1)
{
moveit();
}
else
{
donothing();
}
}
void moveit()
{
int x = 1;
for (int i = 50; i > -1; i = i + x ) {
if (i >= topspeed)
{
topspeedhold(); // hold top speed for trackdistance variable
x = -1; // switch direction at peak
}
if (i <= bottomspeed)
{
x = +1; // switch direction at troff
moves++;
actualtracknumber++;
}
int benspeed=i;
myStepper.setSpeed(benspeed);
myStepper.step(motiondirection);
if (moves >=1) motiondirection = backwardsincrement; // switch direction
if (moves >=2) motiondirection = forwardsincrement , moves = 0;
if (actualtracknumber >= tracknumber * 2 ) // when the motor has done forward and back the TRACKNUMBER amount set then back to void();
{
goforit = 0;
return;
}
}
}
void topspeedhold() // hold top speed for trackdistance variable
{
int f = 1;
for (int f = 50; f > -1; f = f + 1 ) {
if (f >= trackdistance)
{
return;
}
myStepper.setSpeed(topspeed);
myStepper.step(motiondirection);
}
}
void donothing()
{
while(1);
}