#define controlPin 2
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by . will be slowed by a delay in the loop.
int val = 0;
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(controlPin, INPUT);
}
void loop() {
val = digitalRead(controlPin); // read the input pin
if (val == HIGH) {
analogWrite(led, brightness);// set the brightness of pin 9:
brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
delay(100);// slows the fade time }
if (brightness >= 255) { // keeps the LEDS bright once they reach max brigthness
brightness = 255;
}
}
if (val == LOW) {
analogWrite(led, brightness);// set the brightness of pin 9:
brightness = brightness - fadeAmount; // change the brightness for next time through the loop:
delay(100);// slows the fade time }
if (brightness <= 0) {
brightness = 0;
}
}
}