-
Notifications
You must be signed in to change notification settings - Fork 1
/
bike.ino
82 lines (67 loc) · 2.13 KB
/
bike.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
#include "config.h"
#ifdef JOYSTICKSHIELD
#define pinButtonR (uint8_t)2 // Up button
#define pinButtonA (uint8_t)3 // Right button
#define pinButtonB (uint8_t)4 // Down button
#define pinButtonL (uint8_t)5 // Left button
#define pinButtonAdd (uint8_t)7 // F button
#define pinHallSensor (uint8_t)13
#else
#define pinInterruptMPU (uint8_t)0
#define pinButtonB (uint8_t)1
// Pins 2 and 3 and for SCA and SCL of the MPU6050
#define pinButtonA (uint8_t)4
#define pinButtonAdd (uint8_t)5
#define pinButtonR (uint8_t)6
#define pinButtonL (uint8_t)7
#define pinHallSensor (uint8_t)8
#define pinSPTDLeftJS (uint8_t)9
#define pinRotationLED (uint8_t)12
#define pinSpeedLED (uint8_t)13
#endif
const uint8_t buttons[] = { pinButtonR, pinButtonA, pinButtonB, pinButtonL, pinButtonAdd };
#define pinLAnalogX (uint8_t)A0
#define pinLAnalogY (uint8_t)A1
#include <I2Cdev.h>
#include <MPU6050_6Axis_MotionApps20.h>
#include <Wire.h>
#include <SwitchJoystick.h>
boolean gameStarted = false;
void setup() {
for (int i = 0; i < 5; i++) pinMode(buttons[i], INPUT_PULLUP); // TODO: check what this implies when it isn't using the JoystickShield
#ifndef JOYSTICKSHIELD
// pinMode(pinLAnalogX, INPUT); // TODO: check what this implies when it isn't using the JoystickShield
// pinMode(pinLAnalogY, INPUT); // TODO: check what this implies when it isn't using the JoystickShield
pinMode(pinSPTDLeftJS, INPUT);
pinMode(pinSpeedLED, OUTPUT);
pinMode(pinRotationLED, OUTPUT);
i2cSetup();
MPU6050Connect();
#endif
pinMode(pinHallSensor, INPUT);
resetFrequency();
initializeSwitch();
#ifndef JOYSTICKSHIELD
updateLEDsGameUnstarted();
#endif
}
void loop() {
// Wait for the A button to start playing.
if (gameStarted) {
updateGameData();
sendGameData();
delay(50);
} else {
updateGameStarted();
delay(100);
}
}
void updateGameStarted() {
gameStarted = checkGameStarted();
#ifndef JOYSTICKSHIELD
updateLEDsGameStarted(gameStarted);
#endif
}
boolean checkGameStarted() {
return (digitalRead(pinButtonA) == LOW);
}