Implementing 2D rendering in a 3X3 LED display.

Hey Arduino forum! I'm now doing a project that consists of a 9LED 3X3 "LCD screen" and a couple of buttons. My goal is to try and build a game similar to tag, were there would be two dots and one dot would follow a dot controlled by you. The point of the game is to stay as long as possible without being caught by the dot following you.

In the beginning I thought I had all the abilities to create such a project, but while working on it, I ran into a problem. I began to think on the fact of how am I'm going to make does dots move as thought they are virtually on a 2D surface?

For example: If I would have a statement that would say that if a dot is in a particular position and thats true, make it go y+1 and x-1 (upper-left direction). You see? Instead os saying that if the dot is in a particular position and digitalWrite on a LED if that Statement is true, It would be cool saying move that dot on the X or Y axis.

This is similar to computer programming. For example when a statement in a computer game is true it makes a shape move some direction, which is defined by the X and Y coordinates. (and Z axis in 3D games)

How can I achieve this in controlling a simple 3X3 LED "Display"? (Hopefully if I grasp this concept i'll move to 4X4 or even bigger displays)

Thank you guys!

First off, what does this indicate?

"Expert level: ??????????"

Just curious.

I would do this in two pieces.
Make a section of code that reads columns of data from an array and outputs them to the LEDs in a multiplexed fashion every 5mS or so.
This will be easy expand to a larger array later (or send to a MAX7219, or 2x2 grid of MAX7219s, etc).
Here's a 5x5 example. Drive the anode from array data, sink current in one cathode. When 5mS have gone by, turn the cathode off, drive the anodes with next set of data, turn on next cathode.

During the 5mS wait, you can be doing the next part of the code.
The 2nd part reads the input switches/buttons, or a joystick (really just two potentiometers), whatever the user input is, and maps those inputs into an array position movememt, and moves the "computer" player into position. Somehow you decide when the player is out of room.

CrossRoads:
First off, what does this indicate?

"Expert level: ??????????"

My self criticism of my mastered skill with Arduino :))

Thanks for the answer by the way ))

mixania:
This is similar to computer programming.

No, it IS computer programming.

You've reached the point where you need to learn to program, not just hack around with code.

Start by making an array:

char board[3][3];

This is your 'board'. Think of it like a chessboard with squares. In there you can put letters to represent things in the game, eg 'p' for 'player'. This is what you manipulate with your program. When you move a monster you look in the screen to see if there's a 'p' where the monster is moving too. When you get up to a 16x16 board you can start putting in 'w' for a wall, etc.

Next you need a function to copy/translate the board to the LED display. You call this function on every pass through 'loop()'. For single color LEDs you can have different characters come out with different brightnesses. If you change to RGB LEDs you can have 'p', 'w' and 'm' come out as different colors. All that changes is the board->display function. This separation of board and display gives you great flexibility.

Now... write the program to put things in the array and move them around... :slight_smile:

(Warning: This may not be the 'best' way to do it...but it's easy to program/debug - eg. The display function can easily send the board to the serial monitor when you hold down the 'debug mode' button...)

fungus:
Start by making an array:

This is your 'board'. Think of it like a chessboard with squares. In there you can put letters to represent things in the game, eg 'p' for 'player'. This is what you manipulate with your program. When you move a monster you look in the screen to see if there's a 'p' where the monster is moving too. When you get up to a 16x16 board you can start putting in 'w' for a wall, etc.

Next you need a function to copy/translate the board to the LED display. You call this function on every pass through 'loop()'. For single color LEDs you can have different characters come out with different brightnesses. If you change to RGB LEDs you can have 'p', 'w' and 'm' come out as different colors. All that changes is the board->display function. This separation of board and display gives you great flexibility.

Wow! Thanks man! I think that's exactly what I need. My only question however is in your code example:

fungus:
char board[3][3];

What you're trying to show is sort of having a led with x=3 and y=3 coordinated to do something right? Why are you using char? Does a char have special attributes that can memorize two variables at the same time or what? I don't fully understand this part.

And thanks for your example with walls and monsters. :wink: Its very clear but I would prefer just simple example that would just make a dot run around a 3X3 LED screen and controlling it by buttons. If I would accomplish this, I will consider your idea with RGB LED'S, which I extremely like.

But thank you very much, fungus. I kind of got a perspective of how approximately to do it.

Why are you using char?

Because it is just a single byte, you don't need more.

You need to write a routine that will turn on any LED given its coordinates. There are lots of ways of doing this. The simplest is to have a two dimensional array that define the pin number of each position.
Then all you need to do is:-

digitalWrite( pin[x][y], HIGH);