#include #include #include unsigned char values[14] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; unsigned int index = 0; enum { DEMO, COMET, THROB, TWINKLE, STAR, TRAIN, BOUNCE, RANDOM_WALK, DAYLIGHT } mode; enum { WD_8S = (1< 100 ) increment = -1; else if( brightness <= 0 ) increment = 1; brightness += increment; analogWrite( pin, brightness ); } void stop( void ) { analogWrite( pin, 0 ); } }; Throb *a, *b, *c, *d, *e, *f; long int itrain = 0b11111; int train_length = 5; int how_many_pd_lights = 8; int how_many_pb_lights = 6; int total_lights = 8 + 6; int x = 0; void setup() { MCUSR &= ~(1<> 8) & 0b11111; } values[index]--; values[index++] &= 0xF; index %= 14; } // // Throb is a class that brightens and dims an LED in a cycle, so it appears to throb. // The constructor takes an initial brightness, and a pin number as arguments. // Calling the method change() causes the brightness to change. // // By starting each LED off with a different brightness, they will change out of sync // with one another. By calling the change() method a different number of times for each LED, // they will throb at different rates. // // Only six LEDs can be controlled: PD3, PD5, PD6, PB1, PB2, and PB3. // void throb( void ) { delay( 20 ); for( int i = 0, imax = random(8); i < imax; i++ ) a->change(); for( int i = 0, imax = random(8); i < imax; i++ ) b->change(); for( int i = 0, imax = random(8); i < imax; i++ ) c->change(); for( int i = 0, imax = random(8); i < imax; i++ ) d->change(); for( int i = 0, imax = random(8); i < imax; i++ ) e->change(); for( int i = 0, imax = random(8); i < imax; i++ ) f->change(); } // // Star lights one LED at a time, in a series of 11 LEDs // void star( void ) { int lights = 1 << (1 + ++x); PORTD = lights & 0b11111100; PORTB = (lights >> 8) & 0b11111; x %= 10; power_down( WD_60MS ); } // // Train moves five lights at a time from left to right, like a train. // void train( void ) { itrain <<= 1; itrain = (itrain | (itrain >> total_lights)) & 0xFFFFFFL; PORTD = (itrain >> train_length) & 0b11111100; PORTB = (itrain >> (train_length + how_many_pd_lights)) & 0b11111; power_down( WD_60MS ); } // // This program lights up one LED after another, and then it reverses. // It looks like the light is bouncing from one side to the other. // void bounce( void ) { static unsigned long int lights = 1; static bool go_left = true; if( lights & 4 ) go_left = true; else if( lights & (1<<12) ) go_left = false; wdt_reset(); if( go_left ) lights <<= 1; else lights >>= 1; PORTD = lights & 0b11111100; PORTB = (lights >> 8) & 0b11111; power_down( WD_60MS ); } // // Random walk makes a light appear to walk randomly around in a string // of 11 LEDs. // void random_walk( void ) { static unsigned int lights = 1 << 9; static bool go_left = true; if( random( 100 ) > 50 ) go_left = true; else go_left = false; if( lights & 4 ) go_left = true; else if( lights & (1<<13) ) go_left = false; if( go_left ) lights <<= 1; else lights >>= 1; PORTD = lights & 0b11111100; PORTB = (lights >> 8) & 0b11111; power_down( WD_60MS ); } void twinkle( void ) { int lights = random( 65535 ); PORTD = lights & 0b11111100; PORTB = (lights >> 8) & 0b11111; power_down( WD_60MS ); } void demo( void ) { PORTD = 0; PORTB = 0; for( int i = 0; i < 1000 && getMode() == 0; i++ ) comet(); PORTD = 0; PORTB = 0; for( int i = 0; i < 200 && getMode() == 0; i++ ) throb(); a->stop(); b->stop(); c->stop(); d->stop(); e->stop(); f->stop(); PORTD = 0; PORTB = 0; for( int i = 0; i < 100 && getMode() == 0; i++ ) star(); PORTD = 0; PORTB = 0; for( int i = 0; i < 206 && getMode() == 0; i++ ) train(); PORTD = 0; PORTB = 0; for( int i = 0; i < 200 && getMode() == 0; i++ ) bounce(); PORTD = 0; PORTB = 0; for( int i = 0; i < 200 && getMode() == 0; i++ ) twinkle(); PORTD = 0; PORTB = 0; for( int i = 0; i < 200 && getMode() == 0; i++ ) random_walk(); } void daylight( void ) { PORTD = 0; PORTB = 0; power_down( WD_8S ); } void loop( void ) { switch( getMode() ) { case DEMO: demo(); break; case COMET: comet(); break; case THROB: throb(); break; case TWINKLE: twinkle(); break; case STAR: star(); break; case TRAIN: train(); break; case BOUNCE: bounce(); break; case RANDOM_WALK: random_walk(); break; case DAYLIGHT: daylight(); break; } }