-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.cpp
69 lines (60 loc) · 1.61 KB
/
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
62
63
64
65
66
67
68
69
#include <Stepper.h>
#include "Arduino.h"
#include "motor.h"
#include "melody.h"
#include "memory.h"
// 2036 is one full turn. Must be divisible by 4 as the motor is 4 step
#define ONE_TURN 2036
// Max reliable speed: 10
#define SPEED 8
// Number of steps required to fully open or close gate
#define STEPS_COUNT 8800
Stepper stepper = Stepper(ONE_TURN, STEPPER_PIN_1, STEPPER_PIN_2, STEPPER_PIN_3, STEPPER_PIN_4);
void powerUpMotor () {
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
pinMode(MOTOR_POWER_PIN, OUTPUT);
digitalWrite(MOTOR_POWER_PIN, LOW);
stepper.setSpeed(SPEED);
};
void powerDownMotor () {
digitalWrite(MOTOR_POWER_PIN, HIGH);
pinMode(STEPPER_PIN_1, INPUT);
pinMode(STEPPER_PIN_2, INPUT);
pinMode(STEPPER_PIN_3, INPUT);
pinMode(STEPPER_PIN_4, INPUT);
}
void openGate () {
if(readFromMemory(STATE_ADDRESS) == OPEN) {
return;
}
playMelody(melodyUp, melodyUpLength);
powerUpMotor();
int count = 0;
while (count < STEPS_COUNT) {
stepper.step(-1);
count++;
// NOTE: we must use yield, otherwise ESP will restart after some time
yield();
}
powerDownMotor();
writeToMemory(STATE_ADDRESS, OPEN);
};
void closeGate () {
if(readFromMemory(STATE_ADDRESS) == CLOSE) {
return;
}
playMelody(melodyDown, melodyDownLength);
powerUpMotor();
int count = 0;
while (count < STEPS_COUNT) {
stepper.step(1);
count++;
// NOTE: we must use yield, otherwise ESP will restart after some time
yield();
}
powerDownMotor();
writeToMemory(STATE_ADDRESS, CLOSE);
};