Pantry LED lights

Time to upgrade the pantry lighting!

Features: 
Motion control
Fade on and off

Parts used:
Arduino Pro Mini
Mini PID motion sensor
TIP31AG transistor

Code:

/*
 * Pantry LED Strip
 */

int ledPin = 3;                // choose the pin for the LED
int inputPin = 4;               // choose the input pin (for PIR sensor)
int val = 0;                    // variable for reading the pin status
int r, rd;
int pirState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop(){
  delay(500);
  val = LOW;
  val = digitalRead(inputPin);  // read input value

  if (val == HIGH) {

    if (pirState == LOW) {
      pirState = HIGH;
      for (r = 0; r < 256; r++) {
        analogWrite(ledPin, r);
        delay(75);
      }
    }else{
      analogWrite(ledPin, 255);
    }

  } else {

    if (pirState == HIGH){
      pirState = LOW;

      for (rd = 255; rd > 0; rd--) {
        analogWrite(ledPin, rd);
        delay(40);
      }
      analogWrite(ledPin, 0);

    }else{
      analogWrite(ledPin, 0);
    }


  }
}

Leave a comment

Your email address will not be published. Required fields are marked *