-
Notifications
You must be signed in to change notification settings - Fork 3
/
EX-Turntable.ino
118 lines (103 loc) · 3.46 KB
/
EX-Turntable.ino
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
115
116
117
118
/*
* © 2023 Peter Cole
* © 2022 Peter Cole
*
* This file is part of EX-Turntable
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EX-Turntable. If not, see <https://www.gnu.org/licenses/>.
*/
// Include required libraries.
#include <Arduino.h>
#include "defines.h"
// Include local files
#include "IOFunctions.h"
#include "TurntableFunctions.h"
bool lastRunningState; // Stores last running state to allow turning the stepper off after moves.
void setup() {
// Run startup configuration
startupConfiguration();
// Set up the stepper driver
setupStepperDriver();
// If we're not sensor testing, start Wire()
if (!sensorTesting) setupWire();
// Display EX-Turntable configuration
displayTTEXConfig();
}
void loop() {
// If we're only testing sensors, don't do anything else.
if (sensorTesting) {
bool testHomeSensorState = getHomeState();
if (testHomeSensorState != homeSensorState) {
if (testHomeSensorState == HOME_SENSOR_ACTIVE_STATE) {
Serial.println(F("Home sensor ACTIVATED"));
} else {
Serial.println(F("Home sensor DEACTIVATED"));
}
homeSensorState = testHomeSensorState;
}
getHomeState();
#if TURNTABLE_EX_MODE == TRAVERSER
bool testLimitSensorState = getLimitState();
if (testLimitSensorState != limitSensorState) {
if (testLimitSensorState == LIMIT_SENSOR_ACTIVE_STATE) {
Serial.println(F("Limit sensor ACTIVATED"));
} else {
Serial.println(F("Limit sensor DEACTIVATED"));
}
limitSensorState = testLimitSensorState;
}
#endif
} else {
#if TURNTABLE_EX_MODE == TRAVERSER
// If we hit our limit switch when not calibrating, stop!
if (getLimitState() == LIMIT_SENSOR_ACTIVE_STATE && !calibrating && stepper.isRunning() && stepper.targetPosition() < 0) {
Serial.println(F("ALERT! Limit sensor activitated, halting stepper"));
if (!homed) {
homed = 1;
}
stepper.stop();
stepper.setCurrentPosition(stepper.currentPosition());
}
// If we hit our home switch when not homing, stop!
if (getHomeState() == HOME_SENSOR_ACTIVE_STATE && homed && !calibrating && stepper.isRunning() && stepper.distanceToGo() > 0) {
Serial.println(F("ALERT! Home sensor activitated, halting stepper"));
stepper.stop();
stepper.setCurrentPosition(0);
}
#endif
// If we haven't successfully homed yet, do it.
if (homed == 0) {
moveHome();
}
// If flag is set for calibrating, do it.
if (calibrating) {
calibration();
}
// Process the stepper object continuously.
stepper.run();
// Process our LED.
processLED();
// If disabling on idle is enabled, disable the stepper.
#if defined(DISABLE_OUTPUTS_IDLE)
if (stepper.isRunning() != lastRunningState) {
lastRunningState = stepper.isRunning();
if (!lastRunningState) {
stepper.disableOutputs();
}
}
#endif
}
// Receive and process and serial input for test commands.
processSerialInput();
}