#include <avr/io.h>
#include  <util/delay.h>


void init(void){
	//set inputs and outputs	
	   DDRB &= ~(1<<DDB2); // PD3 as Input 
	   DDRA |= (1<<DDA7); // PA7 as Output 


	   PORTB |= (1<<PB2); // Pullup PB2
}


int main(void){

    

   //state
   int state=0; //starting with state 0 and wait for the button to be pressed
   
   int debounceTicks = 50; // delay of millisec that have to pass by before a click and it is assumed to be safe

   
   while (1) {
	//starting with state 0
	if (state ==0) { // waiting a button to be pressed
		if ((~PINB & (1 << PB2))){ //button is pressed
			_delay_ms(debounceTicks);
			state =1;
		}
	}else if(state ==1){
		_delay_ms(debounceTicks);
		if ((PINB & (1 << PB2))){ //button is not pressed
			state=2;
		}
			
	}else if(state ==2){
		click();
		state =0;	
	}
		

      
   }
}

void click(void){
	PORTA ^= (1<<PA7); // Toggle the bit at PA7,LED
}
