// // 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 three LEDs can be controlled: those on pin 4 (P1.2), pin 9 (P2.1), and pin 12 (P2.4). // class Throb { int brightness; int pin; int increment; public: Throb( int b, int p ) { brightness = b; pin = p; increment = 1; pinMode( pin, OUTPUT ); } void change( void ) { if( brightness > 100 ) increment = -1; else if( brightness < 0 ) increment = 1; brightness += increment; analogWrite( pin, brightness ); } }; Throb *a, *b, *c; void setup() { a = new Throb( 5, P1_2 ); b = new Throb( 16, P2_1 ); c = new Throb( 64, P2_4 ); } void loop() { a->change(); b->change(); b->change(); c->change(); c->change(); c->change(); delay( 20 ); }