|  |  | @ -4,35 +4,24 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <avr/sleep.h> |  |  |  | #include <avr/sleep.h> | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <avr/eeprom.h> |  |  |  | #include <avr/eeprom.h> | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | // 1 count = 0,065536 s
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #define ACTIVE_COUNT_MAX 13733 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #define ANIMATION_INTERVALL 3 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | uint8_t EEMEM on_off_state; |  |  |  | uint8_t EEMEM on_off_state; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | volatile uint32_t leds = 0x00; | 
			
		
	
		
		
			
				
					
					|  |  |  | uint8_t anim_nr = 0; |  |  |  | uint8_t anim_nr = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | unsigned long NextVal(void); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | unsigned long InitSeed(void); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | void random(void); |  |  |  | void random(void); | 
			
		
	
		
		
			
				
					
					|  |  |  | void links_rechts(void); |  |  |  | void links_rechts(void); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | void (*animation[])(void) = { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     links_rechts, | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     random | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void (*animation[4])(void); |  |  |  | unsigned long NextVal(void) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint32_t leds = 0x00; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint32_t active_count = 0;      |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint32_t active_count_max = 13733; // 1 count = 0,065536 s
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint16_t count = 0; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint16_t count_max = 3; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile uint8_t dir = 0; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | static unsigned long Seed; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | unsigned long InitSeed() |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     return NextVal(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | unsigned long NextVal() |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  | { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     static unsigned long Seed; | 
			
		
	
		
		
			
				
					
					|  |  |  |     Seed=Seed*1632125L+1013904223L; |  |  |  |     Seed=Seed*1632125L+1013904223L; | 
			
		
	
		
		
			
				
					
					|  |  |  |     return Seed; |  |  |  |     return Seed; | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | @ -47,6 +36,7 @@ void random(){ | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void links_rechts(){ |  |  |  | void links_rechts(){ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     static uint8_t dir = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |     if(dir) |  |  |  |     if(dir) | 
			
		
	
		
		
			
				
					
					|  |  |  |         leds = leds << 1; |  |  |  |         leds = leds << 1; | 
			
		
	
		
		
			
				
					
					|  |  |  |     else |  |  |  |     else | 
			
		
	
	
		
		
			
				
					|  |  | @ -66,9 +56,6 @@ void write_leds(void){ | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | int main(void) |  |  |  | int main(void) | 
			
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  | { | 
			
		
	
		
		
			
				
					
					|  |  |  |     animation[0] = links_rechts; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     animation[1] = random; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     // reset switch as on/off switch
 |  |  |  |     // reset switch as on/off switch
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     if(eeprom_read_byte(&on_off_state)){ |  |  |  |     if(eeprom_read_byte(&on_off_state)){ | 
			
		
	
		
		
			
				
					
					|  |  |  |         eeprom_write_byte(&on_off_state, 0); |  |  |  |         eeprom_write_byte(&on_off_state, 0); | 
			
		
	
	
		
		
			
				
					|  |  | @ -80,34 +67,24 @@ int main(void) | 
			
		
	
		
		
			
				
					
					|  |  |  |         // power consumption is below 1uA
 |  |  |  |         // power consumption is below 1uA
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     InitSeed(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     DDRC = 0xFF; |  |  |  |     DDRC = 0xFF; | 
			
		
	
		
		
			
				
					
					|  |  |  |     DDRD = 0xFF; |  |  |  |     DDRD = 0xFF; | 
			
		
	
		
		
			
				
					
					|  |  |  |     DDRB = 0x07; |  |  |  |     DDRB = 0x07; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     //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
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     // PB0, PB1 and PB2 are now inputs with pull-up enabled
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     //TIMSK1 |= 1 << TOIE1;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     TCCR0B |= (1<<CS01)|(1<<CS00);  |  |  |  |     TCCR0B |= (1<<CS01)|(1<<CS00);  | 
			
		
	
		
		
			
				
					
					|  |  |  |     TIMSK0 |= 1 << TOIE0; |  |  |  |     TIMSK0 |= 1 << TOIE0; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     //PCICR |= (1 << PCIE0);     // set PCIE0 to enable PCMSK0 scan
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     //PCMSK0 |= (1 << PCINT0);   // set PCINT0 to trigger an interrupt on state change
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |      |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     ADCSRA |= (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); |  |  |  |     ADCSRA |= (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); | 
			
		
	
		
		
			
				
					
					|  |  |  | 	ADMUX  = 1<<REFS0; |  |  |  | 	ADMUX  = 1<<REFS0; | 
			
		
	
		
		
			
				
					
					|  |  |  | 	ADMUX  |= 6; |  |  |  | 	ADMUX  |= 6; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     sei();                     // turn on interrupts
 |  |  |  |     sei(); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     while(1) |  |  |  |     while(1) | 
			
		
	
		
		
			
				
					
					|  |  |  |     { |  |  |  |     { | 
			
		
	
		
		
			
				
					
					|  |  |  |         write_leds(); |  |  |  |         write_leds(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         //check buttons on ADC6 and ADC7
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         ADCSRA |= 1<<ADSC; |  |  |  |         ADCSRA |= 1<<ADSC; | 
			
		
	
		
		
			
				
					
					|  |  |  |         while(ADCSRA & (1<<ADSC)); |  |  |  |         while(ADCSRA & (1<<ADSC)); | 
			
		
	
		
		
			
				
					
					|  |  |  |         if(ADC < 512){ |  |  |  |         if(ADC < 512){ | 
			
		
	
	
		
		
			
				
					|  |  | @ -115,34 +92,18 @@ int main(void) | 
			
		
	
		
		
			
				
					
					|  |  |  |                 anim_nr = 0; |  |  |  |                 anim_nr = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |             else |  |  |  |             else | 
			
		
	
		
		
			
				
					
					|  |  |  |                 anim_nr = 1; |  |  |  |                 anim_nr = 1; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             anim_nr%=sizeof(animation); | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |         ADMUX ^= 0x01; |  |  |  |         ADMUX ^= 0x01; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //ISR (PCINT0_vect)
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //{
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    if(!debounce_lock){
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        TCCR1B |= (1<<CS00); 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        debounce_lock=1;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        if((~PINB & 0x01)) 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //            leds_active ^= 0x01;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //            active_count=0;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //            if(!leds_active){
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //                write_leds();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //                TCCR0B &= ~((1<<CS01)|(1<<CS00)); 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //                set_sleep_mode(SLEEP_MODE_PWR_SAVE);
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //                sleep_mode();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //                TCCR0B |= (1<<CS01)|(1<<CS00); 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //            }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //}
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | ISR(TIMER0_OVF_vect){ |  |  |  | ISR(TIMER0_OVF_vect){ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     static uint32_t active_count = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     static uint16_t count = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     active_count++; |  |  |  |     active_count++; | 
			
		
	
		
		
			
				
					
					|  |  |  |     if(count>=count_max){ |  |  |  |     if(count>=ANIMATION_INTERVALL){ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         count=0; |  |  |  |         count=0; | 
			
		
	
		
		
			
				
					
					|  |  |  |         (*animation[anim_nr])(); |  |  |  |         (*animation[anim_nr])(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | @ -150,23 +111,9 @@ ISR(TIMER0_OVF_vect){ | 
			
		
	
		
		
			
				
					
					|  |  |  |     { |  |  |  |     { | 
			
		
	
		
		
			
				
					
					|  |  |  |         count++; |  |  |  |         count++; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |     if(active_count >= active_count_max){ |  |  |  |     if(active_count >= ACTIVE_COUNT_MAX){ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         active_count=0; |  |  |  |         active_count=0; | 
			
		
	
		
		
			
				
					
					|  |  |  |         //leds_active = 0;
 |  |  |  |         //leds_active = 0;
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         //TODO maybe sleep here
 |  |  |  |         //TODO maybe sleep here
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //ISR(TIMER1_OVF_vect){
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    if(debounce_count >= debounce_count_max)
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        TCCR1B &= ~(1<<CS00); 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        debounce_lock=0;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        debounce_count=0;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    else
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //        debounce_count++;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //    }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //}
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
	
		
		
			
				
					|  |  | 
 |