Arduino Project – LED Skull Eyes 2: Wink Effect

Oct 13, 2015

 

More variables and the addition of a "wink" effect to make the LED skull eyes seem more animated.

 

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.



Tags: Arduino

Related Content

Arduino Project – LED Skull 5: Adding Subroutines/Functions

This code was a great opportunity to introduce the idea of subroutines and returning values from subroutines (functions) with an Arduino Uno.

Fire Breathing Arduino Pumpkin With IR Remote Control

A small change to another fire-breathing Arduino pumpkin replaces a proximity sensor with an small IR remote control.

Arduino Project – LED Skull 4: Add HC-SR04 Ultrasonic Module

The next step in creating a spookier skull was to add an HC-SR04 ultrasonic module that would fire the LEDs based on proximity to a viewer.

Arduino Project – LED Skull Eyes 3: Adding a Second Eye

The next step involves adding a second "eye" (LED), independent of the first, to maintain the wink effect.

Arduino Project – LED Skull Eyes 1

The first part of this project (adding LED "eyes" to a skull, driven by an Arduino UNO) was about teaching code optimization, abstraction and commenting.

Arduino IDE "'lt' was not declared in this scope" Error

Be aware of a small encoding issue and the resulting "lt/gt was not declared in this scope" error when pasting into the excellent Arduino IDE for Mac.

Category List


Tag List


Tag Cloud



Archive