Go Down

Topic: PIR Sensor & LEDs (Arduino Newbie) (Read 972 times) previous topic - next topic

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
   
    }
           
}
JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

AWOL

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

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"? 
JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

AWOL

Code: [Select]
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.
"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.

Thank you, AWOL!  Will use your suggestion.
JOPIN STUDIOS | Trusted Web Design since 1998 | www.jopin.com

Go Up