Hi,
I'm putting together a project that will have several PIR motion sensors. When movement is detected in the area the system will trigger relays and turn on related strands of high powered LEDs.
Right now I'm using a delay(x) to set the pin high but the problem is during the delay time the PIR isn't sensing. I want to set this up so if someone is moving in the area they will constantly reset delay(x). When the movement ends I want it to cycle through delay(x) and power the pin down.
I think this means I need to use an interrupt but I have no idea how to do that. I've also read about Millis() but I don't know how to use it.
My sketch is below, any and all advise is appreciated.
I'll bet the PIR is sensing during the delay time
Your Arduino isn't responding to the sensing, but that's not the same thing at all.
You don't need interrupts; millis will do what you want.
Have a look at the blink without delay example for clues.
(Why are you using analogRead? PIR detect movement, not range/proximity)
Cool, that looks like what I'm looking for, I'll give it a try.
I'll be using this in a small bike shop with lots of activity. I'm using analog read with the thought that when the sensors are in place I can adjust sensitivity to prevent false reads. Is this a good idea or should I use digital read and spend more time on the placement of the sensors?
OK, I checked out the blink without delay and merged it with my sketch but I can't quite get it to work. Here's what I have.
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 11; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 3000; // interval at which to blink (milliseconds)
// Sensor variables
#define SENSOR 0
#define PROXIMITY_THRESHOLD 50
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(SENSOR, INPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (analogRead(SENSOR) < PROXIMITY_THRESHOLD) // Is someone close?
{
digitalWrite(ledPin, HIGH);
interval; // THIS DOESN'T WORK
}
else if (analogRead(SENSOR) > PROXIMITY_THRESHOLD)
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, LOW);
}
// set the LED with the ledState of the variable:
// digitalWrite(ledPin, ledState);
}
If I am in the triggered state
1.1) If millis() minus last-triggered-start is greater than timeout
1.1.1) leave the triggered state
If the sensor shows movement
2.1) If not in the triggered state
2.1.1) enter the triggered state
2.2) set the last-triggered-start time to the current time
Do whatever you need to do with whether you are in the triggered state or not
PIR detector modules detect motion and output a high or low signal, they don't output a continuously varying voltage based on how much motion is sensed. That is all internal. There is often a pot to adjust the sensitivity. Sometimes they don't output a very high voltage, which is where an analog channel might come in handy, but that's usually fixable with a pullup resistor. So, don't feel restrained to using the analog inputs only.