Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: Bigger1338 on Apr 14, 2018, 03:24 pm

Title: Turning LED off quickly after motion sensor trigger.
Post by: Bigger1338 on Apr 14, 2018, 03:24 pm
Hello,

I friend asked me to help put together a project for his kid's class. The idea is they are racing RC cars through a track, and they want a light to blink when the cars pass the finish line. I decided to use a motion sensor to trigger an LED, and it works great, for turning on the LED, but I want to turn the LED off fast after the motion sensor stops detecting motion. I've tested it under pretty controlled conditions. I'm wondering if it's just the sensor, or if it's my code. I've tried adjusting the potentiomets on the sensor, and I can make it less sensitive, and stay responsive longer, but still no luck turning the LED off faster..

If someone can help that would be awesome. The sensor is a PIR Motion sensor.


Here's the code.


Code: [Select]
int ledState = LOW;
int motion = 5;
int motionLed = 7;

void setup() {
 pinMode(5, INPUT);
 pinMode(7, OUTPUT);
}

void loop()
{
short sensor = digitalRead(motion);
 if(sensor == HIGH){
    digitalWrite(7, HIGH);             
    digitalWrite(7, LOW);

  }
}


Thanks in advance for any recommendations
Title: Re: Turning LED off quickly after motion sensor trigger.
Post by: PaulRB on Apr 14, 2018, 03:33 pm
Please read the forum sticky post to find out how to post code correctly, and edit your first post to fix it.

Sounds as if the sensor's output remains high for too long, even when you have adjusted it to the minimum setting. Right now, your code switches on the led as long as the sensor output is high. In fact, it switches the led on and off at high speed, too fast to see, dimming the led.

What you need your code to do is switch on the led when the sensor output changes from low to high. Then keep the led on for a short period, then switch the led off.
Title: Re: Turning LED off quickly after motion sensor trigger.
Post by: Bigger1338 on Apr 16, 2018, 06:41 pm
Thanks for the input. I have updated my code accordingly in my original post.

I did originally have a delay between the LED being turned on and the off. It was like this.

Code: [Select]

int ledState = LOW;
int motion = 5;
int motionLed = 7;

void setup() {
  pinMode(5, INPUT);
  pinMode(7, OUTPUT);
}

void loop()
{
 short sensor = digitalRead(motion);
  if(sensor == HIGH){
     digitalWrite(7, HIGH); 
    delay(500);             
     digitalWrite(7, LOW);

   }
}


That didn't have any impact on how quickly the LED turned off. It still stays on for a couple seconds. I did suspect that it was still seeing the sensor input as HIGH, but I'm just not sure how to get around that.
Title: Re: Turning LED off quickly after motion sensor trigger.
Post by: PaulRB on Apr 16, 2018, 09:20 pm
Quote
I'm just not sure how to get around that
See first sentence in paragraph 3 of reply #1. Your code is now doing the second sentence of that paragraph.
Title: Re: Turning LED off quickly after motion sensor trigger.
Post by: 6v6gt on Apr 18, 2018, 12:13 pm
Your problem is going to be that PIR motion sensors latch for a period after being triggered so you may not be able to get a fast response out of them to indicate when the object has passed out of view.

You may be better off with an infrared proximity sensor for example: https://www.sparkfun.com/products/242 (https://www.sparkfun.com/products/242)