#include <avr/interrupt.h>
#include <avr/io.h>

#define THRESHOLD 150

int j = 0;
int i = 0;

/* image data */
unsigned char data[5][8] = {
	{
		0x30,	0x78,	0x3C,	0x1E,
		0x3C,	0x78,	0x30,	0X00
	},

	{
		0x28,	0x44,	0x22,	0x02,
		0x22,	0x44,	0x28,	0X00
	},

	{
		0x20,	0x20,	0x24,	0x04,
		0x24,	0x20,	0x20,	0x00
	},

	{
		0x40,	0x7E,	0x40,	0x02,
		0x40,	0x7E,	0x40,	0x00
	}

};

/* timer overflow interrupt routine */
ISR(TIMER0_OVF_vect)
{
	PORTB = data[j][i];
	if( ++i > 7 )
		i = 0;
	
	TCNT0 = 0xFF - 0x10;
}

/* for checking button pressed */
int pushed( int pin );

int main( int argc, char** argv ){

	// set portB  output
	DDRB = 0xFF;

	// set portD input
	DDRD = 0x00;

	// Initialize timer & enable timer interrupt
	TCCR0B = 0x04;
	TIMSK = 0x02;

	TCNT0 = 0xFF-0x10;

	/* enable global interrupts */
	sei();

	do {

		/* if button pusshed? then show next symbol */
		if( pushed(1) )
			if( ++j > 3 )
				j = 0;

	} while(1);

	return 0;

}

int pushed( int pin ){

	static int pressed = 1;
	static int count = 1;

	if( (PIND & (0x01<<pin)) == 0){

		// to avoid chattering
		if( pressed && ++count > THRESHOLD ){
			pressed = 0;
			
			return 1;
		}

	}
	else 
		pressed = count = 1;

	return 0;

}


