Skip to content

Instantly share code, notes, and snippets.

@pearlchen
Created October 19, 2013 19:43
Show Gist options
  • Save pearlchen/7060577 to your computer and use it in GitHub Desktop.
Save pearlchen/7060577 to your computer and use it in GitHub Desktop.
Use analogWrite() to fade an LED up and down.
int ledPin = 6; // Note that pin 13 on the Uno doesn't support PWM!
int brightness = 0;
int stepValue = 1;
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
analogWrite(ledPin, brightness);
delay(10);
brightness = brightness + stepValue;
if ( brightness >= 255 ) {
stepValue = -1;
}else if ( brightness <= 0 ) {
stepValue = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment