0

Using a PIR to trigger an LED and soundboard
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Using a PIR to trigger an LED and soundboard

by jc27 on Sat May 25, 2019 10:03 am

I am using a PIR to trigger an LED and an audio fx board connected to an UNO. The way I would like it to work would be when the PIR detects motion, it will trigger an LED to fade and have the audio fx board play a sound clip. The code I have is basically the fade example and a bit from a PIR example I found.. As it is right now, the LED blinks, but not fades, and the audio fx board does get triggered. Is there any way to get the LED to fade in and out rather than blink?

Code: Select all | TOGGLE FULL SIZE
int ledPin = 12;               
int brightness = 0;   
int fadeAmount = 5;
int inputPin = 2;               // choose the input pin (for PIR sensor)
int val = 0;                    // variable for reading the pin status
int pirState = LOW;             // track state of PIR module
int audioPin = 8;
                               
void setup()
{
  pinMode(audioPin, OUTPUT);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  digitalWrite (inputPin, LOW);
  digitalWrite (audioPin, HIGH);

}
void loop()
{
  val = digitalRead(inputPin);   // read input value from PIR
  switch (val)
  {
    case HIGH:
      {
        digitalWrite(audioPin, LOW);  // trigger audio fx board
        analogWrite(ledPin, brightness); 
        brightness = brightness + fadeAmount;
        if (brightness <= 0 || brightness >= 255)
        {
          fadeAmount = -fadeAmount;
         
        }
        delay(30);
          //digitalWrite(ledPin, HIGH);
          //delay(100);
          //digitalWrite(ledPin, LOW);
          //delay(100);
       }     
    case LOW:
      {
        digitalWrite(audioPin, HIGH);
        digitalWrite(ledPin, LOW); 
      }
  }
}

 

jc27
 
Posts: 165
Joined: Sun Nov 24, 2013 5:05 am

Re: Using a PIR to trigger an LED and soundboard

by adafruit_support_bill on Sat May 25, 2019 10:38 am

Which Arduino are you using? AnalogWrite only works on PWM pins. On an UNO, those are pins 3, 5, 6, 9, 10, and 11.

adafruit_support_bill
 
Posts: 74647
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a PIR to trigger an LED and soundboard

by jc27 on Sun May 26, 2019 8:11 am

Yeah, I forgot about the PWM pins, and it works now. I am wondering now if it is possible to have the pin remain in the HIGH state for a set time duration when it is triggered by the PIR. The sound clips I am using all have different play times and on some of them the LED has stopped fading before the clip is done playing.

jc27
 
Posts: 165
Joined: Sun Nov 24, 2013 5:05 am

Re: Using a PIR to trigger an LED and soundboard

by adafruit_support_bill on Sun May 26, 2019 9:31 am

So you want the led fade action to last at least as long as your longest sound clip?

adafruit_support_bill
 
Posts: 74647
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a PIR to trigger an LED and soundboard

by jc27 on Sun May 26, 2019 3:37 pm

Yes. I don’t know if it’s possible though. The shortest sound clip is around 3 seconds, the longest around 10.

jc27
 
Posts: 165
Joined: Sun Nov 24, 2013 5:05 am

Re: Using a PIR to trigger an LED and soundboard

by adafruit_support_bill on Sun May 26, 2019 3:48 pm

Changing your "HIGH" case to something like this should make it run for 10 seconds:

Code: Select all | TOGGLE FULL SIZE
        case HIGH:
        {
            digitalWrite(audioPin, LOW);  // trigger audio fx board

            long end = millis() + 10000; // 10 seconds from now in milliseconds
           
            while (millis() < end)
            {
                analogWrite(ledPin, brightness); 
                brightness = brightness + fadeAmount;
                if (brightness <= 0 || brightness >= 255)
                {
                  fadeAmount = -fadeAmount;
                }
                delay(30);
            }
        }     

adafruit_support_bill
 
Posts: 74647
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a PIR to trigger an LED and soundboard

by jc27 on Sun May 26, 2019 11:31 pm

I put that new section in the code and it works much better, thanks. One thing though, if I change the 10000 to a lower number the time the LED fades doesn't change hardly at all. I set it all the way down to 800 and the fastest time I could get was 8 seconds.
Anyway, its good enough for what I am planning on using it for, thanks again.

jc27
 
Posts: 165
Joined: Sun Nov 24, 2013 5:05 am

Re: Using a PIR to trigger an LED and soundboard

by adafruit_support_bill on Mon May 27, 2019 5:36 am

. . . the fastest time I could get was 8 seconds.

The loop will get re-started as long as the output of the PIR is high.

adafruit_support_bill
 
Posts: 74647
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.