PIR Sensor Code Help

I have a code that I'm having a little difficulty with. Basically I'm trying to get it so that when the LED activated by the PIR sensor (HC-S501) lights up that it will change the LED sequence, and when it turns off the LED sequence will go back to the first one.

Code is attached.

KnightRiderSurveillanceModule_pde.ino (12.3 KB)

First task : get rid of all the delay()s so that the program can be responsive to inputs. Use millis() for timing as in the BlinkWithoutDelay example in the IDE

UKHeliBob:
First task : get rid of all the delay()s so that the program can be responsive to inputs. Use millis() for timing as in the BlinkWithoutDelay example in the IDE

I don't think it's the delays that are the problem, the button input method works perfectly, I just want the PIR sensor's LED value to basically do what the button does Bob? :wink:

I think I figured it out, I added this in place of the button check and it seems to work the way I want now.

// check if PIR LED is Lighted
        val = digitalRead(ledPin);
       
        if(val == HIGH) 
        {
           sequence = 2; // if sequence is at 3 already, make it 1 again
        }
        if(val == LOW){
          sequence = 1;
        }
        if(val == HIGH)
        {
           sequence = 2; // if sequence is at 3 already, make it 1 again
        }
        if(val == LOW){
          sequence = 1;
        }

Are you planning to handle the other values that digitalRead() might return?

PaulS:

        if(val == HIGH)

{
          sequence = 2; // if sequence is at 3 already, make it 1 again
       }
       if(val == LOW){
         sequence = 1;
       }



Are you planning to handle the other values that digitalRead() might return?

Thanks @PaulS,
Possibly, the idea I'm working on is that this additional Surveillance Mode module would use the PIR sensor to transition some indicator LEDs between say an
"At Rest" or "Normal Mode"
when the Surveillance Mode is switched on from our Knight O.S. software.
When the PIR sensor detects motion the indicator LEDs will change to the
"In Out"
Display pattern and the motion indicator LED I'll most likely connect to an Optocoupler that will switch a button on the Arduino Mega that our Knight O.S. uses so that our software can then trigger the computer camera to start taking snapshots every second or so until the PIR sensor no longer detects motion and then the camera would then stop snapping pictures.

I have that 2nd part set up in our Knight O.S. software already, all it needs is the trigger method from this additional Module using the extra Arduino Uno I have for it :wink:

I have a video demo of the testing of the Surveillance indicator Module working on the breadboard here:
Video Demo Of Surveillance Module

Possibly

What other values might they be?

PaulS:
What other values might they be?

Not really sure, mostly I just need to know if the LED for the indicator for the PIR sensor being active.

On = Change the LED chaser pattern & then send that pulse to trigger the Picture snapping on the Knight O.S.

Off = Change the LED chaser pattern back to the default One at a time back and forth pattern & Turn off the Picture taking function on the Knight O.S.

Not really sure

The digitalRead() function can only return HIGH or LOW. If the value isn't HIGH, it must be LOW. There is no point in testing for LOW.

   if(val == HIGH)
   {
      // do something
   }
   else
   {
      // The pin must be low
   }

PaulS:
The digitalRead() function can only return HIGH or LOW. If the value isn't HIGH, it must be LOW. There is no point in testing for LOW.

   if(val == HIGH)

{
      // do something
  }
  else
  {
      // The pin must be low
  }

Ahh ok thanks Paul,
I'll try that out and see if it performs better. :wink:

PaulS:
The digitalRead() function can only return HIGH or LOW. If the value isn't HIGH, it must be LOW. There is no point in testing for LOW.

   if(val == HIGH)

{
     // do something
  }
  else
  {
     // The pin must be low
  }

I added in the delay that I had commented out and it seems to work better, this way the sensor does not trip as soon as the device is turned on.

// check if PIR LED is Lighted
        val = digitalRead(ledPin);
       
        if(val == HIGH) 
        {
           sequence = 2; // if sequence is at 3 already, make it 1 again
        }
        else
        {
          sequence = 1;
        }
        
                delay(300);  // we need this here to ensure that only one button press is registered 
                             // otherwise sequence is very quickly incremented and the circuit doesn't work
                             // if you don't understand what I mean, remove this delay and try it...
// if you don't understand what I mean, remove this delay and try it...

You REALLY need to look at the state change detection example. You want to increment the value when the switch BECOMES pressed, not when the switch IS pressed.

PaulS:

// if you don't understand what I mean, remove this delay and try it...

You REALLY need to look at the state change detection example. You want to increment the value when the switch BECOMES pressed, not when the switch IS pressed.

Sorry I might not have been clear Paul.... the button I changed to use the LED for the PIR to Trigger the sequence instead of the button, I'm also just using sequence 1 & 2 and not using sequence 3, I just left 3 in there just in case I ever found a future use for it, right now I can only see me needing two different visual states. Sorry if I made that a little confusing.

Sorry if I made that a little confusing.

Not a little...

the button I changed to use the LED for the PIR to Trigger the sequence instead of the button

I hope that that makes sense to you. It doesn't make sense to me.

PaulS:
Not a little...
I hope that that makes sense to you. It doesn't make sense to me.

lol Ok lets say that the original script used a momentary push button to change the LED sequences right?

Well all I did was add in the PIR sensor and instead of the momentary button changing the LED sequences I have the LED that the PIR sensor lights up to do the job of the momentary button.

Clear now ? :wink:

Clear now ?

Yes.

PaulS:
Yes.

Ahh good... lol I was afraid my communication skills were slipping :wink:
I was doing a little experimenting to see if the PIR sensor would work through glass, tinted glass no but transparent glass it seems to work through OK. I'll have to see if I can get it to work through a transparent piece of Lexan plastic.

A little off topic I know :wink:

I tweaked the code a little in an attempt to get this to work with 2 PIR sensors but I must have goofed up my code.

Code Attached

KnightRiderSurveillanceModule_pde.ino (12.6 KB)

int pir1Pin = 12; // Input for HC-S501 Number ONE
int pir2Pin = A1; // Input for HC-S501 Number TWO
int pirState; //We start, assuming no motion detected

int pirValue; //Place to store PIR Value

Two PIR pins; one state. Not a good ratio.

  pirState = digitalRead(pir1Pin) + digitalRead(pir2Pin);

Adding states is certainly a novel concept. I wonder why more people don't do that. Because it doesn't work would be my guess. 8)

PaulS:

int pir1Pin = 12; // Input for HC-S501 Number ONE

int pir2Pin = A1; // Input for HC-S501 Number TWO
int pirState; //We start, assuming no motion detected

int pirValue; //Place to store PIR Value



Two PIR pins; one state. Not a good ratio.



pirState = digitalRead(pir1Pin) + digitalRead(pir2Pin);



Adding states is certainly a novel concept. I wonder why more people don't do that. Because it doesn't work would be my guess. 8)

Seemed like a way to go.... is there a better way to do this Paul?