-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockem_arduino_test.ino
124 lines (105 loc) · 2.52 KB
/
rockem_arduino_test.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
119
120
121
122
123
124
int leftPin = 6;
int rightPin = 7;
int rightLimitPin = 11;
int leftLimitPin = 9;
int leftRightCenterPin = 13;
bool movementHaltedLeft = false;
bool movementHaltedRight = false;
// 0 Center Pin 12
// 0 In Pin 8
// 0 Out 10
// 1 In Pin 9
// 1 Out Pin 11
// 1 Center Pin 13
void setup() {
// Setup Output Pins - THEY MUST START HIGH OR THERE WILL BE FIRE
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(leftPin, HIGH);
digitalWrite(rightPin, HIGH);
// Setup Limit Switches
pinMode(rightLimitPin, INPUT);
pinMode(leftLimitPin, INPUT);
pinMode(leftRightCenterPin, INPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
int user = 0;
int moveLeftRight = 0;
int moveForwardBack = 0;
int punchLeft = 0;
int punchRight = 0;
user = Serial.parseInt();
moveLeftRight = Serial.parseInt();
moveForwardBack = Serial.parseInt();
punchLeft = Serial.parseInt();
punchRight = Serial.parseInt();
if (Serial.read() == '\n') {
handleLeftRight(moveLeftRight);
if (punchRight == 1) {
calibrate();
}
Serial.print(user, DEC);
Serial.print(moveLeftRight, DEC);
Serial.print(moveForwardBack, DEC);
Serial.print(punchLeft, DEC);
Serial.println(punchRight, DEC);
}
}
}
void calibrate() {
Serial.end();
while (!movementHaltedLeft) {
moveLeft();
}
while (digitalRead(leftRightCenterPin) == HIGH) {
moveRight();
}
Serial.begin(9600);
}
void handleLeftRight(int _moveLeftRight) {
if (_moveLeftRight == -1) {
moveRight();
} else if (_moveLeftRight == 1) {
moveLeft();
} else {
haltLeftRightMovement();
}
}
void moveRight() {
if (digitalRead(rightLimitPin) == HIGH) {
movementHaltedRight = false;
digitalWrite(rightPin, HIGH);
digitalWrite(leftPin, LOW);
} else {
movementHaltedRight = true;
haltLeftRightMovement();
}
}
void moveLeft() {
if (digitalRead(leftLimitPin) == HIGH) {
movementHaltedLeft = false;
digitalWrite(rightPin, LOW);
digitalWrite(leftPin, HIGH);
} else {
movementHaltedLeft = true;
haltLeftRightMovement();
}
}
void haltLeftRightMovement() {
digitalWrite(rightPin, HIGH);
digitalWrite(leftPin, HIGH);
}