As the kids started playing with the brightness and pause values that were discussed in part 1 it became obvious that more control over things would make the project more fun and the animation more realistic.
The first change was to add an additional pause at maximum LED brightness. The idea is that the brightness will ramp up relatively quickly, pause at that peak, then ramp down more slowly, with a longer pause at minimum brightness. The original code had just one pause, at minimum brightness, so we added a second (and renamed some constants) like this:
// BRIGHTNESS // minimum brightness level for the led int brightness_minimum = 1; // maximum brightness level for the led int brightness_maximum = 75; // PAUSES // pause between each increase in brightness int pause_between_increase = 12; // pause between each decrease in brightness int pause_between_decrease = 36; // pause at max brightness int pause_at_max_brightness = 2000; // pause at min brightness int pause_at_min_brightness = 5000; const int ledPin = 9; // the pin that the LED is attached to void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output: } void loop() { // ramp up led brightness for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++) //loop from brightness_minimum to brightness_maximum { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_increase); } // hold at max brightness delay(pause_at_max_brightness); // ramp down led brightness for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--) //loop from brightness_maximum down to brightness_minimum { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_decrease); } // hold at minimum brightness delay(pause_at_min_brightness); }
This got us close to the desired effect – a rapid ramp up in brightness, a long pause at that level, then a slower dimming and longer dormant period. The effect seemed creepier when the LED did not dim to zero, but instead just barely stayed on at 1.
It then seemed like it would be fun to add a "wink" effect. Again the brightness would ramp up quickly, pause at maximum brightness, then wink by setting the brightness to zero for a very brief time. After that it would dim down again.
This was done by adding some more constants, and an if statement – if no blink value is specified there is no reason to turn off the LED.
The revised code looks like this:
// BRIGHTNESS // minimum brightness level for the led int brightness_minimum = 1; // maximum brightness level for the led int brightness_maximum = 75; // PAUSES // pause between each increase in brightness int pause_between_increase = 12; // pause between each decrease in brightness int pause_between_decrease = 64; // pause at max brightness, before the wink effect int pause_at_max_before_wink = 2000; // pause for the wink effect int pause_for_wink = 50; // pause at max brightness, after the wink effect int pause_at_max_after_wink = 200; // the pause at min brightness int pause_at_min_brightness = 500; const int ledPin = 9; // the pin that the LED is attached to void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output: } void loop() { // ramp up led brightness for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++) //loop from brightness_minimum to brightness_maximum { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_increase); } // hold at max brightness before wink delay(pause_at_max_before_wink); // wink (optional) if (pause_for_wink > 0) { analogWrite(ledPin, 0); // turn led off, creating the wink effect: delay(pause_for_wink); analogWrite(ledPin, brightness_maximum); // restore led to max brightness, ending the wink effect: } // hold at max brightness after wink delay(pause_at_max_after_wink); // ramp down led brightness for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--) //loop from brightness_maximum down to brightness_minimum { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_decrease); } // hold at minimum brightness delay(pause_at_min_brightness); }
The kids will continue to play with the values but the effect seems pretty good so far, with just some fine tuning to be done, and a second non-winking eye to be added.