# Dear friends, due to increased popularity of our Tinusaur Project in the last couple of days and unexpected high demand for the kits in our online store . . . we just ran out of almost all of them.

We contacted our suppliers and ordered more parts but it could take up to 3 weeks until they arrive so please be patient.

If you need just the PCB you can order it directly from OSH Park at this address: https://oshpark.com/shared_projects/9IZsFiXL.

## The Tinusaur Plays Conway’s Game of Life

I was playing with the MAX7219LED8x8 library and writing some code for how to use a simple scheduler to automate the task of outputting the buffer to the LED 8×8 matrix. So I was thinking … may be writing a simple game will illustrate the use of those libraries very well. Because just few days earlier I was looking at some Arduino projects implementing the Conway’s Game of Life I decided to write it for ATtiny85 and MAX7219/LED 8×8.

The Game of Life is a classical computer game and a cellular automaton created by the British mathematician John Horton Conway in 1970. This is a zero-player game which means that once it starts no input from user is required to play the game’s turns – it goes by itself.

Its simple rules (outlined below) allow to be implemented on very simple microprocessor systems and Tinusaur (and ATtiny systems in general) could be perfect platform for that.

# Hardware

One Tinusaur Board connected to LED matrix 8×8 controlled by MAX7219.

# Drivers

The MAX7219Led8x8 library is used to output the pixels to the LED 8×8 matrix.

# The Rules

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called atick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

# The Program

The board is defined as a short byte array:

```typedef uint8_t life_board[8];
```

An initial board is specified by the bits in that array:

```life_board life_oscillators_blinkers = {
0b00000000,
0b00000000,
0b01110000,
0b00000000,
0b00000100,
0b00000100,
0b00000100,
0b00000000
};
```

Here are the most important functions that are implemented:

```void life_board_init(life_board buffer);
void life_board_out(void);
uint8_t life_cell_count(uint8_t cx, uint8_t cy);
void life_board_turn(void);
void life_board2_copy(void);
```

The life_board_init function initializes the board with preset values and life_board_out outputs the content of the boards to the LED 8×8 matrix.

The life_cell_count function counts how many neighbors the specified cell has.

The life_board_turn performs one turn of the game based on the rules described above. It reads the data from the main buffer life_board_buffer array and stores the result in the life_board_buffer2 array. After that life_board2_copy copies the new data to the main buffer which then is outputted to the LED 8×8 matrix.

Here is the source code of the most important function – life_board_turn:

```void life_board_turn(void) {
for (uint8_t y = 0; y <= 7; y++) {
for (uint8_t x = 0; x <= 7; x++) {
uint8_t count = life_cell_count(x, y);
if (LIFE_CELL_ISSET(x, y)) {
if (count < 2)
LIFE_CELL_CLR(x, y);
else if (count == 2 || count == 3)
LIFE_CELL_SET(x, y);
else if (count > 3)
LIFE_CELL_CLR(x, y);
} else {
if (count == 3)
LIFE_CELL_SET(x, y);
else
LIFE_CELL_CLR(x, y);
}
}
}
}
```

This function implements the rules of the Game of Life.

That’s it.

In the program there is some more code that shows about 10 well know and interesting shapes such as blinker, toad, beacon, glider, etc.

The source code is part of the MAX7219LED8x8 library and it is available at https://bitbucket.org/tinusaur/max7219led8x8.

This article was permanently put at Conway’s Game of Life page.

## UPDATE: MAX7219LED8x8 uses simple scheduler

The MAX7219LED8x8 library uses now simple scheduler to automate the task of outputting the buffer to the LED 8×8 matrix. This is not like a real task scheduler (in a real operating system) but it uses the ATtiny85 microcontroller‘s timer and its interrupt to do certain things on regular intervals.

So this is how I’ve got the idea …

While I was working on some code that uses the MAX7219LED8x8 library I figured out that the task of writing the content of the memory buffer to the MAX7219 could be automated by hooking some code to the ATtiny85 timer.

The modification and additions could be broken down into 2 parts:

First, the scheduling part that initializes the ATtiny85 timer, starts it and handles the hardware interrupt.

Second, the MAX7219LED8x8 library functions for setting/clearing pixels and outputting the buffer that now should work with the scheduler.

There are only 3 functions to handle the scheduling:

```void scheduler_init(void);
void scheduler_start(uint8_t max);
void scheduler_userfunc(void);
```

The scheduler_init function initializes the timer and the scheduler_start starts the timer task to be executed on equal interval defined by the max parameter.

The scheduler_userfunc function should be implemented in the application so it could be called on regular intervals.

There is not “stop” function at the moment since it was not needed in this particular case.

The code for initializing and starting the ATtiny85 timer is very simple:

```void scheduler_init(void) {
// Setup Timer
TCCR0A |= (1 << WGM01); // set timer in CTC mode
TIMSK |= (1 << OCIE0A); // set Bit 4 – OCIE0A:
// ... Timer/Counter0 Output Compare Match A Interrupt Enable
}

void scheduler_start(uint8_t max) {
// IMPORTANT: Requires TIMER0_COMPA_vect to be setup.
sei(); //  Enable global interrupts
OCR0A = max;    // set value for OCR0A - Output Compare Register A
// Prescale and start timer: 1/1024-th
TCCR0B |= (1 << CS02) | (0 << CS01) | (1 << CS00);
}

// Define interrupt vector
ISR(TIMER0_COMPA_vect)
{
scheduler_userfunc();
// Note: No need to clear flags in TIFR - done automatically
}
```

On the MAX7219LED8x8 side there are only 3 functions currently implemented:

```void max7219s_init(void);
void max7219s_start(void);
void max7219s_buffer_out(void);
```

The max7219s_buffer_out function is the one called within the scheduler_userfunc timer handler.

Everything else remains the same – we use MAX7219_buffer_set(x, y) to set pixel and MAX7219_buffer_clr(x, y) to clear pixel.

What could be improved and optimized: output the buffer only when it is changed.

More information is available at the MAX7219LED8x8 library page.

The source code of the MAX7219LED8x8 library, the scheduler and everything else is available at https://bitbucket.org/tinusaur/max7219led8x8.

## MAX7219 driver for LED Matrix 8×8

MAX7219LED8x8 is a C library for working with the MAX7219 display driver to control 8×8 LED matrix. It is intended to be used with the Tinusaur board but should also work with any other board based on Atmel ATtiny85 or similar microcontroller.

The MAX7219 is manufactured by Maxim Integrated is compact, serial input display driver that could interface microcontrollers to 64 individual LEDs such as 8×8 LED matrix. Only one external resistor is required to set the segment current for all LEDs.

To put that in simpler words – with the MAX7219 driver it is possible to control 8×8 LED matrix using just 2 wires serial interface – one for the sync clock and one for the data. There is another wire that could be used to enable/disable the communication with the chip. The maximum frequency for the serial interface is 10MHz.

The LED matrix 8×8 is connected almost diretcly to the MAX7219 driver – only few external components are required.

Working with MAX7219 is very simple – turning on and off individual LEDs is done by sending 2-bytes command to the driver containing the row and the byte which bits define which LED value to set.

There are also few other command that are needed during the initialization process.

The library supports short buffer – only 8 bytes in size – to keep the values before sending them to the driver.

MAX7219LED8x8 is written in plain C and does not require any additional libraries to function except those that come with the WinAVR SDK.

Using it is very simple …

```    MAX7219_init();
MAX7219_buffer_set(2, 3); // Set pixel
MAX7219_buffer_clr(4, 5); // Clear pixel
MAX7219_buffer_out(); // Output the buffer
```

Please continue to MAX7219LED8x8 page to see full source code the rest of the article.

# External Resources

The source code of the MAX7219LED8x8 library is available at https://bitbucket.org/tinusaur/max7219led8x8

MAX7219 specification and datasheet:

## TinuDHT – ATtiny Library for DHT11

Ever wanted to do a project with that cheap DHT11 temperature/humidity sensor and did not want to go the Arduino way but with a simple ATtiny85? You probably know already about  the issues with the existing Arduino based libraries running on the ATtiny microcontrollers, but can’t deal with them. TinuDHT is our answer to this.

TinuDHT is a C library for working with the DHT11 temperature/humidity sensor intended to be used with the Tinusaur but should also work with any other board based on ATtiny85 or similar microcontroller.

The DHT11 is very basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor for measurements, and sends out the info to the data pin. It is relatively simple to use it, but requires precise timing to retrieve the data correctly. One disadvantage of this sensor is that you can get new data from it no more often than once every 1 or 2 seconds.

The primary problem with the direct use of the Arduino libraries is that the ATtiny85 and Tinusaur in particular do not have enough resource to handle the send/receive process properly, i.e. not enough CPU power, in result of which the timing of the signals that are sent to the sensor and received from it become messed up. In addition those libraries use Arduino specific code and/or C++ specific syntax which makes them incompatible with the plain C language.

TinuDHT library is based on DHT11Lib code. It was adapted for ATtiny, removed Arduino dependencies and timing was adjusted to work well on ATtiny85 at 1 MHz. There are few other changes and optimizations for speed and size.

TinuDHT is written in plain C and does not require any additional libraries to function except those that come with the WinAVR SDK.

Please go to TinuDHT page to see the full document.

The source code of the TinuDHT library is available at https://bitbucket.org/tinusaur/tinudht.

## Tutorial 002: Fading LED x1

Another beginners tutorial is on the way – this time about a fading in and out LED.

This is simple tutorial that shows how to connect a LED to the ATtiny85 based Tinusaur board and write a program that makes the LED to fade in and out using PWM (pulse-width-modulation) technique.

Note: The code in this tutorial does not use the built-in PWM capabilities of the ATtiny microcontrollers, instead it uses direct bit manipulation since this un easier way to understand how it works. Another tutorial should cover the PWM functionality that is built into the microcontroller.

The Tinusaur board is a standard ATtiny breakout board so this could be applied to almost any other board that has ATtiny microcontroller on it. The code was tested to work with ATtiny13, ATtiny25, ATtiny45 and ATtiny85 but will probably work on any other ATtiny microcontrollers as well.

Please go to Tutorial 002: Fading LED x1 to see the full document.

You can also check the Tinusaur Board – Assembling Guide and the WinAVR – Setup Guide.

## Arduino IDE – Setup Guide

We have put together a short guide how to setup and use Arduino IDE for programming the Attiny85 microcontroller and the Tinusaur Board in particular.

Note: This guide was tested under Microsoft Windows 8.1 operating system.

Note: The example source code was tested on ATtiny85 microcontroller installed on a Tinusaur Board and programmed using USBasp ISP programmer.

Note: This is not a guide how to use the Arduino IDE but rather how to setup one for use with AТtiny microcontrollers and specifically the Tinusaur.

The guide goes through the:

• Installation of the Arduino IDE.
• Setup the IDE for ATtiny and Tinusaur, adding boards definitions.
• Setup USBasp Programmer, just brief overview.
• Test the Arduino IDE with the Tinusaur, writing blinking LED program.

The entire Arduino IDE Setup Guide is available under the Guides menu.