Skip to content

Commit 15666a3

Browse files
committed
More screen examples
1 parent fb2a612 commit 15666a3

2 files changed

Lines changed: 374 additions & 0 deletions

File tree

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/*
2+
8x8 LED matrix pong
3+
for Arduino Mega
4+
5+
This example controls two 8x8 LED matrices in a game of pong.
6+
It takes input from two analog inputs to set the
7+
paddles, and moves the ball accordingly.
8+
9+
created 1 Apr 2009
10+
updated 31 Oct 2020
11+
by Tom Igoe
12+
13+
The matrices used are labeled HS-788AS. They were bought surplus.
14+
They are laid out as follows
15+
(warning: yours might have different pin configurations):
16+
17+
rows are the anodes
18+
cols are the cathodes
19+
20+
label HS-788AS is on the left side of the chip
21+
_________
22+
col 4 ----| |---- row 1
23+
col 2 ----| |---- row 2
24+
row 7 ----| |---- col 7
25+
row 6 ----| |---- row 8
26+
col 1 ----| |---- col 5
27+
row 4 ----| |---- row 3
28+
col 3 ----| |---- row 5
29+
col 6 ----| |---- col 8
30+
---------
31+
32+
Pin numbers:
33+
Matrix 1:
34+
2,3,4,5,6,7,8,9,12,11,12,13, 34, 35, 36, 37
35+
36+
Matrix 2:
37+
22,23,24,25,26,27,28,29,30,31,38,39,41,43,45,47,49,51,53
38+
39+
*/
40+
41+
// define the edges of the screen:
42+
const int LEFT = 0;
43+
const int RIGHT = 15;
44+
const int TOP = 0;
45+
const int BOTTOM = 7;
46+
47+
int pixels[16][8]; // 2-dimensional array of pixels
48+
49+
// 2-dimensional array of row pin numbers (for two matrices):
50+
int row[2][8] = {
51+
{
52+
6, 11, 10, 3, 35, 4, 8, 9
53+
}
54+
,
55+
{
56+
25, 43, 45, 28, 49, 27, 23, 22
57+
}
58+
};
59+
60+
// 2-dimensional array of column pin numbers (for two matrices):
61+
int col[2][8] = {
62+
{
63+
2, 7, 37, 5, 13, 36, 12, 34
64+
}
65+
,
66+
{
67+
29, 24, 53, 26, 39, 51, 41, 47
68+
}
69+
};
70+
71+
int ballX = 8; // X position of the ball
72+
int ballY = 4; // Y position of the ball
73+
int ballDirectionY = 1; // X direction of the ball
74+
int ballDirectionX = 1; // Y direction of the ball
75+
76+
int rightPaddleY = 0; // X position of the center of the right paddle
77+
int leftPaddleY = 0; // Y position of the center of the right paddle
78+
79+
long timeStamp = 0; // time stamp to control the pauses between ball moves
80+
long interval = 120; // interval between ball moves, in milliseconds
81+
boolean gamePaused = false; // state of the game
82+
83+
void setup() {
84+
// initialize the I/O pins as outputs:
85+
86+
// iterate over the two matrices:
87+
for (int thisMatrix = 0; thisMatrix < 2; thisMatrix++) {
88+
// iterate over the pins:
89+
for (int thisPin = 0; thisPin < 8; thisPin++) {
90+
// initialize the output pins:
91+
pinMode(col[thisMatrix][thisPin], OUTPUT);
92+
pinMode(row[thisMatrix][thisPin], OUTPUT);
93+
// take the col pins (i.e. the cathodes) high to ensure that
94+
// the LEDS are off:
95+
digitalWrite(col[thisMatrix][thisPin], HIGH);
96+
}
97+
}
98+
99+
// initialize the pixel matrix:
100+
for (int x = 0; x < 16; x++) {
101+
for (int y = 0; y < 8; y++) {
102+
pixels[x][y] = HIGH;
103+
}
104+
}
105+
}
106+
107+
void loop() {
108+
// read input:
109+
readSensors();
110+
// move the ball:
111+
if (gamePaused) {
112+
if (millis() - timeStamp > interval * 10) {
113+
// if enough time has passed, start the game again:
114+
gamePaused = false;
115+
}
116+
}
117+
// if the game isn't paused, and enough time between ball moves
118+
// has passed, move the ball and update the timestamp:
119+
else {
120+
if (millis() - timeStamp > interval) {
121+
moveBall();
122+
timeStamp = millis();
123+
}
124+
}
125+
// draw the screen:
126+
refreshScreen();
127+
}
128+
129+
void readSensors() {
130+
// set the left paddle to off:
131+
setPaddle(LEFT, leftPaddleY, HIGH);
132+
// set the right paddle to off:
133+
setPaddle(RIGHT, rightPaddleY, HIGH);
134+
135+
// read the sensors for X and Y values:
136+
leftPaddleY = map(analogRead(0), 0, 1023, 0, 7);
137+
rightPaddleY = map(analogRead(1), 0, 1023, 0, 7);
138+
139+
// set the left paddle to on:
140+
setPaddle(LEFT, leftPaddleY, LOW);
141+
// set the right paddle to on:
142+
setPaddle(RIGHT, rightPaddleY, LOW);
143+
}
144+
145+
void setPaddle(int paddleX, int paddleY, int state) {
146+
// set the last right paddle to on:
147+
pixels[paddleX][paddleY] = state;
148+
// set the bottom pixel of the paddle:
149+
if (paddleY < BOTTOM) {
150+
pixels[paddleX][paddleY + 1] = state;
151+
}
152+
153+
// set the top pixel of the paddle:
154+
if (paddleY > TOP) {
155+
pixels[paddleX][paddleY - 1] = state;
156+
}
157+
}
158+
159+
void moveBall() {
160+
// check to see if the ball is in the horizontal range
161+
// of the paddles:
162+
163+
// right:
164+
if (ballX >= RIGHT - 1) {
165+
// if the ball's next Y position is between
166+
// the top and bottom of the paddle, reverse its X direction:
167+
if ((ballY + ballDirectionY >= rightPaddleY - 1)
168+
&& (ballY + ballDirectionY <= rightPaddleY + 1)) {
169+
// reverse the ball horizontal direction:
170+
ballDirectionX = -ballDirectionX;
171+
}
172+
}
173+
174+
// left:
175+
if (ballX <= LEFT + 1) {
176+
// if the ball's next Y position is between
177+
// the top and bottom of the paddle, reverse its X direction:
178+
if ((ballY + ballDirectionY >= leftPaddleY - 1 )
179+
&& (ballY + ballDirectionY <= leftPaddleY + 1 )) {
180+
// reverse the ball horizontal direction:
181+
ballDirectionX = -ballDirectionX;
182+
}
183+
}
184+
185+
// if the ball goes off the screen bottom,
186+
// reverse its Y direction:
187+
if (ballY == BOTTOM) {
188+
ballDirectionY = -ballDirectionY;
189+
}
190+
// if the ball goes off the screen top,
191+
// reverse its X direction:
192+
if (ballY == TOP) {
193+
ballDirectionY = -ballDirectionY;
194+
}
195+
196+
// clear the ball's previous position:
197+
pixels[ballX][ballY] = HIGH;
198+
199+
// if the ball goes off the screen left or right:
200+
if ((ballX == LEFT) || (ballX == RIGHT)) {
201+
// reset the ball:
202+
ballX = 8;
203+
ballY = 4;
204+
// pause and note the time you paused:
205+
gamePaused = true;
206+
timeStamp = millis();
207+
}
208+
// increment the ball's position in both directions:
209+
ballX = ballX + ballDirectionX;
210+
ballY = ballY + ballDirectionY;
211+
212+
// if the game isn't paused, set the ball
213+
// in its new position:
214+
if (!gamePaused) {
215+
// set the new position:
216+
pixels[ballX][ballY] = LOW;
217+
}
218+
}
219+
220+
void refreshScreen() {
221+
// iterate over the matrices:
222+
for (int thisMatrix = 0; thisMatrix < 2; thisMatrix++) {
223+
// iterate over the rows (anodes):
224+
for (int thisRow = 0; thisRow < 8; thisRow++) {
225+
// take the row pin (anode) high:
226+
digitalWrite(row[thisMatrix][thisRow], HIGH);
227+
// iterate over the cols (cathodes):
228+
for (int thisCol = 0; thisCol < 8; thisCol++) {
229+
// get the state of the current pixel;
230+
int thisPixel = pixels[thisRow + (thisMatrix * 8)][thisCol];
231+
// when the row is HIGH and the col is LOW,
232+
// the LED where they meet turns on:
233+
digitalWrite(col[thisMatrix][thisCol], thisPixel);
234+
// turn the pixel off:
235+
digitalWrite(col[thisMatrix][thisCol], HIGH);
236+
}
237+
// take the row pin low to turn off the whole row:
238+
digitalWrite(row[thisMatrix][thisRow], LOW);
239+
}
240+
}
241+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Super Duper Knight Rider
3+
for Arduino Mega
4+
5+
This example controls two 8x8 LED matrices, lighting one LED at a time,
6+
then bouncing back to the beginning.
7+
8+
created 29 Mar 2009
9+
by Tom Igoe
10+
11+
12+
The matrices used are labeled HS-788AS. They were bought surplus.
13+
They are laid out as follows
14+
(warning: yours might have different pin configurations):
15+
16+
rows are the anodes
17+
cols are the cathodes
18+
19+
label HS-788AS is on the left side of the chip
20+
_________
21+
col 4 ----| |---- row 1
22+
col 2 ----| |---- row 2
23+
row 7 ----| |---- col 7
24+
row 6 ----| |---- row 8
25+
col 1 ----| |---- col 5
26+
row 4 ----| |---- row 3
27+
col 3 ----| |---- row 5
28+
col 6 ----| |---- col 8
29+
---------
30+
31+
Pin numbers:
32+
Matrix 1:
33+
2,3,4,5,6,7,8,9,12,11,12,13, 34, 35, 36, 37
34+
35+
Matrix 2:
36+
22,23,24,25,26,27,28,29,30,31,38,39,41,43,45,47,49,51,53
37+
38+
*/
39+
int row[] = {
40+
22,23,27,49,28,45,43,25};
41+
42+
int col[] = {
43+
47,41,51,39,26,53,24,29};
44+
45+
46+
int row2[] = {
47+
9,8,4,35,3,10,11,6};
48+
49+
int col2[] = {
50+
34,12,36,13,5,37,7,2};
51+
52+
53+
54+
void setup() {
55+
for (int thisPin = 0; thisPin < 8; thisPin++) {
56+
// initialize the output pins for matrix 1:
57+
pinMode(col[thisPin], OUTPUT);
58+
pinMode(row[thisPin], OUTPUT);
59+
// take the col pins (i.e. the cathodes) high to ensure that
60+
// the LEDS are off:
61+
digitalWrite(col[thisPin], HIGH);
62+
63+
// initialize the output pins for matrix 2:
64+
pinMode(col2[thisPin], OUTPUT);
65+
pinMode(row2[thisPin], OUTPUT);
66+
67+
// take the col pins (i.e. the cathodes) high to ensure that
68+
// the LEDS are off:
69+
digitalWrite(col2[thisPin], HIGH);
70+
}
71+
}
72+
73+
void loop() {
74+
// light up the first matrix:
75+
// iterate over the rows (anodes):
76+
for (int thisrow = 0; thisrow < 8; thisrow++) {
77+
// take the row pin (anode) high:
78+
digitalWrite(row[thisrow], HIGH);
79+
// iterate over the cols (cathodes):
80+
for (int thiscol = 0; thiscol < 8; thiscol++) {
81+
// when the row is high and the col is low,
82+
// the LED where they meet turns on:
83+
digitalWrite(col[thiscol], LOW);
84+
delay(50);
85+
// take the col pin (cathode) high to turn the LED off:
86+
digitalWrite(col[thiscol], HIGH);
87+
}
88+
// take the row pin low to turn off the whole row:
89+
digitalWrite(row[thisrow], LOW);
90+
}
91+
92+
// light up the second matrix:
93+
94+
// iterate over the rows (anodes):
95+
for (int thisrow = 0; thisrow < 8; thisrow++) {
96+
// take the row pin (anode) high:
97+
digitalWrite(row2[thisrow], HIGH);
98+
// iterate over the cols (cathodes):
99+
for (int thiscol = 0; thiscol < 8; thiscol++) {
100+
// when the row is high and the col is low,
101+
// the LED where they meet turns on:
102+
digitalWrite(col2[thiscol], LOW);
103+
delay(50);
104+
// take the col pin (cathode) high to turn the LED off:
105+
digitalWrite(col2[thiscol], HIGH);
106+
}
107+
// take the row pin low to turn off the whole row:
108+
digitalWrite(row2[thisrow], LOW);
109+
}
110+
111+
// do the same to go backwards, counting backwards:
112+
113+
// second matrix in reverse:
114+
for (int thisrow = 7; thisrow > 0; thisrow--) {
115+
digitalWrite(row2[thisrow], HIGH);
116+
for (int thiscol = 7; thiscol >0; thiscol--) {
117+
digitalWrite(col2[thiscol], LOW);
118+
delay(50);
119+
digitalWrite(col2[thiscol], HIGH);
120+
}
121+
digitalWrite(row2[thisrow], LOW);
122+
}
123+
// first matrix in reverse:
124+
for (int thisrow = 7; thisrow > 0; thisrow--) {
125+
digitalWrite(row[thisrow], HIGH);
126+
for (int thiscol = 7; thiscol >0; thiscol--) {
127+
digitalWrite(col[thiscol], LOW);
128+
delay(50);
129+
digitalWrite(col[thiscol], HIGH);
130+
}
131+
digitalWrite(row[thisrow], LOW);
132+
}
133+
}

0 commit comments

Comments
 (0)