#include int main(void) { DDRB &= ~(1 << DDB0); //Configures B0 as input PORTB |= _BV(PORTB0); //Enables the pull-up resistor which prevents the pin from being in a floating state DDRB |= (1 << DDB2); //Configures B2 as output while (1) { static byte isOn = 0; if ((PINB & (1 << PINB0)) == 0) { /* Since we have used a pull-up resistor, the reading will be '1' when the button is not pressed, and '0' when it is */ delay(100); if ((PINB & (1 << PINB0)) == 0) { //'Debouncing' - we want to ensure that the button has actually been pushed for a certain amount of time (100 ms in this case). isOn = !isOn; if (isOn) { PORTB |= (1 << PORTB2); //Turn on B2 } else { PORTB &= ~(1 << PORTB2); } } while ((PINB & (1 << PINB0)) == 0); //Do nothing as long as button is pressed } } }