Pages: [1]   Go Down
Author Topic: PIR Sensor & LEDs (Arduino Newbie)  (Read 894 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 2
Posts: 48
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, I'm a complete newbie to Arduino but am having a blast experimenting with it.  I'm trying to create a simple alarm system whereby an LED lights whenever a PIR sensor detects motion.  I'm able to get the LED to light when motion is detected, however, I want to make it so the LED continues to be lit (blinks continuously) until a pushbutton is depressed as a reset.

For the life of me, I can figure out the code to keep the LED blinking until a reset via pushbutton... it blinks for a few seconds and then turns off until new motion is detected... any help would be greatly appreciated! Thanks!

My code so far:

*/

const byte alarmLED =  9;    // LED pin for INTRUDER DETECTED!!! (blink)
const byte safeLED = 13;   // LED pin for SAFE (steady)
const byte motionPin = 2;   // motion detector input pin

int brightness = 0;   // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by
int resetButton = 7; // the Reset Button pin

byte senseMotion = 0;       // variable to hold current state of motion detector

void setup() {
  // set the digital pin directions
  pinMode(alarmLED, OUTPUT);
  pinMode(safeLED, OUTPUT); 
  pinMode(motionPin, INPUT);
  pinMode(resetButton, INPUT);
}

void loop()
{
 
 
    // Now watch for burglers
    senseMotion = digitalRead(motionPin);
   
      if (senseMotion == HIGH)
        {   
        // burgler found!
        analogWrite(alarmLED, brightness); // set the brightness for ledPin
        digitalWrite(safeLED, LOW); // turns off LED2 when motion is sensed
        brightness = brightness + fadeAmount; // change the brightness for next time through the loop
     
      if (brightness == 0 || brightness == 255) // reverse the direction of the fading at the ends of the fade
        {
        fadeAmount = -fadeAmount;
        }
      delay (10); // (set how fast it fades) wait for X milliseconds to see the dimming effect
        }
       
    // Need to hold the blinking until a Reset is detected
   
               
    else
    {                      // no burgler, yet...
     
      digitalWrite(alarmLED, LOW); // turns off LED1 if motion is not sensed
      digitalWrite(safeLED, HIGH); // turns on LED2 when NO motion
   
    }
           
}
Logged

JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

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

You're testing "senseMotion" every time through loop, and this initiates your flashing.
When "senseMotion" goes low, you stop flashing.
Instead, set a flag when "senseMotion goes HIGH, and only reset the flag when the button is pressed.
Your flashing now depends on the flag, not "senseMotion"
Logged

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

Offline Offline
Newbie
*
Karma: 2
Posts: 48
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the response AWOL!  Sorry, I'm really new to programming... can you give an example of what you mean by "set a flag"? 
Logged

JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

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

Code:
int flag;  //global scope, initialised to zero


// some time later, in loop
if (senseMotion == HIGH) {
  flag = 1;
}

Of course, it is better if you give your flag a meaningful name, like "motionLatch", or something.
Logged

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

Offline Offline
Newbie
*
Karma: 2
Posts: 48
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, AWOL!  Will use your suggestion.
Logged

JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

Pages: [1]   Go Up
Jump to: