-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ino
More file actions
213 lines (185 loc) · 6.83 KB
/
Copy pathmain.ino
File metadata and controls
213 lines (185 loc) · 6.83 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <Bluepad32.h>
// PIN CONNECTIONS
int ENApin = 4; // Motor 1 PWM
int IN1pin = 13; // Motor 1 direction
int IN2pin = 26; // Motor 1 direction
int IN3pin = 14; // Motor 2 direction
int IN4pin = 27; // Motor 2 direction
int ENBpin = 5; // Motor 2 PWMW
int servoPin = 25; // Pin servo
const int ledPins[] = {16, 23, 17};
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int bumperPins[] = {25, 12}; // LEFT and RIGHT
int remainingLives = 3;
bool bumperState = false;
ControllerPtr myControllers[BP32_MAX_GAMEPADS];
// This callback gets called any time a new gamepad is connected.
// Up to 4 gamepads can be connected at the same time.
void onConnectedController(ControllerPtr ctl) {
bool foundEmptySlot = false;
for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
if (myControllers[i] == nullptr) {
Serial.printf("CALLBACK: Controller is connected, index=%d\n", i);
// Additionally, you can get certain gamepad properties like:
// Model, VID, PID, BTAddr, flags, etc.
ControllerProperties properties = ctl->getProperties();
Serial.printf("Controller model: %s, VID=0x%04x, PID=0x%04x\n", ctl->getModelName().c_str(), properties.vendor_id, properties.product_id);
myControllers[i] = ctl;
foundEmptySlot = true;
break;
}
}
if (!foundEmptySlot) {
Serial.println("CALLBACK: Controller connected, but could not found empty slot");
}
}
void onDisconnectedController(ControllerPtr ctl) {
bool foundController = false;
for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
if (myControllers[i] == ctl) {
Serial.printf("CALLBACK: Controller disconnected from index=%d\n", i);
myControllers[i] = nullptr;
foundController = true;
break;
}
}
if (!foundController) {
Serial.println("CALLBACK: Controller disconnected, but not found in myControllers");
}
}
// == SEE CONTROLLER VALUES IN SERIAL MONITOR == //
void dumpGamepad(ControllerPtr ctl) {
Serial.printf(
"idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: %4d, %4d, brake: %4d, throttle: %4d, "
"misc: 0x%02x, gyro x:%6d y:%6d z:%6d, accel x:%6d y:%6d z:%6d\n",
ctl->index(), // Controller Index
ctl->dpad(), // D-pad
ctl->buttons(), // bitmask of pressed buttons
ctl->axisX(), // (-511 - 512) left X Axis
ctl->axisY(), // (-511 - 512) left Y axis
ctl->axisRX(), // (-511 - 512) right X axis
ctl->axisRY(), // (-511 - 512) right Y axis
ctl->brake(), // (0 - 1023): brake button
ctl->throttle(), // (0 - 1023): throttle (AKA gas) button
ctl->miscButtons(), // bitmask of pressed "misc" buttons
ctl->gyroX(), // Gyro X
ctl->gyroY(), // Gyro Y
ctl->gyroZ(), // Gyro Z
ctl->accelX(), // Accelerometer X
ctl->accelY(), // Accelerometer Y
ctl->accelZ() // Accelerometer Z
);
}
// == GAME CONTROLLER ACTIONS SECTION == //
void processGamepad(ControllerPtr ctl) {
if(remainingLives > 0){
//== LEFT JOYSTICK - UP ==//
if (ctl->axisY() <= -25) {
// map joystick values to motor speed
int motorSpeed = map(ctl->axisY(), -25, -508, 70, 255);
// move motors/robot forward
digitalWrite(IN1pin, HIGH);
digitalWrite(IN2pin, LOW);
analogWrite(ENApin, motorSpeed);
digitalWrite(IN3pin, LOW);
digitalWrite(IN4pin, HIGH);
analogWrite(ENBpin, motorSpeed);
}
//== LEFT JOYSTICK - DOWN ==//
if (ctl->axisY() >= 25) {
// map joystick values to motor speed
int motorSpeed = map(ctl->axisY(), 25, 512, 70, 255);
// move motors/robot in reverse
digitalWrite(IN1pin, LOW);
digitalWrite(IN2pin, HIGH);
analogWrite(ENApin, motorSpeed);
digitalWrite(IN3pin, HIGH);
digitalWrite(IN4pin, LOW);
analogWrite(ENBpin, motorSpeed);
}
//== RIGHT JOYSTICK - LEFT ==//
if (ctl->axisRX() <= -25) {
// map joystick values to motor speed
int motorSpeed = map(ctl->axisRX(), -25, -508, 70, 255);
// turn robot left - move right motor forward, keep left motor still
digitalWrite(IN1pin, HIGH);
digitalWrite(IN2pin, LOW);
analogWrite(ENApin, motorSpeed);
digitalWrite(IN3pin, HIGH);
digitalWrite(IN4pin, LOW);
analogWrite(ENBpin, motorSpeed);
}
//== RIGHT JOYSTICK - RIGHT ==//
if (ctl->axisRX() >= 25) {
// map joystick values to motor speed
int motorSpeed = map(ctl->axisRX(), 25, 512, 70, 255);
// turn robot right - move left motor forward, keep right motor still
digitalWrite(IN1pin, LOW);
digitalWrite(IN2pin, HIGH);
analogWrite(ENApin, motorSpeed);
digitalWrite(IN3pin, LOW);
digitalWrite(IN4pin, HIGH);
analogWrite(ENBpin, motorSpeed);
}
//== LEFT JOYSTICK DEADZONE ==//
if (ctl->axisY() > -25 && ctl->axisY() < 25 && ctl->axisRX() > -25 && ctl->axisRX() < 25) {
// keep motors off
analogWrite(ENApin,0);
analogWrite(ENBpin, 0);
}
dumpGamepad(ctl);
}
}
void processControllers() {
for (auto myController : myControllers) {
if (myController && myController->isConnected() && myController->hasData()) {
if (myController->isGamepad()) {
processGamepad(myController);
}
else {
Serial.println("Unsupported controller");
}
}
}
}
void setup() {
pinMode(ENApin, OUTPUT);
pinMode(IN1pin, OUTPUT);
pinMode(IN2pin, OUTPUT);
pinMode(IN3pin, OUTPUT);
pinMode(IN4pin, OUTPUT);
pinMode(ENBpin, OUTPUT);
for(int i = 0; i < numLeds; i++) pinMode(ledPins[i], OUTPUT);
for(int i = 0; i < numLeds; i++) digitalWrite(ledPins[i], HIGH);
pinMode(bumperPins[0], INPUT_PULLUP);
pinMode(bumperPins[1], INPUT_PULLUP);
Serial.begin(115200);
Serial.printf("Firmware: %s\n", BP32.firmwareVersion());
const uint8_t* addr = BP32.localBdAddress();
Serial.printf("BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
// Setup the Bluepad32 callbacks
BP32.setup(&onConnectedController, &onDisconnectedController);
// "forgetBluetoothKeys()" should be called when the user performs
BP32.forgetBluetoothKeys();
// Enables mouse / touchpad support for gamepads that support them.
BP32.enableVirtualDevice(false);
}
void loop() {
bool dataUpdated = BP32.update();
if (dataUpdated)
processControllers();
if(remainingLives > 0){
if ((digitalRead(bumperPins[0]) == LOW || digitalRead(bumperPins[1]) == LOW) && !bumperState) {
digitalWrite(ledPins[remainingLives-1], LOW);
remainingLives--;
bumperState = true;
}else if(digitalRead(bumperPins[0]) == HIGH && digitalRead(bumperPins[1]) == HIGH){
bumperState = false;
}
}else{
for(int i = 0; i < numLeds; i++) digitalWrite(ledPins[i], LOW);
delay(1000);
for(int i = 0; i < numLeds; i++) digitalWrite(ledPins[i], HIGH);
}
delay(50);
}