Ping Pong with 8×8 Led Display on Arduino

ping-pong Found here:

http://www.instructables.com/id/Pong-with-8×8-Led-Matrix-on-Arduino/

The origal site, as found in the above link, works again! (doesn’t work anymore??)

ping-pong

The biggest problem was to find out how to connect the 8×8 led display to arduino.
Also, the code we found, was not telling me much, there was/is no documentation in it … shame!

First to know is that analog ports can be used as digital inputs-ports, they go by the name: 14 for A0, 15 for A1, … and 19 for A5.

pin-layout-arduino

So this is how you should connect the arduino-digital-outputs to the 8×8 display-matrix:

// pins of the 8x8-display layout
// 8x8-display 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// arduino-pins 5 4 3 2 A0 A1 A2 A3 13 12 11 10 9 8 7 6
byte pins[16] = {5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10, 9, 8, 7, 6};

8x8-c-anode

Do not forget to put extra resistors to limit the current, as marked in the datasheet of the display you use.
You should put the resistors in serie with:

  • or the rows; display-pins 9, 14, 8, 12, 1, 7, 2, 5
  • or the columns; display-pins 13, 3, 4, 10, 6, 11, 15 and 16
    ping-pong-weerstanden

I also tried to document the code a bit, but ended not far …

This is the code we found on the net:
http://forum.arduino.cc/index.php?topic=110767.0
Or the original site:
http://blog.bsoares.com.br/arduino/ping-pong-with-8×8-led-matrix-on-arduino

One remark:
I had to change the digitalWrite-lines from HIGH to LOW and reverse, because I used a common anode-8×8-display (original HIGH where there is a ‘//1’ at the end).


/**
* Ping Pong with 8x8 Led Dot Matrix on Arduino
*
* @author Bruno Soares
* @website http://www.bsoares.com.br
*/

//
// y 7 6 5 4 3 2 1 0
// 17), 0-13 + A0,A1,A2,A3
for (int i = 2; i = 8) {
screenCol = 0;
screenRow++;
if (screenRow >= 8) {
screenRow = 0;
}
}

if ( (screen[screenRow] >> screenCol) & B1 == B1 ) {
digitalWrite(translatePin(rows[screenRow]), !COMMON_CATHODE);//1
digitalWrite(translatePin(cols[screenCol]), COMMON_CATHODE);
} else {
digitalWrite(translatePin(rows[screenRow]), COMMON_CATHODE);
digitalWrite(translatePin(cols[screenCol]), !COMMON_CATHODE);//1
}
} // end doubleBuffer()

byte translatePin(byte original) {
return pins[original - 1];
} // end translatePin()

// put everything to zero
void allOFF() {
for (int i = 0; i < 8; i++) {
//screen[i] = 0;
screen[i] = B11111111;
}
}

//
void on(byte row, byte column) {
screen[column-1] |= (B1 << (row - 1)); // |= compound bitwise or
// << bitshift left
// screen[column-1] = screen[column-1] | (B1 << (row - 1));
}

void off(byte row, byte column) {
screen[column-1] &= ~(B1 << (row - 1)); // &= compound bitwise and, ~ bitwise not
}

// calulate the wall's left and right depending on the values of the pot-meters
void calcWall()
{
left = analogRead(PIN_LEFT); // reading value pot-meter between 0 and 1024
right = analogRead(PIN_RIGHT);
// everything above and under 223 is ignored and remapped to 0-6
left = constrain(map(left, 223, 800, 0, 6), 0, 6); // Constrains a number to be within a range.
right = constrain(map(right, 223, 800, 6, 0), 0, 6);

clearWall();

on(1, left + 1); // put 2-led wall left and right
on(1, left + 2); // on row 1 and row 8
//on(1, left + 3);
on(8, right + 1);
on(8, right + 2);
//on(8, right + 3);

_wall[0] = left;
_wall[1] = right;
show();
}

void clearWall()
{
for (int i = 0; i < 8; i++)
screen[i] &= B01111110; // put this bit in screen
// the 2 1 and 8 leds are defined by the wall
}

void clearGame()
{
for (int i = 0; i < 8; i++)
screen[i] &= B10000001;
}

void loop() {
calcWall();
enterFrameHandler();
delay(50);
}

void enterFrameHandler()
{
if (_count++ 1)
_speed--;
}

//
void resetAnim()
{
for (int i = 0; i < 8; i++)
{
screen[i] = B11111111;
delay(25);
}
for (int i = 0; i < 8; i++)
{
screen[i] = B00000000;
delay(5);
}
}

// put a face on the lcd :-)
void face()
{
on(1, 1);
on(1, 2);
on(2, 1);
on(2, 2);
on(7, 1);
on(7, 2);
on(8, 1);
on(8, 2);
on(1, 1);
on(1, 2);
on(4, 4);
on(4, 5);
on(5, 4);
on(5, 5);
on(2, 7);
on(7, 7);
on(3, 8);
on(4, 8);
on(5, 8);
on(6, 8);
delay(3000);
}

void reset()
{
resetAnim();
resetAnim();

// put begin-dot random between 3 and 5 in x and y position
_px = random(3, 5);
_py = random(3, 5);
_angle = random(0, 2) == 0 ? 0 : 180;
_speed = 5;
_countPoints = 0;

show();
delay(500);
}

void show()
{
clearGame();
on(_px + 1, _py + 1);
}

void checkCollision()
{
if (_px == _w - 1)
{
if (_angle == 315 || _angle == 0 || _angle == 45)
{
if (_py == _wall[1] || _py == _wall[1] + 1 )
{
if (_angle == 0 && _py == _wall[1])
retorted(225);
else if (_angle == 0 && _py == _wall[1] + 1)
retorted(135);
else if (_angle == 45 && _py == _wall[1])
retorted(135);
else if (_angle == 45 && _py == _wall[1] + 1)
retorted(180);
else if (_angle == 315 && _py == _wall[1])
retorted(180);
else if (_angle == 315 && _py == _wall[1] + 1)
retorted(225);
}
}
}
else if (_px == 1)
{
if (_angle == 225 || _angle == 180 || _angle == 135)
{
if (_py == _wall[0] || _py == _wall[0] + 1)
{
if (_angle == 180 && _py == _wall[0])
retorted(315);
else if (_angle == 180 && _py == _wall[0] + 1)
retorted(45);
else if (_angle == 135 && _py == _wall[0])
retorted(45);
else if (_angle == 135 && _py == _wall[0] + 1)
retorted(0);
else if (_angle == 225 && _py == _wall[0])
retorted(0);
else if (_angle == 225 && _py == _wall[0] + 1)
retorted(315);
}
}
}

if (_px == _w)
{
reset();
}
else if (_px == 0)
{
reset();
}
else if (_py == _h)
{
if (_angle == 45)
_angle = 315;
else if (_angle == 135)
_angle = 225;
}
else if (_py == 0)
{
if (_angle == 225)
_angle = 135;
else if (_angle == 315)
_angle = 45;
}
}

void calcAngleIncrement()
{
if (_angle == 0 || _angle == 360)
{
_px += 1;
}
else if (_angle == 45)
{
_px += 1;
_py += 1;
}
else if (_angle == 135)
{
_px -= 1;
_py += 1;
}
else if (_angle == 180)
{
_px -= 1;
}
else if (_angle == 225)
{
_px -= 1;
_py -= 1;
}
else if (_angle == 315)
{
_px += 1;
_py -= 1;
}
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s