-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinballMachineCode.ino
299 lines (203 loc) · 6.97 KB
/
PinballMachineCode.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Pinball Machine- Enggineering II , Wil Permenter
* Created 4/18/2018 End xx/xx/xxxx
* Description: This Code is a pinball machine code and has the
* ability to add score and controll lights. Built for Arduino UNO
*/
//used to better control 7 Segment LED's
#include "SevenSeg.h"
SevenSeg sevenseg(true); //True For debugging
//used to have led light shows stored else where
#include "ledShow.h"
ledShow ledshow(true); //True for debugging
int waitTime = 1; // sets refreshRate, Lowest = 1
//Pin Var Setup
int scorePin = A0; //Used for point/hit detection
int spinnyPin = A1; // Hit Detection for spinny thing
//Connect these pins to the coraspoinding spots on display
/*
1 = 2
2 = 3
3 = 4
4 = 5
*/
int A = 6;
int B = 7;
int C = 8;
int D = 9;
int E = 10;
int F = 11;
int G = 12;
//Score Var Setup
int pointAddTen = 600; //Analog Input for +10
int pointAddTwent = 650;//Analog Input for + 20
int pointAdd = 0; // Storage Var
int pointSpinnyLast = 0; // Will store the var and will be used to compare to the last
// Display Var Most Right will be static 0
int scoreOnePlace = 0; // Second From Right 1's place
int scoreTenPlace = 0;// Third From Right 10's place
int scoreHundPlace = 0; // Most Left One 100's Place
int scoreThousPlace = 0; // Most Left One 1000's Place
bool scoreChange = true; //If score needs to be updated will be true
int scoreDisplay = 2; //Tells what segment to turn on based off Pin Numbe(look Above);
int scoreNumber = 0; //the number to be displayed
int latch = 0; //latching for preventing over count on score
int latch2 = 0;//
int bonusBool = 0;
int bonusRand = 0;
unsigned long bonusTime = 0;
unsigned long bonusTime1= 0;
unsigned long time1 = 0;
int holder = 0;
void setup() {
//setup
sevenseg.begin(9200); //starts serial dont ask why this is done 2x
ledshow.begin(9200); // starts serial
//Pins For Seven Seg
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT); // 2-5 are for ABCD see pin layout chart.
pinMode(4,OUTPUT);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
pinMode(1,OUTPUT); // drop target leds
//Pins For LED Control
//Other Stuff
// Sets All Segments to off on Start
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
//run start up animation
ledshow.startUp(); //does start up animation
}
void loop() {
//bonus
bonusTime = millis();
if(bonusTime <= 300){ //generate number for bonus
bonusRand = random(0,9);
Serial.println(bonusRand);
}
if(bonusRand == scoreTenPlace){ //random number
bonusBool = 1;
}
if(bonusBool == 1){
ledshow.bonus(time1);
Serial.println("bonus!");
}else{
digitalWrite(1,HIGH);
}
if(bonusTime >1000){ // rest after set time
bonusBool = 0;
bonusTime1 = bonusTime;
bonusTime = 0;
}
// Score Detection
pointAdd = analogRead(scorePin); //reads For point
if(pointAdd > pointAddTen && pointAdd < pointAddTwent && latch == 0){ //Will Run if was +10
if(bonusBool == 1){
scoreTenPlace +=2; // Adds 2x points if bonus Time
}else{
scoreTenPlace += 1; //Adds Points to Score
}
scoreChange = true; //Tells us to update Display
latch =1; //sets latch to 1 bc a hit was detecteed
}else if(pointAdd > pointAddTwent && latch == 0){ //Will run if +20
if(bonusBool == 1){
scoreTenPlace +=4; // Adds 2x points if bonus Time
}else{
scoreTenPlace += 2; //Adds Points to Score
}
scoreChange = true;
latch = 1; // sets latch to 1 bc hit was detected
}else{
scoreChange = false; // bool to skip score if no change needed ( this makes the leds not flicker as much);
int spinnyNum = analogRead(spinnyPin);
spinnyNum -= 1006;
if(spinnyNum > 10 && latch2 == 1){
scoreChange = true;
if(bonusBool == 1){
scoreTenPlace +=1; // Adds 2x points if bonus Time
}else{
scoreOnePlace += 5; //Adds ~5 points per spin might be off due to delays in our sevenseg.cpp
}
latch2 = 0;
}
if(spinnyNum <10){ // This is for the makeshift encoder.
latch2= 1;
}
if (pointAdd < 400){ // this de latches the scoreing stuff
latch = 0;
}
}
//ScoreMaths
if(scoreChange){ // skips steps if change not needed, scoreChange is a bool so it will be true of false
/*
* I know this part is basiclly reinventing the wheel but i feel this gives more control over scores. Its basicly just
* going in and looking at the specific place value of our score.
*/
if(scoreOnePlace >= 10){ //if ones place is 10 then will carry over to tens place scoreTwo
scoreOnePlace -= 10;
scoreTenPlace += 1;
}
if(scoreTenPlace >= 10){ //if tens place is 10 then will carry over to hundreds place scoreTwo
scoreTenPlace -= 10;
scoreHundPlace += 1;
}
if(scoreHundPlace >= 10){ //if tens place is 10 then will carry over to hundreds place scoreTwo
scoreHundPlace -= 10;
scoreThousPlace += 1;
}
Serial.print(scoreThousPlace); // Prints Score Note: do this inside the loop so it only sends it if it needs to so its not spammed :)
Serial.print(scoreHundPlace);
Serial.print(scoreTenPlace);
Serial.println(scoreOnePlace);
}
//Display Score
/*
* This will be using custom Library that is a work in progress.
* It is written for a 4x 7 segment display part no. HS420561K-32
*/
displayLoop: // for goto
if(scoreDisplay == 2){
scoreNumber = scoreThousPlace;
}else if(scoreDisplay == 3){
scoreNumber = scoreHundPlace;
}else if(scoreDisplay == 4){
scoreNumber = scoreTenPlace;
}else if(scoreDisplay == 5){ //set as else if to try to fix problem did not work so just goint to leave it cause why not :I
scoreNumber = scoreOnePlace;
}
if(scoreNumber == 0){ // will print 1 segment at a time and its coraspoindg number. Refer to SevenSeg.cpp for the code.
sevenseg.zero(scoreDisplay);
}else if (scoreNumber == 1){
sevenseg.one(scoreDisplay);
}else if (scoreNumber == 2){
sevenseg.two(scoreDisplay);
}else if( scoreNumber == 3){
sevenseg.three(scoreDisplay);
}else if (scoreNumber == 4){
sevenseg.four(scoreDisplay);
}else if (scoreNumber == 5){
sevenseg.five(scoreDisplay);
}else if (scoreNumber == 6){
sevenseg.six(scoreDisplay);
}else if (scoreNumber == 7){
sevenseg.seven(scoreDisplay);
}else if (scoreNumber == 8){
sevenseg.eight(scoreDisplay);
}else if (scoreNumber == 9){ //leaveing else if for debugging cause why not
sevenseg.nine(scoreDisplay);
}
scoreDisplay += 1; // counts what 7 seg we are displaying
if( scoreDisplay <= 5){ // makes sure we have turned all of them on once
goto displayLoop;
}
scoreDisplay = 2; // resets scoreDisplay and will go back to top
// else will go to top
}