# Snake on home-made 6x6 matrix

Hi all,

I've build and soldered a 6x6 led matrix, and my goal is to play snake on it. I've retrieved some code online, made for a 8x8 matrix, and made some adaptation. It almost works !

... but it displays each line twice. I've filmed the result, you can watch it here : Snake Arduino Issue - 6x6 matrix hand made - YouTube

You can see the snake moving from left to right. It's supposed to be on line 5 (counting from 1 from the top), and not 4 and 5, while the apple is supposed to be only on line 6, and not 1 and 6.

The code was too long to fit in this message, you can find it here : May 19 2:17 PM - Codeshare (let me know if there's another solution to share it with you guys)

Diagram looks like this one, but for 6x6 matrix, all on digital In/Out, with the needed resistors. Pins 2 to 7 are for columns (negative pins of LEDs), and 8 to 13 for rows (positive pins). No button for the moment, but they'll come on pins 0 and 1 for right and left.

I've tried to solve this by myself for days, and it's driving me crazy. I even built a second matrix to check that my soldering wasn't the issue. Would anybody be able to help me ? The code has tons of comment in order to help you understand it.

Thank you

Schematic diagram please! No point us trying to help if your Arduino or leds only have a few hours to live!

My bad, I've added a schematic diagram. I don't know how to create one from scratch, so I've put one that is almost the same - a generic one for a LED matrix.

tcibils:
a generic one for a LED matrix.

A generic circuit for destroying an Arduino.

I saw resistors on your bread board in the video. Where are they on your schematic? What value are they?

I've taken the schematic on the internet. I've put 1k Ohm resistors before each row (positive input of each LED). I've used this DIY here, but without the multiplexer.

Those 1K resistors may have saved your Arduino from severe damage. Why did you choose that value? If you don't know, then you have been lucky.

Just because you see a schematic on the internet, does not mean it is a good design. That first schematic you linked to would damage the Arduino and the leds.

The schematic on the diy site is better, but you say you followed it except the multiplexer? Does that mean you did use the transistors? What type of transistors did you use and what resistor have you used on the transistor's bases?

The "ghosting" or "bleeding" problem you are seeing on your display is caused by the function called outputDisplay() in your code. It is badly written. I will suggest some changes:

``````// We update the physical display of the LED matrix
void outputDisplay() {

// Store the row number of the matrix that is currently lit and the time it was lit
// The keyword "static" means that the variable's values are not lost between calls
static int currentRow;
static unsigned long lastUpdateTime;

// Get the current time
unsigned long timeNow = millis();

// Is it time to move to the next row of the matrix?
if (timeNow - lastUpdateTime >= 3) {

// Turn the previous line off
digitalWrite(linePin[currentRow], LOW);

// Update the current matrix row
if (currentRow < numberOfRows) currentRow++; else currentRow = 0;

// We then proceed column by column
for (int j = 0; j < numberOfColumns; j++) {
// A column must be 0=LOW to receive electricity and light up, and 1=HIGH to block it and hence stay off.
// If the LEDMatrix contains a value >0, ie red, orange or green, the dot should light up
if (LEDMatrix[currentRow][j] > 0) {
// We hence set this column to LOW to allow the electricity to flow in it
digitalWrite(columnPin[j], LOW);
}
else {
// On the other hand, if the value is 0, we set the column to HIGH to block the electricity
digitalWrite(columnPin[j], HIGH);
}
}

// We light up the current row and record the time it was lit
digitalWrite(linePin[currentRow], HIGH);
lastUpdateTime = timeNow;
}
}
``````

Hi,
Welcome to the forum.

If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment, but try code tags first.

You can also attach your schematic diagram as well.

Thanks.. Tom...

Thanks a lot for your help, I've tried it, and it's working !

So my understanding is that I was poorly using the timer, right ? The "static" for the current row values only makes the code cleaner.

For the resistors values : I've killed a LED once by forgeting it, and now know my lesson on this. I followed the DIY, except the multiplexer, and hence exepct also the transistors (they make sense only because of the multiplexer). I could've calculated the value with Ohm's law, but I've supposed that if 1kOhm resistors were fine for 8 leds, they were fine also for 6.
Basically, it's a LED matrix as in the first schematic, but with resistors with values as in the second.

Anyway, thank you for your help again, it's now working fine =D

tcibils:
I was poorly using the timer, right ?

If by "timer" you are referring to these lines of code:

``````#include <TimerOne.h>
Timer1.initialize(2000);
``````

Those lines are not doing anything, they can be removed. This was true even before my changes.

tcibils:
The "static" for the current row values only makes the code cleaner.

Not just cleaner. If you remove the "static" keywords, you will almost certainly find that the code will not work. As I said in the code comments, "static" means that the value of those variables is preserved from one call of outputDisplay() to the next. This is important because I changed the way outputDisplay() works. Before, each time outputDisplay() was called, it lit every row of the matrix, but only for a very very short period (and also lit another row because of the bad coding). After my change, outputDisplay() lights only one row at a time, and leaves that row lit for 3ms. The next time outputDisplay() is called, if 3ms have not yet passed, it does nothing, but if 3ms has passed, it lights the next row and so on.

tcibils:
I followed the DIY, except the multiplexer, and hence exepct also the transistors (they make sense only because of the multiplexer).

Incorrect, the transistors would be very useful even without the multiplexer. The multiplexer and the transistors serve different purposes. The multiplexer reduces the number of Arduino pins required from 10 to 2 for the rows. The transistors increase the amount of current that can be supplied to each row of leds.

An Arduino pin can source or sink a maximum of 40mA. The pin cannot limit the current itself, and if you allow more than 40mA to flow, it will be damaged. With the 1K resistors you are using now, which are in series with the leds, the total current with all 6 leds on is less than 40mA, so that is OK. But it does make the leds quite dim. The leds would be brighter if you used a lower value series resistor, such as 220R, but then the total current for the row would exceed 40mA and damage the pin.

Using transistors on the row pins would allow you to use the 220R resistors because a transistor can withstand more current than the Arduino pin. For example a bc337 transistor can allow up to 800mA.

When using transistors like these (known as "BJT" or "NPN" or "PNP") it is important to limit the current flowing from the Arduino pin to the "base" pin of the transistor. This current can be very small, so 1K or even 4K7 is probably OK. The transistor then "amplifies" this small current to the large current needed by 6 leds with 220R series resistors.

I hope I have cleared up some misconceptions for you. Feel free to ask more questions.

Thank you for your answer, I got back in the project and understood your comments on static and transistors Indeed, the leds are too weak currently, I'll add an external source of power controlled by the transistors to solve that.

Thanks again, a bit late