20 Feb 2017

The basics of Arduino game programming: Start & Board Setup. (Part 1)

Preamble

Have you ever played games? Sure! Would you like to build and program yours one? Follow to me!

I guarantee, that the programming of games is one of the fun parts of computer education along with brainstorm. Game programming is a complex task which always includes: a creation of logic, data flow management, debugging, optimization. Be sure, if you able to program even a simple old 8-bit game you have achieved a lot.

let's start.

Hardware is based on popular Arduino board. Nevertheless, the principles of programming can be implemented on others platforms: PCs, Texas Instrument's Launchpad, Adafruit etc.

Hardware Kit:
  1. Arduino Nano/UNO (ATmega328) 16 MHz board. Original or clone or any Arduino compatible board will good.
  2. OLED Display SSD1306 with I2C or SPI interface connection. SPI is better for speed and performance. Currently, I use I2C.
  3. Breadboard: MB102 Breadboard 830 Point Solderless.
  4. Buttons - 6 pcs.
  5. LED: Red color - 1 pcs.
  6. Transistor: 2n3904 - 1pcs. 2n2222 or any other general purpose small signal npn-type.
  7. Speaker - 1pcs. Piezo or electromagnetic type.
  8. Resistors: 240 - 270 Ohms - 4 pcs.
  9. Resistor: 1 - 10k - 1pcs.
  10. Capacitor: 22n - 100n - 1 pcs.
  11. Jumpers, Dupont Cable or 0 Ohms resistors.
Everything of the list you can get in local or online stores: Adafruit, SparkFun, Aliexpress etc. I got from China Ali!

Connections and schematic:


Breadboard
Breadboard

Schematic
Schematic
The OLED displays require a 3.3V power supply and 3.3V logic levels. Many OLED modules from China have an onboard voltage regulator, that allow you connect display directly to 5v power supply line. However, they do not have an onboard logic level converter. Fortunately, the display driver SSD1306 chip is good accepting even 5v Arduino boards logic levels. To avoid over current, I use two resistors R2, R3 by 240 - 270 Ohms.

You no need to use these resistors if you got an OLED from Adafruit, they are fully and safety compatible with any 5V microcontrollers, such as the Arduino board.

Software:
  1. Install Arduino IDE / Compiler on your machine. I use 1.8.1 version.
  2. Launch Arduino, select Tool menu -> Board: choose your target board Nano or UNO. Select  Tool menu again -> Processor: choose ATmega328.
Ok, next step is installing Adafruit libraries for managing OLED display.
  1. Select Sketch -> Include Library -> Manage Libraries
  2. Search and install: Adafruit SSD1306
  3. Search and install: Adafruit GFX Library
Almost done.

Open with text editor Adafruit_SSD1306.h 
Path: Arduino Projects Folder / libraries / Adafruit_SSD1306

Find 73 to 75 lines and edit to following:

     #define SSD1306_128_64
//   #define SSD1306_128_32
//   #define SSD1306_96_16

If you do not do this for 128 x 64 OLED display, you will get missing of lines on the display output.

Troubleshooting

Mostly, display modules from china (driver chip) use 0x3C i2c address. If you got a problem with initialization: display completely does not respond, probably the problem is on an incorrect i2c address. Be sure in the correct electric connections.

Try 0x3D, if still not working, you should scan to find a right address. Google to "MultiSpeedI2CScanner".

Electric Tips and Safety Advice
  1. Double check connection before power on.
  2. Always! Always keep by hand a metal leg of a component when you cutting out it by wire-cutters. By damn laws, it shoots straight in the eye!
CODE:
---------------------------------------------------------------------

/*

   Game Starter Project 15.02.2017
   Board: Arduino Nano (ATmega328), OLED Display: SSD1306 i2c
   Author: Aleksey M.
   http://eekit.blogspot.com

*/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Display Init
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// Hardware Init
#define UP          8
#define DOWN        10
#define LEFT        9
#define RIGHT       5
#define A           A0
#define B           A1
#define SPEAKER     3
// Debugging LEDs
#define LED         2
#define BOARDLED    13

void setup() {
  // Hardware setup
  pinMode (UP, INPUT_PULLUP);
  pinMode (DOWN, INPUT_PULLUP);
  pinMode (LEFT, INPUT_PULLUP);
  pinMode (RIGHT, INPUT_PULLUP);
  pinMode (A, INPUT_PULLUP);
  pinMode (SPEAKER, OUTPUT);
  pinMode (LED, OUTPUT);
  digitalWrite(SPEAKER, LOW);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  // initialize with the I2C addr 0x3D (for the 128x64)
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(500);

  // Clear the buffer.
  display.clearDisplay();

  // Invert Display.
  //display.invertDisplay(true);

  display.setTextSize(1);
  display.setTextColor(WHITE);
}

void loop() {
  // Game code here
  display.clearDisplay();
  display.setCursor(0, 10);   
  display.println("Hello Game!");
  display.display();
}

---------------------------------------------------------------------

Upload it. Do you see "Hello Game!"? If not, check connections and repeat everything one more time.
SSD1306 OLED Display


No comments:

Post a Comment

Popular Posts