-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStepperMotor.h
More file actions
114 lines (100 loc) · 2.47 KB
/
Copy pathStepperMotor.h
File metadata and controls
114 lines (100 loc) · 2.47 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class StepperMotor {
private:
const int ENABLE = D8;
const int DIR = D7;
const int STEP = D6;
const int DIRR = D5;
const int LED = D3;
bool constructed;
int maxSpeed = 850;
int thisSpeed = 500; //0-1023 (1-1024)
public:
bool running;
bool clockwise;
int disabled;
int multiplier = 50;//50*0 to 50*1023 = 51150
StepperMotor() {}
void begin() {
pinMode(D3, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
constructed = true;
Serial.print("Motor PINS - Enable: " + String(D8) + "; DIR: " + String(D7) + "; DIRR: " + String(D5) + "; STEP: " + String(D6));
off();
}
int speed(int value = -1) {
if (value != -1) thisSpeed = value;
return thisSpeed;
}
void run() {
if (constructed == false or running == false) {return;}
digitalWrite(STEP, HIGH);
int motorDelay = (1024 - thisSpeed) * multiplier; //constant acceleration
motorDelay = motorDelay / 1024.0 * (1024.0 * 1.1 - thisSpeed); //exponential acceleration
motorDelay += maxSpeed;
delayMicroseconds(motorDelay);
digitalWrite(STEP, LOW);
delayMicroseconds(motorDelay);
}
void forward() {
if (constructed == false) {return;}
running = true;
clockwise = true;
on();
digitalWrite(DIR, HIGH);
digitalWrite(DIRR, LOW);
}
void backward() {
if (constructed == false) {return;}
running = true;
clockwise = false;
on();
digitalWrite(DIR, LOW);
digitalWrite(DIRR, HIGH);
}
void stop() {
if (constructed == false) {return;}
running = false;
if (disabled == 0) off();
}
void on() {
if (constructed == false) {return;}
analogWrite(LED, 177);
digitalWrite(ENABLE, LOW);
}
void off() {
if (constructed == false) {return;}
digitalWrite(LED, LOW);
digitalWrite(ENABLE, HIGH);
}
bool command(String payload) {
payload.toUpperCase();
if (payload == "OFF") {
off();
} else if (payload == "ON") {
on();
} else if (payload == "STOP") {
stop();
} else if (payload == "FORWARD") {
forward();
} else if (payload == "BACKWARD") {
backward();
} else {
return false;
}
return true;
}
String state() {
if (digitalRead(ENABLE) == HIGH) {
return "OFF";
} else if (running == false) {
return "STOP";
} else if (clockwise == true) {
return "FORWARD";
} else {
return "BACKWARD";
}
}
};