This pair of variations on Blink will continue to exercise the solo LED and illuminate some principles of Pulse Width Modulation.

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough, the result seems as if the signal is steady at value between on or off.

In Dissolve, as in Blink, the LED is on half the time and off half the time. This is a 50% "duty cycle," the percentage of time the signal is on. The average value is thus 50% of the full voltage (2.5 volts on the Arduino). The difference between Blink and Dissolve is that Dissolve increases the on-off speed (the PWM frequency) as it runs. What you will notice is that eventually, roughly around when the LED is on for 20ms and off for 20ms, you will stop seeing it blink, and it will appear to be steady at about half brightness ("about" half because perceived brightness is another story all together). To see that it is still blinking, shake your Arduino and watch the light.



/*
 * Dissolve
 *
 * A variation on Blink, the the basic Arduino example.  Turns on an LED on for one second,  then off for one second, then on for a little less than a second, then off for a little less, and then speeds it up a little faster each time...  
 * We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */


int ledPin = 13;                // LED connected to digital pin 13

int delayTime = 1000;          // starts just like Blink does, but this will decrease over time

float multiplyByFactor = 0.95;   // each cycle, multiply delay time by this factor

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(delayTime);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(delayTime);                  // waits for a second

  // For each blink cycle, mutiple delay by factor:  1000, 950, 902, 856, 813, 772, ...
  // until delayTime is just one millisecond  
  if (delayTime > 1) {
    delayTime= delayTime * multiplyByFactor;
  }
}
 

Throb takes the PWM principle and uses it to create a throbbing LED that slowly increases and decreases in brightness. Unlike Dissolve, where the duty-cycle never changes but PWM frequency increases, Throb changes the duty-cycle but maintains a constant PWM frequency. Each 30 ms cycle (variable: int cycleTime) is shared between on and off. First the LED is turned on for 0 ms, and then off for 30 ms. Next, it's on of 1 ms, and off for 29. Then on for 2 ms and off 28 ms and so on. This repeats until the LED is on for all 30 ms. At this point the process is sent in the other direction and Throb starts decreasing the "on time" by 1 ms each cycle. In other worse, it's decreasing the duty-cycle or narrowing the pulse-width. As you will see, this process continues back and forth. The resulting effect is the throbbing LED. For a fancier effect, think about ways that the rate of change (variable: int changeSpeed) can differ over the trip between fully on and fully off. For example, slowing down or speeding up that change as it approaches maximum brightness.

/*
 * Throb
 *
 * A variation on Blink.  This sketch presents the concept of PWM or
 * Pulse Width Modulation. It "throbs" the brightness of an LED, making it
 * gradually brighter and dimmer.
 *
 * From Blink: We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 */


int ledPin = 13;                // LED connected to digital pin 13

// once this is working, reprogram with a cycle time of 100 to see more clearly how the throbbing works
int cycleTime = 30;             // the total duration of a PWM cycle

int changeSpeed = 1;            // throb speed, or the number of milliseconds to change the onTimePerCycle per Cycle

boolean brightnessGoingUp = true;     // whether the brightness is going up or down
int onTimePerCycle = 0;      // the number of milliseconds to turn the LED within a cycle

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{

  digitalWrite(ledPin, HIGH);        // sets the LED on
  delay(onTimePerCycle);             // wait for onTimePerCycle milliseconds
  digitalWrite(ledPin, LOW);         // sets the LED off
  delay(cycleTime - onTimePerCycle); // waitfor the rest of the complete cycle              

  // Now adjust the brightness up or down depending the the current state
  if (brightnessGoingUp) {  
    onTimePerCycle += changeSpeed;
  }
  else {
    onTimePerCycle -= changeSpeed;
  }  

  // if we are at full brightness, where the LED is on for the complete cycle
  if (onTimePerCycle >= cycleTime) {
    brightnessGoingUp = false;       // switch mode to brightness going down
    onTimePerCycle = cycleTime;      // make sure brightness did not go over
  }
  // if we are at zero brightness, where the LED is not on at all in a cycle
  if (onTimePerCycle <= 0) {        
    brightnessGoingUp = true;      // switch mode to brightness going up
    onTimePerCycle = 0;            // make sure the onTime didn't go bellow zero        
  }

}
 

Share