Go Down

Topic: using PIR Sensor to fading LED (Read 2448 times) previous topic - next topic

arduinoOK

when the PIR throws HIGH the LED strobes.  When the PIR throws LOW it fades while strobing repeatedly

PaulS

Quote
when the PIR throws HIGH the LED strobes.  When the PIR throws LOW it fades while strobing repeatedly

Not sure what you mean by "strobes". If the fading is not smooth, there is a problem with your LED or how it is wired.

arduinoOK

I messed with my hardware a bit and It was my LED going bad... Now the LED stays solid when the PIR throws HIGH and fades (as it should) but now it cycles through the fade when the PIR throws LOW

PaulS

Quote
Now the LED stays solid when the PIR throws HIGH and fades (as it should) but now it cycles through the fade when the PIR throws LOW

Have you changed the code so that firstLEDvalue has different values when the PIR throws HIGH and when it throws LOW? If not, then the behavior you are seeing is expected.

arduinoOK

Thanks for the help so far but I still haven't found a solution to this...

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

arduinoOK

I finally figured it out!  XD

Code: [Select]
const int firstLED = 3;      // pin for the LED
const int inputPin = 2;      // input pin for the PIR sensor
int PIRstate;               // variable to hold the last PIR state
int val;                    // variable for reading the pin status

void setup() {

  pinMode(inputPin, INPUT);              //declare PIR as input
  PIRstate = digitalRead(inputPin);      //assign PIR state to PIRstate
}

void loop() {

  val = digitalRead(inputPin);      // read input value
  if (val != PIRstate)                      // check if the input has changed
  {
    if(val == HIGH){ 
      analogWrite(firstLED, 255);
    }
    else{

      for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
        // sets the value (range from 0 to 255):
        analogWrite(firstLED, fadeValue);         
        // wait for 30 milliseconds to see the dimming effect   
        delay(30);                           
      }
    }
  }
  PIRstate = val;                 // save the new state to the variable
}





DDRYA

This post was super helpful.. however the only problem I am encountering (which im assuming is going to be a 'duh' moment when someone responds) but how do i set up my PIR? I am using an UNO... i had the 5v lead of PIR in 5v of arduino, ground in GND and the 'OUT' lead of PIR in digital pin 2... but I get nothing at all?


DDRYA

haha YES!!! thank you! life saver!!!!

Go Up