Loading...
Pages: 1 [2]   Go Down
Author Topic: using PIR Sensor to fading LED  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Online Online
Brattain Member
*****
Karma: 137
Posts: 19010
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sketch?
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I finally figured it out!  smiley-lol

Code:
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
}




Logged

Pages: 1 [2]   Go Up
Print
 
Jump to: