/* A simple LED driver for a "Terminator 2" carnival costume */ #include #include #include #include #include // non linear PWM table to compensate non linear eye response const uint8_t pwmtable[32] PROGMEM = { 0, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 8, 10, 11, 13, 16, 19, 23, 27, 32, 38, 45, 54, 64, 76, 91, 108, 128, 152, 181, 215, 255 }; // flicker light const uint8_t flicker_light[256] PROGMEM = { 17 , 20 , 22 , 22 , 25 , 25 , 27 , 31 , 30 , 23 , 19 , 18 , 19 , 17 , 15 , 15 , 10 , 8 , 4 , 4 , 10 , 20 , 23 , 25 , 25 , 25 , 23 , 21 , 20 , 24 , 17 , 19 , 25 , 29 , 31 , 27 , 22 , 29 , 29 , 29 , 31 , 31 , 27 , 21 , 16 , 10 , 5 , 5 , 6 , 7 , 8 , 10 , 10 , 15 , 15 , 15 , 18 , 16 , 15 , 19 , 19 , 21 , 23 , 23 , 22 , 23 , 21 , 23 , 21 , 19 , 17 , 17 , 15 , 15 , 14 , 15 , 15 , 15 , 17 , 17 , 23 , 23 , 23 , 23 , 21 , 21 , 23 , 23 , 23 , 17 , 17 , 16 , 18 , 18 , 17 , 13 , 17 , 15 , 21 , 21 , 25 , 25 , 28 , 20 , 20 , 17 , 16 , 14 , 10 , 8 , 5 , 8 , 10 , 7 , 7 , 5 , 4 , 8 , 4 , 8 , 10 , 11 , 15 , 18 , 22 , 22 , 22 , 28 , 28 , 31 , 31 , 28 , 25 , 29 , 31 , 27 , 22 , 29 , 29 , 29 , 31 , 31 , 27 , 21 , 16 , 10 , 5 , 5 , 6 , 7 , 8 , 10 , 10 , 10 , 12 , 15 , 6 , 6 , 4 , 0 , 0 , 0 , 5 , 10 , 10 , 16 , 25 , 25 , 28 , 20 , 20 , 28 , 28 , 31 , 31 , 31 , 27 , 20 , 10 , 9 , 6 , 6 , 6 , 6 , 10 , 10 , 15 , 15 , 15 , 10 , 10 , 5 , 5 , 5 , 10 , 10 , 20 , 18 , 20 , 20 , 25 , 25 , 19 , 18 , 20 , 10 , 6 , 6 , 0 , 1 , 4 , 10 , 10 , 15 , 15 , 20 , 20 , 20 , 22 , 23 , 24 , 28 , 22 , 16 , 14 , 8 , 0 , 0 , 10 , 10 , 20 , 30 , 31 , 30 , 28 , 26 , 30 , 30 , 28 , 28 , 25 , 25 , 28 , 28 , 31 , 31 , 25 , 25 , 30 , 30 , 31 , 31 , 29 , 29 , 27 , 22 } ; void delay(uint16_t ms) { while(ms--) _delay_ms(1); } // generate an new random number from min to max int rand_range(int min, int max) { int tmp; tmp = (((int32_t)rand()*(max-min+1))>>15)+min; return tmp; } void pwm_up_down (int16_t size, uint16_t time_step) { int16_t tmp; for (tmp = 2; tmp < size; tmp++) { OCR1B = pgm_read_word (&pwmtable[tmp]); delay(time_step); } for (tmp = size-1; tmp >= 2; tmp--) { OCR1B = pgm_read_word (&pwmtable[tmp]); delay(time_step); } } int main(void) { int i, j; // IO init DDRB = (1< 244 Hz TCCR1 = (1<