#define F_CPU 7900000 // clock frequency, set according to clock used!

#include <inttypes.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define HPERIOD 0.01524
#define RATIO 0.4
#define NPULSES 16

#define LEDOFF 0b00000001
#define LEDON  0b00010111


int main(void)
{
	uint8_t i;

	DDRB  = 0b00010110; // pin PB0 is input, pins PB1-PB4 are output
	PORTB = 0b00000001; // pull-up for input pin PB0
	asm volatile ("nop");
	asm volatile ("nop");

	if ( PINB & (1<<PINB0) ) 
	{

	    for(i=0;i<NPULSES;i++)
		{
			PORTB = LEDON;
			_delay_ms(HPERIOD);
			PORTB = LEDOFF;
			_delay_ms(HPERIOD);
		}

		_delay_ms(7.33); // instant

		for(i=0;i<NPULSES;i++)
		{
			PORTB = LEDON;
			_delay_ms(HPERIOD);
			PORTB = LEDOFF;
			_delay_ms(HPERIOD);
		}	
	}
	else
	{
	    for(i=0;i<NPULSES;i++)
		{
			PORTB = LEDON;
			_delay_ms(HPERIOD);
			PORTB = LEDOFF;
			_delay_ms(HPERIOD);
		}

		_delay_ms(5.36); // delayed

		for(i=0;i<NPULSES;i++)
		{
			PORTB = LEDON;
			_delay_ms(HPERIOD);
			PORTB = LEDOFF;
			_delay_ms(HPERIOD);
		}

	}

	set_sleep_mode(SLEEP_MODE_PWR_DOWN);
	sleep_mode();

}
