// // // hello.arduino.44.blink.button.c // // try to make an LED that blinks and interacts with a button // // original blink code Neil Gershenfeld // 3/14/11 // // (c) Massachusetts Institute of Technology 2011 // Permission granted for experimental and personal use; // license for commercial sale available from MIT. // //Edits Perovich Oct 2012; change board to ATtiny44, change pins etc #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin (turn on) #define clear(port,pin) (port &= (~pin)) // clear port pin (turn off) #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define led_delay() _delay_ms(300) // LED delay (changed from 100) //LED defines: mine is port A (not B) for everything, because PA pin #define led_port PORTA #define led_direction DDRA #define led_pin (1 << PA2) //Button defines: mine is B (not A) for everything, because PB pin #define button_port PORTB #define button_pin (1 << PB2) int main(void) { // // main // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize LED pin // clear(led_port, led_pin); output(led_direction, led_pin); //Turn on pullup resistor on port B (so not floating) PORTB = _BV(PB2); while(1) { //PINB is port B input pins reading //if evaluates to TRUE if nonzero //If the button is not pressed, do nothing... if(pin_test(PINB, button_pin)){ clear(led_port, led_pin); } //If pin_test evaluates to false... //If the button is pressed (and held down) blink the LED until it is released else{ set(led_port, led_pin); led_delay(); clear(led_port, led_pin); led_delay(); } } }