/** * * Blinking LED ATTiny85 "hello world" * * Following the tutorial at: * http://www.instructables.com/id/Honey-I-Shrunk-the-Arduino-Moving-from-Arduino-t/?ALLSTEPS * */ #include // F_CPU frequency to be defined at command line #include #define TRUE 1 #define FALSE 0 #define LED0 PB3 #define LED1 PB2 #define LED2 PB1 #define LED3 PB0 #define LED4 PB4 const int leds[] = {LED0, LED1, LED2, LED3, LED4}; #define LED_COUNT (sizeof(leds) / sizeof(leds[0])) uint8_t led_states[LED_COUNT]; #define DELAY_NORMAL 100; #define DELAY_SLOW 150; #define DELAY_FAST 50; const int DELAY = DELAY_NORMAL; void led_on(int led_index) { PORTB |= (1 << leds[led_index]); led_states[led_index] = 1; } void led_off(int led_index) { PORTB &= ~(1 << leds[led_index]); led_states[led_index] = 0; } void setup_leds() { for (int i = 0; i != LED_COUNT; ++i) { DDRB |= (1 << leds[i]); led_off(i); led_states[i] = 0; } } void accumulate_next(int led_index) { if(led_states[led_index]) { led_off(led_index); } else { led_on(led_index); } _delay_ms(DELAY); } void accumulate() { for (int i = 0; i != LED_COUNT; ++i) accumulate_next(i); } void accumulate_backwards() { for (int i = LED_COUNT - 1; i >= 0; --i) accumulate_next(i); } void row_next(int led_index, int previous_led_index) { led_on(led_index); led_off(previous_led_index); _delay_ms(DELAY); } void row() { for (int i = 0; i != LED_COUNT; ++i) { int previous = (i + LED_COUNT - 1) % LED_COUNT; row_next(i, previous); } } void row_backwards() { for (int i = LED_COUNT - 1; i >= 0; --i) { int previous = (i + 1) % LED_COUNT; row_next(i, previous); } } void all_on() { for (int i = 0; i != LED_COUNT; ++i) led_on(i); } void toggle_all() { for (int i = 0; i != LED_COUNT; ++i) { if(led_states[i]) led_off(i); else led_on(i); } } void blink(int ntimes) { setup_leds(); for (int i = 0; i != ntimes * 2; ++i) { toggle_all(); _delay_ms(DELAY); } } int main () { setup_leds(); while (1) { accumulate(); accumulate_backwards(); accumulate(); accumulate_backwards(); row(); row_backwards(); row(); row_backwards(); blink(5); } return 0; }