diff --git a/code/src/main.c b/code/src/main.c index 8e18d94..6e8f577 100644 --- a/code/src/main.c +++ b/code/src/main.c @@ -2,24 +2,23 @@ #include // has to be added to use uint8_t #include // Needed to use interrupts #include +#include + +uint8_t EEMEM on_off_state; +uint8_t anim_nr = 0; unsigned long NextVal(void); -unsigned long InitSeed(); +unsigned long InitSeed(void); void random(void); -void link_rechts(void); +void links_rechts(void); -int (*animation)(void) = &link_rechts; +void (*animation[4])(void); -volatile uint8_t leds = 0x00; -volatile uint8_t leds_active = 0x00; +volatile uint32_t leds = 0x00; volatile uint32_t active_count = 0; volatile uint32_t active_count_max = 13733; // 1 count = 0,065536 s -volatile uint8_t debounce_lock = 0; -volatile uint8_t debounce_count = 0; -volatile uint8_t debounce_count_max = 10; - volatile uint16_t count = 0; volatile uint16_t count_max = 3; @@ -42,89 +41,110 @@ void random(){ int temp; do{ temp=NextVal(); - temp=1 << (temp%8); + temp=1 << (temp%18); }while(temp==leds); leds=temp; } -void link_rechts(){ +void links_rechts(){ if(dir) leds = leds << 1; else leds = leds >> 1; - if(!(leds & 0x7F)){ + if(!(leds & 0x1FFFF)){ leds = 0x08; dir^=1; } } -void write_leds(){ - if(leds_active){ - PORTC = leds; - PORTD = (leds>>1)&((1<>8) & 0x3F; + PORTB = (leds>>14) & 0x07; } int main(void) { + animation[0] = links_rechts; + animation[1] = random; + + // reset switch as on/off switch + if(eeprom_read_byte(&on_off_state)){ + eeprom_write_byte(&on_off_state, 0); + } + else{ + eeprom_write_byte(&on_off_state, 1); + set_sleep_mode(SLEEP_MODE_PWR_SAVE); + sleep_mode(); + // power consumption is below 1uA + } + InitSeed(); DDRC = 0xFF; DDRD = 0xFF; + DDRB = 0x07; - DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin + //DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin // PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs - PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up + //PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up // PB0, PB1 and PB2 are now inputs with pull-up enabled - TIMSK1 |= 1 << TOIE1; + //TIMSK1 |= 1 << TOIE1; TCCR0B |= (1<=count_max){ count=0; - animation(); + (*animation[anim_nr])(); } else { @@ -132,20 +152,21 @@ ISR(TIMER0_OVF_vect){ } if(active_count >= active_count_max){ active_count=0; - leds_active = 0; + //leds_active = 0; + //TODO maybe sleep here } } -ISR(TIMER1_OVF_vect){ - if(debounce_count >= debounce_count_max) - { - TCCR1B &= ~(1<= debounce_count_max) +// { +// TCCR1B &= ~(1<