Tetris Clone With OLED SSD1306(I2C) for Arduino Nano / Uno

19,541

48

34

Introduction: Tetris Clone With OLED SSD1306(I2C) for Arduino Nano / Uno

This Instructable is about getting Tiny Tetris running on a 128x64 OLED display using an Arduino Nano or Uno.

Step 1: What You Need.

Arduino Nano or Arduino Uno:

Nano

Uno

Display:

OLED I2C SSD1306

Keypad:

AD Analog Keyboard Module

Note: other key pads and switch configurations could be used but you would need to change the code.

Some cables to connect things:

Cables

Step 2: Uno Wiring

Uno wiring:

Note: the key pad shown represents the pad in the parts list, I have not built the one shown but if you did it would be easy to get it working, only a few small changes would be needed in the code.

Fritzing Part

Step 3: Nano Wiring

Nano wiring:

Note: the key pad shown represents the pad in the parts list, I have not built the one shown but if you did it would be easy to get it working, only a few small changes would be needed in the code.

Fritzing Part

Step 4: The Code

If you are new to Arduino have a look at this to get started:

How to have fun with Arduino

Code attached and on GitHub below:

Code on GitHub - this has the most up to date code including Art work and Sound!

There is still a lot to be done with the code, see below:

High score functionality.

[Done] Better key pad control code.

Decent random number generator.

[Done] Start thinking about adding sound.

Create a letter font, create a proper system for rendering numbers and letters.

Tidy up code and optimize for memory, sort out the globals and types.

Create defines for all the magic numbers but they are useful for now.

Step 5: Have Fun!

3 People Made This Project!

Recommendations

  • Reclaimed Materials Contest

    Reclaimed Materials Contest
  • Paint Challenge

    Paint Challenge
  • Laser Challenge

    Laser Challenge

34 Comments

0
beans
beans

6 weeks ago

Ive tried running this on battery power (6v) through the vin pin on the nano. But now the buttons arent debounced and the pieces fly accross the screen when a button is pressed. When plugging in through usb there is no issue. How can I fix this? Thanks

0
BADFEED
BADFEED

Reply 6 weeks ago

Hi beans,

I really wish I had never used the analogue key pad for
this project, I was trying to keep things simple but it has caused so
many issues.

I think the values from the analogue key pad are
different because of the different supply voltage - when supplying the
Nano with an external voltage the values returned from the AD converter
will be different (I think)

There is a example sketch called
AnalogInOutSerial in examples, try your key pad with that and record the
values you get for each key press, then change the processKeys()
accordingly:

for example the code for key right is below:

else if ((analogKey < 510) && (analogKey > 500)) {


Debounce++;

if (Debounce > 10) {

if (processKey) {

uiKeyCode = KEY_RIGHT; //key will be processed immediately
key.right = millis();

}

if (millis() < key.right + leftRight) processKey = false;
else {
processKey = true;
acceleration = acceleration + 70;
if (acceleration > leftRight) acceleration = leftRight;
}

}
}

change the 510 and 500 to be withing a range of the values that you recorded.

Sorry I can't be of more help.

I want to revisit this project and use proper assigned pins for each input, also lots more things.

PS.
on git hub there is code for this although the acceleration is not
addressed correctly I think. I really need to revisit this project :)

Good luck!

0
lenyamash8
lenyamash8

1 year ago

При подключении вместо заставки появляются точки, разбросанные по экрану. Это не так страшно, но когда игра запускается, работает только одна клавиша

0
BADFEED
BADFEED

Reply 6 weeks ago

When
you connect, instead of a splash screen, there are points scattered
around the screen. It's not that scary, but when the game starts, only
one key works.

Please see reply to beans below, sorry for -very late reply-

Need to revisit this project :)

0
stephanlange95
stephanlange95

1 year ago

Hi, Imade, what is written in this manual with the same components, but I have the problem, that, whatever I do, the display is still black. I tried it with the uno and nano. I tried it with two different displays. What can I do, that it will work?

0
BitSand
BitSand

Reply 1 year ago

Hi stephanlange95,

The best approach would be that you first try getting demos going for the SSD1306 OLED.

Once you get something on the screen, go back to the game.

Hopefully that helps, come back to me after that if you still have trouble.

Take care.

0
stephanlange95
stephanlange95

Reply 1 year ago

Ok, I will. Do you can recommend any demo?

0
seawarrior181
seawarrior181

1 year ago

Outstanding project. What modifications would be necessary to make this work on a 128 x 32 SSD1306 OLED?

0
lukas.kraemer.2004
lukas.kraemer.2004

1 year ago

Awesome project and thanks a lot for sharing, but I have one question:

I have seen several tutorials on tiny tetris and tried many things, but only the display works for me. After I turn on the Arduino, the game works quite normally and the display shows what it should, but it is impossible for me to control the individual blocks using the buttons. I've already tried to recreate the above circuit with the different resistors, but it still does not work, because I do not know what I need to change in the dpad.cpp file to make it work. Do you still have a tip, what else could I test? Or should I simply buy a ready-made solution like this one? Will it then work without me having to change the code?



Thank you for possible answers


https://www.ebay.de/itm/AD-Keyboard-Simulate-Five-Key-Module-Analog-Button-for-Sensor-Expansion-Bo-Nd/113980865162?_trkparms=aid%3D111001%26algo%3DREC.SEED%26ao%3D1%26asc%3D20170511121231%26meid%3Df6c29bd1876147c1a171de199a36a39d%26pid%3D100675%26rk%3D1%26rkt%3D15%26sd%3D113980865162%26itm%3D113980865162%26pmt%3D1%26noa%3D1%26pg%3D2380057&_trksid=p2380057.c100675.m4236&_trkparms=pageci%3Afd136ca2-1363-11ea-becc-74dbd1802852%7Cparentrq%3Abc0e2ba316e0ad4fb8451e51ffe0bbf8%7Ciid%3A1

0
joymonkey
joymonkey

Reply 1 year ago

I used a slightly different button layout, and different resistors, so had to determine what the correct values were and edited dpad.cpp accordingly.

To figure out your values, edit dpad.cpp, add a command to print the button value to serial. At line 47, change it from...

currentPos = getPosition(KEYPAD_PIN)*1;
delay(100);

to...

currentPos = getPosition(KEYPAD_PIN)*1;
Serial.println(currentPos);
delay(100);

Now upload the sketch and watch the serial monitor on your computer. When you press each button it should print a different value on the serial monitor. Note the value for each button.
Then edit lines 25-29 so the buttons action will correspond with the values that your Arduino is seeing.
{460, 480}, //KEY_MIDDLE 0

{122, 234}, //KEY_LEFT 1

{700, 750}, //KEY_RIGHT 2

{-1, 70}, //KEY_DOWN 3

{260, 350} //KEY_ROTATE 4



0
lukas.kraemer.2004
lukas.kraemer.2004

Reply 1 year ago

Thank you, everything works fine now. I think it's great that you answered me so fast and you described exactly what I have to do. I'll probably solder the circuit to a circuit board and then make a matching enclosure with the 3d printer. I have considered using as mobile power supply either a Lipo or a 18650 battery. I have already ordered the corresponding charging electronics (TP4056). The problem is that I need to transform the approximate 3.7V to at least 5-6V for the Arduino to function normally. I still have an Xl6009 step up module lying around, but it's way too big in my opinion and even with over 4 amperes of maximum current a total waste. Do you have any recommendation for a small step up module (about 500ma) which works reliably?

0
joymonkey
joymonkey

1 year ago

Thanks again @BADFEED !
Here's my "almost done" Tiny Tetris project with the 2.42" OLED...

IMG_20191201_173630-COLLAGE.jpg
0
joymonkey
joymonkey

2 years ago

I love this!
I've been looking for a high-contrast, simple Tetris clone as a gift for my father, who suffers from dementia but loved Tetris back in the Gameboy days. Since his eyesight isn't what it used to be, I'm going to try and get it working with a larger OLED. I found a 2.42 inch I2C one with a matching resolution, but it uses the SSD1309 instead of the usua SSD1306. Hopefully it won't be too tough to tweak for the other chipset.

0
BADFEED
BADFEED

Reply 2 years ago

Hello,

I had a quick look at the datasheet for the SSD1309 and the only real difference I can find to the SSD1306 is driving current -this would make sense as your display is bigger.
So I think all should work as is.
Remember to check out Git Hub, it has a lot of extras including a different key pad configuration.
I really want to revisit this project at some stage, there are a lot of improvements that can be made.
Best of luck!

0
joymonkey
joymonkey

Reply 2 years ago

Thanks! I starting testing the SSD1309 display yesterday. I'm using a generic one I picked up on Amazon, that appears identical to the ones produced by DIYmore (which some people seem to have success with on the Arduino forum)...
https://amzn.to/2OyvXib
https://www.diymore.cc/products/2-42-inch-12864-oled-display-module-iic-i2c-spi-serial-for-arduino-c51-stm32-green-white-blue-yellow

I set the resistors accordingly, and the I2C Scanner sketch does see the display at address 0x3C, but I haven't had any luck getting it to display anything yet. I'll keep you posted!

0
joymonkey
joymonkey

Reply 2 years ago

I've gotten this OLED working using the U8G2 library.
First problem was that this display takes a 3.3V supply only. I think I fried my first one by giving it 5V
To get it running via I2C I had to connect the display's Reset pin to 3.3V via a 10K resistor, and to GND via a 10uF capacitor.
Now I've got the example sketches in U8G2 running using the constructor...
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

However, when I try running the TinyTetris sketch (from GitHub), I just get garbage (a static display, no motion). Any ideas?
I suppose I could start porting the sketch over to use the U8G2 library, but I'm not much of a coder, and I'm also not sure if there'll be enough RAM with that library piled in.
Edit: I tested by sending the OLED_INVERSE_DISPLAY and OLED_NORMAL_DISPLAY commands during setup() a few times, and that does appear to work (the garbage displayed flips to negative and back), so it *is* working, but something needs tweaking somewhere.

IMG_20191122_082326.jpg
0
BADFEED
BADFEED

Reply 2 years ago

Emm. Could you try the TinyTetris.ino linked in this instructable and let me know how it goes?
Its about as simple as it gets and might rule out issues with the graphics on git hub.

I'm not sure what the issue could be..

I don't think you will be able to port to the U8G2 library as Tiny Tetris uses low level commands sent directly to the SSD1306 to achieve the speed.

What kind of micro controller are you using and do you have one of the smaller OLED -SSD1306- screens to try?

Ill keep thinking about it..



0
joymonkey
joymonkey

Reply 2 years ago

Thank you!
I had been trying a bunch of different boards (Teensy 3.2, Metro M4, Arduino Zero, Uno), and should have really been focusing on one. I went back to basics and did a clean set up using an Arduino Pro Mini 3.3V/8Mhz. I tested using the sketch from this post and a .96" OLED. Powered up and it worked first time!
Then I switched over to the 2.42" OLED (again using the 10K pullup and 10uF cap on the displays Reset pin). Lo and behold; it just works! Uploaded the GitHub version and it also just works!
Thanks so much for this! I know my father will love it!

IMG_20191124_110657.jpgIMG_20191124_111206.jpg
0
big daddy 69
big daddy 69

3 years ago

my right button and my spin button make really fast movements but overall it works well