-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstepper_motor.cpp
61 lines (50 loc) · 1.39 KB
/
stepper_motor.cpp
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
#include "stepper_motor.h"
StepperMotor::StepperMotor() {
};
StepperMotor::StepperMotor(int steps, int pin1, int pin2, unsigned long rpm) {
this->steps = steps;
this->pin1 = pin1;
this->pin2 = pin2;
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
this->stepDelay = 60L * 1000L * 1000L / this->steps / rpm;
};
void StepperMotor::setSteps(int steps) {
this->stepsToMove = steps;
};
int StepperMotor::step() {
int stepsMoved = 0;
unsigned long now = micros();
if (this->stepsToMove > 0) {
if (now - this->lastStepTime >= this->stepDelay) {
this->lastStepTime = now;
this->stepMotor(this->stepsToMove % 4);
this->stepsToMove--;
stepsMoved = 1;
}
}
return stepsMoved;
}
bool StepperMotor::isFinished() {
return this->stepsToMove == 0;
}
void StepperMotor::stepMotor(int thisStep) {
switch(thisStep) {
case 0:
digitalWrite(this->pin1, LOW);
digitalWrite(this->pin2, HIGH);
break;
case 1:
digitalWrite(this->pin1, HIGH);
digitalWrite(this->pin2, HIGH);
break;
case 2:
digitalWrite(this->pin1, HIGH);
digitalWrite(this->pin2, LOW);
break;
case 3:
digitalWrite(this->pin1, LOW);
digitalWrite(this->pin2, LOW);
break;
}
};