Arduino101 LEDFader

From Twin Cities Maker wiki
Jump to: navigation, search

Shows the most basic use of the PWM pins – make lights brighter and dimmer. Also introduces the for loop.

Breadboard layout

LED Fader bb.png

Code

int ledPin = 11;
int inc = 1;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  for (int i=0; i>-1; i=i+inc){
    analogWrite(ledPin, i);
    if (i==255){
    	inc = -inc;
    }
    if (i==0){
    	inc = -inc;
    }
    delay(10);
  }
}