#include <avr/io.h>

//time delay program,
//PORD D, eight dip switches, goes from 0 to 255T, defines time off
//set in secDelay, 1300 gives approximately 5 seconds maximum
//                 2600 gives approximately 10 seconds
//PORT B: six of the inputs are used, typically with 5 position dip switch
//since 6p dip switches are less common.
//on PORT B dip switch, all switches up (open) gives infinity time on.


// +5V bus                       LED:red
// PB6 = Q   goes from 0-->5 V   LED:blue
// PB7 = ~Q  goes from 5-->0 V   LED:green
//                              Normal tri-colour LED sequence: yellow-->purple

void secDelay(int count){
	int i;
	for(;count>0;count--){
//		for(i=1000; i>0;i--){
		for(i=2600; i>0;i--){
			i--;
		}	
	}
}

int main(){
	PORTB	= 0x3F;
	DDRB	= 0xC0;

	PORTD	= 0xFF;
	DDRD	= 0x00;

	/*PORTB	= 0xC0;
	PORTD	= 0x00;*/

	int timeAfter = PINB;
	timeAfter &= 0x3F;

	int timeBefore = PIND;
	// no need to mask since it uses all pins
	
	PORTB = 0x80;
	secDelay(timeBefore*5);
        PORTB = 0x40;

	// BY SIMON: June 5th
	// Commented out lines below for the latest revisions
	// of the relay... it should never shut off.
	if (timeAfter != 0x3F) {
        	secDelay(timeAfter*5);
		PORTB = 0x80; 
	}
        //PORTB = 0x80;

	return 0;
}

