-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller
More file actions
92 lines (76 loc) · 1.85 KB
/
Copy pathcontroller
File metadata and controls
92 lines (76 loc) · 1.85 KB
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
#define DIR_PIN 2
#define STEP_PIN 3
#define MS1 4
#define MS2 5
#define EN 6
char user_input;
int x;
int y;
int state;
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
Serial.begin(9600); //Open Serial connection for debugging
Serial.println("Begin motor control");
}
void loop(){
while(Serial.available()){
user_input = Serial.read(); //Read user input and trigger appropriate function
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (user_input =='1')
{
rotate(1000, .2);
}
else if(user_input =='2')
{
rotate(-1000, 0.2);
}
else if(user_input =='3')
{
rotate(25000, 1); //forward
}
else if(user_input =='4')
{
rotate(-25000, 1); //reverse
}
else if(user_input =='5')
{
for(int j=0; j < 250; j++){
rotate(75, .1);
delay(500);
}
}
else if(user_input =='6')
{
rotate(8000, .1); //forward
}
else
{
Serial.println("Operation complete");
}
//resetEDPins();
}
}
void rotate(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN,dir);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
//Reset Easy Driver pins to default states
void resetEDPins()
{
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIR_PIN, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}