-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinball.ino
240 lines (222 loc) · 4.61 KB
/
Pinball.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Solenoids
int leftFlipperSolenoid = 30;
int rightFlipperSolenoid = 31;
// User Buttons
int leftFlipperButton = A0;
int rightFlipperButton = A1;
int gameStart = A2;
// Testing Buttons
// Playfield Switches
int troughSwitch = A3;
int fSwitch = A4;
int oneOneSwitch = A5;
int oneTwoSwitch = A6;
int oneThreeSwitch = A7;
// Playfield Lights
int shootAgain = 39;
int Bonus1k = 40;
int Bonus2k = 41;
int Bonus4k = 42;
int Bonus8k = 43;
int Bonus16k = 44;
int fLight = 45;
int oneOne = 46;
int oneTwo = 47;
int oneThree = 48;
int gameOver = 49;
// Score
int score = 0; // The score, will be printed to the serial monitor as I have no
// time to implement a scoreboard.
// Assorted other variables
int fState = 0;
int oneOneState = 0;
int oneTwoState = 0;
int oneThreeState = 0;
int shootAgainState = 0;
int gameOverState = 1;
int bonus = 0; // Value of the bonus divided by 1000
void setup()//No plan survives first contact with the enemy.
{
// Setup all the things.
Serial.begin(9600);
pinMode(leftFlipperSolenoid, OUTPUT);
pinMode(rightFlipperSolenoid, OUTPUT);
pinMode(leftFlipperButton, INPUT);
pinMode(rightFlipperButton, INPUT);
pinMode(troughSwitch, INPUT);
pinMode(fSwitch, INPUT);
pinMode(oneOneSwitch, INPUT);
pinMode(oneTwoSwitch, INPUT);
pinMode(oneThreeSwitch, INPUT);
pinMode(shootAgain, OUTPUT);
pinMode(gameOver, OUTPUT);
pinMode(Bonus1k, OUTPUT);
pinMode(Bonus2k, OUTPUT);
pinMode(Bonus4k, OUTPUT);
pinMode(Bonus8k, OUTPUT);
pinMode(Bonus16k, OUTPUT);
pinMode(oneOne, OUTPUT);
pinMode(oneTwo, OUTPUT);
pinMode(oneThree, OUTPUT);
}
void loop()
{
while (gameOverState != 1)
{
digitalWrite(gameOver,LOW);
if (digitalRead(leftFlipperButton) == 1)
{
fireLeftFlipper(1);
}
else
{
fireLeftFlipper(0);
}
if (digitalRead(rightFlipperButton) == 1)
{
fireRightFlipper(1);
}
else
{
fireRightFlipper(0);
}
bonusHandler();
f111LightsHandler();
shootAgainHandler();
}
digitalWrite(gameOver,HIGH);
if (digitalRead(gameStart)==HIGH){
gameOverState = 0;
}
}
void fireLeftFlipper(int fireState)
{
if (fireState == 1)
{
digitalWrite(leftFlipperSolenoid, HIGH);
// Serial.println("test");
}
else
{
digitalWrite(leftFlipperSolenoid, LOW);
}
}
void fireRightFlipper(int fireState)
{
if (fireState == 1)
{
digitalWrite(rightFlipperSolenoid, HIGH);
}
else
{
digitalWrite(rightFlipperSolenoid, LOW);
}
}
void bonusHandler()
{ // Handles the value of the bonus, stored in a single
// byte as the number of values isn't that large.
if (bitRead(bonus, 0) == 1)
{
digitalWrite(Bonus1k, HIGH);
}
else
{
digitalWrite(Bonus1k, LOW);
}
if (bitRead(bonus, 1) == 1)
{
digitalWrite(Bonus2k, HIGH);
}
else
{
digitalWrite(Bonus2k, LOW);
}
if (bitRead(bonus, 2) == 1)
{
digitalWrite(Bonus4k, HIGH);
}
else
{
digitalWrite(Bonus4k, LOW);
}
if (bitRead(bonus, 3) == 1)
{
digitalWrite(Bonus8k, HIGH);
}
else
{
digitalWrite(Bonus8k, LOW);
}
if (bitRead(bonus, 4) == 1)
{
digitalWrite(Bonus16k, HIGH);
}
else
{
digitalWrite(Bonus16k, LOW);
}
}
void f111LightsHandler()
{
if (digitalRead(fSwitch) == HIGH && fState != 1)
{
fState = 1;
digitalWrite(fLight, HIGH);
score = score + 100;
}
if (digitalRead(oneOneSwitch) == HIGH && oneOneState != 1)
{
oneOneState = 1;
digitalWrite(oneOne, HIGH);
score = score + 100;
}
if (digitalRead(oneTwoSwitch) == HIGH && oneTwoState != 1)
{
oneTwoState = 1;
digitalWrite(oneTwo, HIGH);
score = score + 100;
}
if (digitalRead(oneThreeSwitch) == HIGH && oneThreeState != 1)
{
oneThreeState = 1;
digitalWrite(oneThree, HIGH);
score = score + 100;
}
if (fState == 1 && oneOneState == 1 && oneTwoState == 1 && oneThreeState == 1)
{
shootAgainState = 1;
digitalWrite(fLight, LOW);
digitalWrite(oneOne, LOW);
digitalWrite(oneTwo, LOW);
digitalWrite(oneThree, LOW);
fState = 0;
oneOneState = 0;
oneTwoState = 0;
oneThreeState = 0;
}
}
void shootAgainHandler(){
if(shootAgainState==1){
digitalWrite(shootAgain,HIGH);
}
else{
digitalWrite(shootAgain,LOW);
}
}
void gameOverHandler(){
if(digitalRead(troughSwitch)== HIGH && shootAgainState == 1){
shootAgainState = 0;
}
else if (digitalRead(troughSwitch)==HIGH && shootAgainState == 0){//We broke it Jim
Serial.print("Final score is ");
Serial.print(score);
Serial.println(" points, Well done!");
gameOverState=1;
oneOneState = 0;
oneTwoState = 0;
oneThreeState = 0;
fState = 0;
bonus = 0;
score = 0;
}
}