An ATTiny85 based handheld game

WP_20161228_21_29_19_Pro (2)Yes, I love gaming.  And there is nothing more satisfying, to me, than building, sometimes coding and playing something I made.  Now, I don’t always WRITE the code, after all, time is a premium these days, but I don’t mind taking something someone else did and making it work with what I built.  For this project, I was very lazy: the design is also someone else’s.  I really wanted to do something with the ATTiny85, but have not really done anything outside of playing with the Adafruit Trinket or Digispark.

So, for this little project, I wanted to also use one of my cool little ssd1306 OLED screens.  While perusing the net, I came across Webboggles.com.  Here, they are selling a nifty little kit called the ATTiny Arcade Keychain. It looks to be of high quality and the author (Ilya Titov) goes through much detail in the design and build.  There are several posts about it and the games.  The game code and schematic have been made readily available. The first of the games was breakout and that is where I started. 

To build the little game, you will need the following:

  • Attiny85 + dip8 socket
  • SSD1306 OLED screen
  • 3x push buttons
  • 2x resistors (10kOhm optimal)
  • Piezo speaker
  • 3V 2032 coin cell battery
  • perf or vero board
  • I used a little speaker out of a toy cell phone instead of the piezo. I would also recommend socketing the screen instead of soldering it directly, you don’t have to, but I wish I had now.attiny85game_schem

    One other thing to keep in mind, you will need a way to program the ATtiny 85 chip, which I will describe in a follow up post. I actually built two programmers: one on breadboard and a quasi shield for the UNO.  I like that better.

    As you can see from the schematic, it is really simple. Even so, I made a few mistakes at first.  Not paying attention to the chip pinout, I got the pins reversed from pin 8 to pin 5. I, for whatever the reason, assumed the actual pin 8 was pin 5, instead of going from pin 4 to pin 5 at the bottom of the chip. Once I figured that out (I had yet to apply power) the rest was easy. I also got SCL and SDA backward (hey, I’m old).  Once I got my mistakes corrected, I was amazed that this simple circuit was now a little game machine.  Now, you aren’t going to play Call of Duty or even Doom, but you can play many classics on the devices.  I am going to build one or two more as this was a blast. I would also encourage ordering a kit from Webboggles as well.

    My next post will discuss creating an Attiny 85 programmer for the UNO.

    WP_20161231_15_20_15_Pro (2)

    Advertisements

    It may be small, but this OLED is cute and useful and that little DHT11 really knows when something is hot or not

    WP_20140302_013As I stated in a previous post, I have a fetish for displays. Any display. Again, while searching eBay for some parts for my Half-Byte Console project, I found a small, .96 inch OLED display. This little gem uses either SPI, Serial or I2C to interface with your device.  I ordered one and received it a few days ago. As is with most of these cheap parts from China, there was little in the way of documentation or code.  Fortunately, more than one vendor sold them and I found some code, which was good since none of the code I found on the net seemed to work.

    This little display is advertised as monochrome, but it really is a two color display.  The top two lines have a yellow background OR yellow foreground and the rest of the screen is blue on black or black on blue.  It was likely out of an old cell phone. To be honest, I’ve not done much research on the history of the screen itself.

    Since there are a number of ways to connect the display, which uses the SSD1306, I chose the method in the code that I found:

    1. SCL –>9
    2. SCA –>8

    Of course, VCC goes to either 3.3v or 5v and GND to ground.  Once the hardware was connected, I uploaded the code I found. 

    The code, found here (but, download it from here, instead), was a simple demonstration and did not come with a very versatile library. Though, it does appear to use the Adafruit GFX library.

    Although too small for most of my uses, I wanted to see if it may fit a project I have in mind further down the road. Turns out, it will. So, I then attempted to bang together WP_20140302_016part of that idea using a DHT11 temperature sensor. This sensor also gives the relative humidity. So, I connect the OLED and the DHT11 sensor to an Arduino Mini Pro. The DHT11’s data pin goes to Pin 2 (TX) on the Arduino, and pin 1 goes to 5v and the third pin to GND. I incorporated the DHT code into my OLED demo and then … problems.  Turns out, the OLED library has no built in method to output numerical data.  Remembering my Tiny Basic code, I stole some code from it to output numerical data and…viola! Works like a charm (see photo above.)

    At six dollars, the screen isn’t really worth it, as there are better screens out there for the same or even less.  However, if you can these cheaper, they will make great little status displays or even part of a smartwatch or other wearable device.

    To use my sample code below, you will need the following libraries installed:

    • SoftwareSerial (should be part of your Arduino installation)
    • IIC_without_ACK.h (use the link above)
    • TinyDHT (download here)
    • avr/power.h (part of your install)

    A note about the AdaFruit libraries: While provided free of charge, developing these is not always an easy task (though, it might for Lady Ada) and, as such, you should patronize them whenever you can. They have a great selection and good pricing. I buy quite a bit from them.

    The display is also capable of graphical display, but I’ve not yet bothered to try and put an image on the screen. There is a bitmap function in the limited library, but you must convert your image to a C array first.  Take a look at the header file for the OLED font.

    have fun!

    Sample Code:

    #include <SoftwareSerial.h>
    #include <IIC_without_ACK.h>
    #include “oledfont.c”  

    #define OLED_SDA 8
    #define OLED_SCL 9

    IIC_without_ACK oled(OLED_SDA, OLED_SCL);//9 — sda,10 — scl

    /*
    * DHT Temperature Sensor
    // Connect DHT pin 1 (on the left) of the sensor to +5V
    // Connect DHT pin 2 of the sensor to whatever your DHTPIN is
    // Connect DHT pin 3 (on the right) of the sensor to GROUND
    */
     
    // include the library code

    #include <TinyDHT.h>        // lightweight DHT sensor library
    #include <avr/power.h>      // needed to up clock to 16 MHz on 5v Trinket
     
    // Uncomment whatever type sensor you are using!
    #define DHTTYPE DHT11     // DHT 11
    //#define DHTTYPE DHT22   // DHT 22  (AM2302)
    //#define DHTTYPE DHT21   // DHT 21 (AM2301)
    #define TEMPTYPE 1        // Use Fahrenheit (0 for celsius)
    #define DHTPIN 1          DHT dht(DHTPIN, DHTTYPE); // Define Temp Sensor
    static unsigned char *spt;
    /***************************************************************************/
    static void pushb(const char b)
    {
        spt–;
        *spt = b;
    }

    /***************************************************************************/
    static unsigned char popb()
    {
        unsigned char b;
        b = *spt;
        spt++;
        return b;
    }

    /***************************************************************************/
    void printnum(unsigned char x, unsigned char y, int num)
    {
            char st[]=”   “;
        int digits=0;
            int pos=0;
           
        if(num < 0)
        {
            num = -num;
            oled.Char_F6x8(x,y,”-“);
        }

        do {
            pushb(num%10+’0′);
            num = num/10;
            digits++;
        }
        while (num > 0);
            while(digits > 0)
              {
                      st[pos]=popb();
                      x++;
                digits–;
                      pos++;
              }
             oled.Char_F6x8(x,y, st);
    }
     
    void setup() { 
      dht.begin();  // Initialize DHT Teperature Sensor
      oled.Initial();
    }
     
    void loop() {
      int8_t h = dht.readHumidity();               // Read humidity
      int16_t t = dht.readTemperature(TEMPTYPE);   // read temperature
     
      delay(2000);
      oled.Fill_Screen(0x00);
     
      if ( t == BAD_TEMP || h == BAD_HUM ) { // if error conditions (see TinyDHT.h)
        oled.Char_F6x8(0,0,”Bad Read on DHT”); //   print error message
      } else {
        oled.Char_F6x8(0,2,”Humidity:”);
        printnum(60,2,h);
        oled.Char_F6x8(78,2,”%”);
        oled.Char_F6x8(0,4,”Temp: “);
        printnum(36,4,t);
        oled.Char_F6x8(50,8,”F”);    
      }
      delay(2000);  // Read temp every second (2000 ms) (DHT sensor max rate)
    }