— weekly — 1 min read
This week I wanted to take a step forward towards displaying my PONG game on an LED matrix. To do so, I found at the lab an LED 32x32 RGB matrix. This looked like the perfect device to display my game:
The downside of finding random devices in the lab is that you never know where they come from or in
what condition they are. I found online the datasheet
which is far from being any detailed, so I had to spend some time thinking how to make the device work. I knew that it needs
5V
power, DIN
and GND
. I tried to strip some bare wires and solder them directly on the matrix:
At the beginning I tried to power and ground it through my last week's breakout board but that showed no signal, so I connected the wires to an external 5V supply. I used the NeoPixel default example code to make sure my code is not the problem. Unfortunately, after many tries I had to figure out that either I am not connecting the device in a proper way or it is just defected. So I had to swap my output device with something I knew was working. I decided to program my PONG game on the OLED SSD 1306 display. I used Lingdong's awesome tutorial to bootstrap my display (code is attached below).
Now I coded a simply ball movement:
1// ball config2int ball_x0;3int ball_y0;4int ball_dx = 1;5int ball_dy = 1;6int ball_x;7int ball_y;8int ball_speed = 50;
1// draw ball2ball_x = ball_x0 + ball_dx;3ball_y = ball_y0 + ball_dy;4OLEDpixel(ball_x, ball_y);5OLEDunpixel(ball_x0, ball_y0);6
7// ball boundaries8if (ball_y >= 63) {9 ball_dy = -1;10}11if (ball_y <= 1) {12 ball_dy = 1;13}14if (ball_x >= 127) {15 ball_dx = -1;16}17if (ball_x <= 2) {18 ball_dx = 1;19}20
21// update ball iteration22ball_x0 = ball_x;23ball_y0 = ball_y;24
25// display26OLEDflush();27delay(ball_speed);
For some reason, the display flickered at the bottom part. At the beginning I though it has something to do with the way I convert integers to pixel locations on the OLED buffer. After debugging for a while I could not find any code mistakes so I assumed that my display was defected. I tried another OLED display with the same code:
And I was right! I used the rest of my time to program the players controllers and movement. In the future, those should be controlled via the distance ToF sensor I experimented with last week.
1// players config2#define CONTROLLER_W 43int winner = 0;4int p_y0 = 32;5int p1_y= p_y0;6int p2_y= p_y0;7int d_p1 = 1;8int d_p2 = -1;
1void drawPlayerController(int player_loc, bool isPlayerOne) {2 int x_offset = 2;3 if (!isPlayerOne) {4 x_offset += 125;5 }6
7 for (int j = 0; j < 64; ++j) {8 OLEDunpixel(x_offset, j);9 }10 11 for (int i = 0; i < (2 * CONTROLLER_W + 1); ++i) {12 int loc = player_loc - CONTROLLER_W + i;13 OLEDpixel(x_offset, loc);14 }15 16}