Arduino Project – LED Skull Eyes 1

Oct 12, 2015

 

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.

 

As a starting point we used the code from a Sunfounder project, a very simple one that fades a single LED in and out.

arduino-led-fade-001.jpg

Copying the code from the Sunfounder website and pasting it into the Arduino IDE for Mac caused an encoding error that broke the code – the greater than (>) and less than (<) symbols were replaced with html equivalents. Once that was fixed the code worked fine:

 

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()
{
  for (int a = 0; a <= 255; a++) //loop from 0 to 255
  {
    analogWrite(ledPin, a); // set the brightness of pin 9:
    delay(8); //wait for 8 microseconds
  }
  for (int a = 255; a >= 0; a--) //loop from 255 down to 0
  {
    analogWrite(ledPin, a); // set the brightness of pin 9:
    delay(8); //wait for 8 microseconds
  }
  delay(800); //wait for 800 microseconds
}

 

The goal here was to teach my kids something about finding code that acts a a good starting point, and modifying it to suit your needs. A first step in that is cleaning it up and optimizing it before making any big changes.

Right away there was something in this code that could be improved. There are both minimum (0) and maximum (255) brightness level values for the LED. Since a big part of this project will be about tweaking those values to get the best effect, it makes sense to declare them as constants up at the top so they can be adjusted easily.

Naming convention... almost all of my life is spent managing the ongoing development of a pretty huge solution. It's far more complex than any of us that work on it can keep in our heads, so I'm a huge fan of highly descriptive names. Yes – it's easier to use a, b, c, x, y, etc., but when you are down on line 500 it can tough to remember the comment on line three that explained what c was. Devising elaborate short names can be a fun challenge, they're rarely easier to remember than single letters when you revisit them months later.

So with that in mind the constants we added were called brightness_minimum and brightness_maximum. We also replaced the variable "a" in the source code with brightness_current. Now, after some adjustments to the comments, the code looks like this, verifies, and produces the same result as the source file.

 

// BRIGHTNESS
// minimum brightness level for the led
int brightness_minimum = 0;
// maximum brightness level for the led
int brightness_maximum = 255;
 
 
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()
{
  //loop from minimum brightness up to max
  for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++)
  {
    analogWrite(ledPin, brightness_current); // set the brightness of pin 9:
    delay(8);
  }
  //loop from maximum brightness down to min
  for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--)
  {
    analogWrite(ledPin, brightness_current); // set the brightness of pin 9:
    delay(8);
  }
  delay(800);
}

 

Next up are the pauses. These two will be adjusted quite a bit as we try and find the values that provide the best effect, so it makes sense to make them constants that are declared up at the top. Again we use very descriptive names that leave no room for confusion, and also the exact same numerical values. We don't to make changes to the output just yet, we're just moving the code around. Introducing too many changes at once makes debugging more complicated.

Again the code verifies fine and produces the same effect with the LED.

 

// BRIGHTNESS
// minimum brightness level for the led
int brightness_minimum = 0;
// maximum brightness level for the led
int brightness_maximum = 255;
 
// PAUSES
// the pause between each increase in brightness
int pause_between_increase = 8;
// the pause between each decrease in brightness
int pause_between_decrease = 8;
// the pause between cycles
int pause_between_cycles = 800;
 
 
 
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()
{
  //loop from minimum brightness up to max
  for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++)
  {
    analogWrite(ledPin, brightness_current); // set the brightness of pin 9:
    delay(pause_between_increase);
  }
  //loop from maximum brightness down to min
  for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--)
  {
    analogWrite(ledPin, brightness_current); // set the brightness of pin 9:
    delay(pause_between_decrease);
  }
  delay(pause_between_cycles);
}

 

Now we have code that is, hopefully, more readable, and the kids can easily adjust all parameters of the effect – the minimum and maximum brightness, the pauses, etc. The next thing may be to have different pause value between cycles – one value for the pause at maximum brightness, another at minimum brightness.

arduino-led-fade-002.jpg

 

 



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

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

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