Dimming LEDs
This program introduces a way to dim and brighten LEDs using a new command called analogWrite. analogWrite is a way of generating pseudo-analog voltages on a digital pin by using Pulse Width Modulation. PWM is in the simpliest of terms just turning something on and off so fast that it appears to dim or brighten.
NOTE: This only works on those pins marked PWM (11, 10, 9, 6, 5, 3).
/* PWM Output
Using PWM to dim and brighten an LED
PG35 Programming Notebook
*/
int ledPin = 9; // PWM pin for the LED
void setup(){} // no setup needed
void loop()
{
for (int i=0; i<=255; i++) // ascending value for i
{
analogWrite(ledPin, i); // sets brightess level to i
delay(100); // pauses for 100ms
}
for (int i=255; i>=0; i--) // descending value for i
{
analogWrite(ledPin, i); // sets brightess level to i
delay(100); // pauses for 100ms
}
}
Comments (0)
You don't have permission to comment on this page.