-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmkcycle.ino
More file actions
210 lines (186 loc) · 4.18 KB
/
smkcycle.ino
File metadata and controls
210 lines (186 loc) · 4.18 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
// OPTIONS
const boolean IS_SNES = true;
const long SNES_MINIMUM_SPEED = 200;
const long SNES_PEDAL_RELEASE_DELAY = 500;
// INPUTS
const int PEDAL_IN = A0;
const int START_IN = 8;
const int LEFT_IN = 10;
const int RIGHT_IN = 9;
// OUTPUTS (For SNES)
const int A_BUTTON = 2;
const int B_BUTTON = 3;
const int DOWN_BUTTON = 4;
const int RIGHT_BUTTON = 5;
const int LEFT_BUTTON = 6;
const int UP_BUTTON = 7;
// OUTPUTS (For Unity ArduinoBikeProxy.cs)
const String RESET_FLAG = "^";
const String MESSAGE_DELIMITER = "|";
// Pedal Variables
long lastPedal = 0;
long timeSinceLastPedal = 0;
int lastPedalIn = 0;
boolean hasPedalled = false;
boolean _stopped = true;
// Button Variables
boolean aPressed = false;
boolean startPressed = false;
boolean rightPressed = false;
boolean leftPressed = false;
// Loop Variables
long lastLoop = millis();
void setup() {
// SNES Button Outs
if (IS_SNES){
pinMode(B_BUTTON, OUTPUT);
pinMode(A_BUTTON, OUTPUT);
pinMode(LEFT_BUTTON, OUTPUT);
pinMode(RIGHT_BUTTON, OUTPUT);
}
// Button and Pedal inputs
pinMode(START_IN, INPUT);
pinMode(LEFT_IN, INPUT);
pinMode(RIGHT_IN, INPUT);
pinMode(PEDAL_IN, INPUT);
// set pullup on analog pin 0
digitalWrite(PEDAL_IN, HIGH);
// Using a digital pin as +5V
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
if(IS_SNES) {
Serial.begin(9600);
} else {
Serial.begin(57600);
Serial.print(RESET_FLAG);
}
}
void loop() {
if ( millis() - lastLoop > 100){
lastLoop = millis();
checkRight();
checkLeft();
checkStart();
}
checkPedal();
}
void sPrint(String message){
if (!IS_SNES){
String out = "";
out += MESSAGE_DELIMITER;
out += message;
out += MESSAGE_DELIMITER;
Serial.println(out);
}else{
Serial.println(message);
}
}
void checkRight(){
// I used inverted buttons by accident
// easier to fix in software for now
if (digitalRead(RIGHT_IN) == HIGH){
if (rightPressed){
sPrint(String("right:0"));
rightPressed = false;
if (IS_SNES){
digitalWrite(RIGHT_BUTTON, LOW);
}
}
}else{
if (!rightPressed){
sPrint(String("right:1"));
rightPressed = true;
if (IS_SNES){
digitalWrite(RIGHT_BUTTON, HIGH);
}
}
}
}
void checkLeft(){
// I used inverted buttons by accident
// easier to fix in software for now
if (digitalRead(LEFT_IN) == HIGH){
if (leftPressed){
sPrint(String("left:0"));
leftPressed = false;
if (IS_SNES){
digitalWrite(LEFT_BUTTON, LOW);
}
}
}else{
if (!leftPressed){
sPrint(String("left:1"));
leftPressed = true;
if (IS_SNES){
digitalWrite(LEFT_BUTTON, HIGH);
}
}
}
}
void checkStart(){
// I used inverted buttons by accident
// easier to fix in software for now
if (digitalRead(START_IN) == HIGH){
if (startPressed){
sPrint(String("start:0"));
startPressed = false;
if (IS_SNES){
digitalWrite(B_BUTTON, LOW);
}
}
}else{
if (!startPressed){
sPrint(String("start:1"));
startPressed = true;
if (IS_SNES){
digitalWrite(B_BUTTON, HIGH);
}
}
}
}
void checkPedal(){
// check if a pedal state has changed
if (digitalRead(PEDAL_IN) != lastPedalIn){
lastPedalIn = digitalRead(PEDAL_IN);
if (lastPedalIn == 0){
pedalStateChange();
}
}
// if a certain amount of time has passed we can assume
// that the rider has stopped peddeling.
if (hasPedalled){
if (!_stopped && millis() - lastPedal > SNES_PEDAL_RELEASE_DELAY){
_stopped = true;
sPrint(String("stop:1"));
releaseA();
}
}
}
void pedalStateChange(){
hasPedalled = true;
timeSinceLastPedal = millis() - lastPedal;
lastPedal = millis();
sPrint(String("speed:") += String(timeSinceLastPedal));
_stopped = false;
if (IS_SNES){
if (timeSinceLastPedal < SNES_MINIMUM_SPEED){
pressA();
}else{
releaseA();
}
}
}
void pressA(){
if (!aPressed){
Serial.println("Press A");
}
aPressed = true;
digitalWrite(A_BUTTON,HIGH);
}
void releaseA(){
if (aPressed){
Serial.println("Release A");
}
aPressed = false;
digitalWrite(A_BUTTON,LOW);
}