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

#define SET(REGISTER, BIT)			REGISTER |= 1 << BIT
#define CLEAR(REGISTER, BIT)		REGISTER &= ~(1 << BIT)
#define TEST(REGISTER, BIT)			REGISTER & 1 << BIT
#define TEST_PIN(PORT, BIT)			PIN ## PORT & 1 << BIT
#define TOGGLE(REGISTER, BIT)		REGISTER ^= 1 << BIT
#define TOGGLE_PIN(PORT, BIT)		PIN ## PORT |= 1 << BIT
#define MODE_INPUT(PORT, BIT)		CLEAR(DDR ## PORT, BIT)
#define MODE_OUTPUT(PORT, BIT)		SET(DDR ## PORT, BIT)
#define IS_PIN_SET(PORT, BIT)		(TEST_PIN(PORT, BIT)) == 0
#define IS_PIN_CLEAR(PORT, BIT)		(TEST_PIN(PORT, BIT)) == 1

#define D1 0
#define D2 1
#define D3 2
#define D4 4
#define SW1 2
#define SW2 4

int main() {
	MODE_OUTPUT(B, D1);
	MODE_INPUT(D, SW1);
	while(1) {
		if (IS_PIN_SET(D, SW1)) {
			TOGGLE_PIN(B, D1);
			_delay_ms(20);
			while(IS_PIN_SET(D, SW1)) {
				continue;
			}
			_delay_ms(20);
 		}
	}
}