/
arduino_2.cpp
112 lines (100 loc) · 2.92 KB
/
arduino_2.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
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
#include <Arduino.h>
#define B_DIR_PIN 5
#define B_STEP_PIN 2
#define D_DIR_PIN 6
#define D_STEP_PIN 3
#define L_DIR_PIN 7
#define L_STEP_PIN 4
#define EN 8
#define X_ENDSTOP 9
#define Y_ENDSTOP 10
#define Z_ENDSTOP 11
#define ABORT A0
#define HOLD A1
#define RESUME A2
#define SPEED_FACTOR 160
#define DEGREE_FACTOR 90
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do somethistrng about it:
stringComplete = true;
}
}
void rotateDeg(int dir_pin, int step_pin, float deg, float speed) {
int dir = (deg > 0) ? HIGH : LOW;
digitalWrite(dir_pin, dir);
int steps = abs(deg) * (1 / 0.225);
int usDelay = (1 / speed) * SPEED_FACTOR;
for (int i = 0; i < steps; i++) {
digitalWrite(step_pin, HIGH);
delayMicroseconds(usDelay);
digitalWrite(step_pin, LOW);
delayMicroseconds(usDelay);
}
}
void setup() {
Serial.begin(38400);
inputString.reserve(200);
pinMode(EN, OUTPUT);
pinMode(B_DIR_PIN, OUTPUT);
pinMode(B_STEP_PIN, OUTPUT);
pinMode(D_DIR_PIN, OUTPUT);
pinMode(D_STEP_PIN, OUTPUT);
pinMode(L_DIR_PIN, OUTPUT);
pinMode(L_STEP_PIN, OUTPUT);
pinMode(X_ENDSTOP, INPUT_PULLUP);
pinMode(Y_ENDSTOP, INPUT_PULLUP);
pinMode(Z_ENDSTOP, INPUT_PULLUP);
pinMode(ABORT, INPUT_PULLUP);
pinMode(HOLD, INPUT_PULLUP);
pinMode(RESUME, INPUT_PULLUP);
digitalWrite(EN, LOW);
}
void loop() {
if (stringComplete) {
for (int i = 0; i < inputString.length(); i++) {
if (inputString[i] == 'B') {
rotateDeg(B_DIR_PIN, B_STEP_PIN, DEGREE_FACTOR, 1);
}
else if (inputString[i] == 'b') {
rotateDeg(B_DIR_PIN, B_STEP_PIN, -DEGREE_FACTOR, 1);
}
else if (inputString[i] == 'D') {
rotateDeg(D_DIR_PIN, D_STEP_PIN, DEGREE_FACTOR, 1);
}
else if (inputString[i] == 'd') {
rotateDeg(D_DIR_PIN, D_STEP_PIN, -DEGREE_FACTOR, 1);
}
else if (inputString[i] == 'L') {
rotateDeg(L_DIR_PIN, L_STEP_PIN, DEGREE_FACTOR, 1);
}
else if (inputString[i] == 'l') {
rotateDeg(L_DIR_PIN, L_STEP_PIN, -DEGREE_FACTOR, 1);
}
delay(300);
Serial.println(inputString[i]);
inputString = "";
stringComplete = false;
}
}
}
int main(void)
{
init();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}