Go Down

### Topic: Implementing 2D rendering in a 3X3 LED display. (Read 2019 times)previous topic - next topic

#### mixania

##### Nov 09, 2012, 08:03 pmLast Edit: Nov 10, 2012, 08:19 pm by mixania Reason: 1
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!

#1
##### Nov 09, 2012, 08:33 pm
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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### mixania

#2
##### Nov 09, 2012, 08:42 pm

First off, what does this indicate?

"Expert level: ?"

My self criticism of my mastered skill with Arduino

Thanks for the answer by the way ))

#### fungus

#3
##### Nov 10, 2012, 01:00 pmLast Edit: Nov 10, 2012, 02:18 pm by fungus Reason: 1

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...

(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...)
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### mixania

#4
##### Nov 10, 2012, 08:15 pmLast Edit: Nov 10, 2012, 08:17 pm by mixania Reason: 1

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:

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. 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.

#### Grumpy_Mike

#5
##### Nov 10, 2012, 09:12 pm
Quote
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:-

Code: [Select]
`digitalWrite( pin[x][y], HIGH);`

Go Up