-
Notifications
You must be signed in to change notification settings - Fork 0
/
PingPong.ino
340 lines (274 loc) · 7.21 KB
/
PingPong.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* clock pins and latch pins vertical and horizotal shift registers used for dotmatrix control*/
#define clockpinV 2
#define clockpinH 3
#define latchpinH 4
#define datapinH 6
#define datapinV 7
#define latchpinV 8
uint8_t i;
int t;
int which = 0;
/* Position details of wedge and ball */
byte position;
byte ballposition;
byte oldpositiontop;
byte newpositiontop;
byte oldpositionbot;
byte newpositionbot;
uint8_t ball;
uint8_t wedge;
int speed=60; // controls the speed of ball
int intial=100;
/* clears the display of dot-matrix */
void cleardisplay()
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00000000);
digitalWrite(latchpinH,HIGH);
}
/* Reads data from analog POT's and measures position of wedges and return the leds data for the rows containing wedges */
byte analogit()
{
int getin = analogRead( which );
digitalWrite(latchpinV,LOW);
if( which == 0)
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<0));
else
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<7));
digitalWrite(latchpinV,HIGH);
if(which==1)
which=0;
else
which=1;
if(getin < 172)
return 0b11100000;
if(getin < 342)
return 0b01110000;
if(getin < 512)
return 0b00111000;
if(getin < 682)
return 0b00011100;
if(getin < 852)
return 0b00001110;
if(getin <= 1024)
return 0b00000111;
}
/* ending display message i have done it in most dumb way possible because i wrote it way back when i was a shitty coder :P */
void end()
{
for(i=1;i<7;i++)
{
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<i));
digitalWrite(latchpinV,HIGH);
if(i==1)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00000000);
digitalWrite(latchpinH,HIGH);
}
if(i==2)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00100100);
digitalWrite(latchpinH,HIGH);
}
if(i==3)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00000000);
digitalWrite(latchpinH,HIGH);
}
if(i==4)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00011000);
digitalWrite(latchpinH,HIGH);
}
if(i==5)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b01000010);
digitalWrite(latchpinH,HIGH);
}
if(i==6)
{
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00111100);
digitalWrite(latchpinH,HIGH);
}
delay(2);
}
}
/* intial setup for the game */
void setup()
{
pinMode(clockpinH,OUTPUT);
pinMode(clockpinV,OUTPUT);
pinMode(latchpinH,OUTPUT);
pinMode(datapinH,OUTPUT);
pinMode(latchpinV,OUTPUT);
pinMode(datapinV,OUTPUT);
digitalWrite(latchpinH,HIGH);
digitalWrite(latchpinV,HIGH);
for(i=3;i<7;i++)
{
for( t=0 ; t<intial; t++)
{
cleardisplay();
digitalWrite(latchpinH,LOW);
position = analogit(); //get position of the wedge
if((i==6) && (which == 0))
oldpositionbot = position;
shiftOut(datapinH,clockpinH,LSBFIRST,position);
digitalWrite(latchpinH,HIGH);
delay(2);
cleardisplay();
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<i));
digitalWrite(latchpinV,HIGH);
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b00010000);
digitalWrite(latchpinH,HIGH);
delay(2);
}
}
ballposition=0b00010000;
ball=2;
}
/*This goes in a loop */
void loop()
{
for(i=6;i>0;i--)
{
for( t=0 ; t<speed; t++)
{
cleardisplay();
digitalWrite(latchpinH,LOW);
position = analogit();
if((i==6) && (which == 0))
newpositionbot=position;
if((i==1) && (which == 1))
oldpositiontop=position;
shiftOut(datapinH,clockpinH,LSBFIRST,position); // shifts the data of dotmatrix row on shift register
digitalWrite(latchpinH,HIGH);
delay(2);//keep for 2ms
cleardisplay(); // clear the display
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<i));
digitalWrite(latchpinV,HIGH);
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,ballposition);
digitalWrite(latchpinH,HIGH);
delay(2);
}
if(i == 6)
{
if(!(oldpositionbot & ballposition))
{
while(1)
{
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<0));
digitalWrite(latchpinV,HIGH);
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b11111111);
digitalWrite(latchpinH,HIGH);
delay(2);
end();
}
}
if(oldpositionbot > newpositionbot)
wedge = 3 ;
else if(oldpositionbot < newpositionbot)
wedge = 1 ;
else
wedge = 2 ;
{
if(wedge==2)
ball=ball;
else if(ball==2)
ball=wedge;
else
ball=2;
}
}
if((ballposition == 0b10000000 )&& (ball==1))
ball=3;
else if((ballposition == 0b00000001) && (ball==3))
ball=1;
if(i!=1)
{
if(ball==1)
ballposition=(ballposition<<1);
if(ball==3)
ballposition=(ballposition>>1);
}
}
for(i=1;i<7;i++)
{
for( t=0 ; t<speed; t++)
{
cleardisplay();
digitalWrite(latchpinH,LOW);
position = analogit();
if((i==1)&& (which == 1))
newpositiontop=position;
if((i==6) && (which == 0))
oldpositionbot=position;
shiftOut(datapinH,clockpinH,LSBFIRST,position);
digitalWrite(latchpinH,HIGH);
delay(2);
cleardisplay();
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<i));
digitalWrite(latchpinV,HIGH);
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,ballposition);
digitalWrite(latchpinH,HIGH);
delay(2);
}
if(i == 1)
{
if(!(oldpositiontop & ballposition))
{
while(1)
{
digitalWrite(latchpinV,LOW);
shiftOut(datapinV,clockpinV,MSBFIRST,~(0b00000001<<7));
digitalWrite(latchpinV,HIGH);
digitalWrite(latchpinH,LOW);
shiftOut(datapinH,clockpinH,LSBFIRST,0b11111111);
digitalWrite(latchpinH,HIGH);
delay(2);
end();
}
}
if(oldpositiontop > newpositiontop)
wedge = 3 ;
else if(oldpositiontop < newpositiontop)
wedge = 1 ;
else
wedge = 2 ;
{
if(wedge==2)
ball=ball;
else if(ball==2)
ball=wedge;
else
ball=2;
}
}
if((ballposition == 0b10000000) && (ball==1))
ball=3;
else if((ballposition == 0b00000001) && (ball==3))
ball=1;
if(i!=6)
{
if(ball==1)
ballposition=(ballposition<<1);
if(ball==3)
ballposition=(ballposition>>1);
}
}
}