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

Oct 21, 2015

 

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

 

So far the kids have wired up one LED, and modified a basic fade in/out program. The modifications included making the code more readable, adding some pauses and creating a wink effect. Also – moving all the pause and brightness values up into let statements at the top of the code to make fine-tuning them easier.

But, of course, a skull has two eyes, and we only had one LED. They had the option of taking the laze way out – we talked about putting a pirate-style patch over one eye. But they decided they wanted to see it through and add a second LED, and change the code so that only one of them "winked".

The added a second LED to pin 10, and altered the code accordingly – declaring the new pin at the outset, and setting the brightness of each led at each step along the way. Except of course during the wink effect, where only one led (the one winking) is turned down to zero for the wink interval.

After the changes and testing the revised code looked 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 = 100;
// 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 = 600;
 
 
 
const int ledPin1 = 9; // the pin that the LED is attached to
const int ledPin2 = 10; // the pin that the LED is attached to
void setup ()
{
  pinMode(ledPin1, OUTPUT); // declare pin 9 to be an output:
  pinMode(ledPin2, OUTPUT); // declare pin 10 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(ledPin1, brightness_current); // set the brightness of pin 9:
    analogWrite(ledPin2, brightness_current); // set the brightness of pin 10:
    delay(pause_between_increase);
  }
 
  // hold at max brightness before wink
  delay(pause_at_max_before_wink);
 
  // wink (optional)
  if (pause_for_wink > 0) {
    analogWrite(ledPin1, 0); // turn first led off, creating the wink effect:
    delay(pause_for_wink);
    analogWrite(ledPin1, brightness_maximum); // restore first 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(ledPin1, brightness_current); // set the brightness of pin 9:
    analogWrite(ledPin2, brightness_current); // set the brightness of pin 10:
    delay(pause_between_decrease);
  }
 
  // hold at minimum brightness
  delay(pause_at_min_brightness);
 
}

 

 

It looks good. The next step will be moving the wiring off the breadboard and mounting it into the skull.



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 2: Wink Effect

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

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