|
| 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 | +} |
0 commit comments