[Lab] Arduino LED Strip, PIR, and Relay

Dave Hunt dave at huntgang.com
Fri Mar 2 16:53:03 EST 2018


I don't often do other peoples "homework" as I personally learn more by
doing it myself, but in this case I thought what the heck.  Hopefully you
take a few minutes to see how I handled it.  I know others may have
approached it differently.  Anyways, try the following code:


//constants
const int ledPin = 13;
const int sensorPin = 2;
const long stayOnPeriod = 2000;   // length of time to stay on (in
milliseconds)

//variables
unsigned long turnOffTime = millis();  //Start with the led turned off

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // check if the sensor is detecting then increase the time to stay on.
  if (digitalRead(sensorPin) == HIGH)
  {
    turnOffTime = millis() + stayOnPeriod;
  }

  if (turnOffTime > millis())
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

On Fri, Mar 2, 2018 at 2:56 PM, Bruce Harding <bsharding at rogers.com> wrote:

> Dave,
>
> I figure out why the delay wasn't working.  A stupid error.  I've looked
> at the millis thing and the blink without delay code, but I'm such a
> beginner I'm not sure how to take what I'm using and convert it to use
> millis.
>
> ====
> bruce
>
> On 2 March 2018 at 14:28, Dave Hunt <dave at huntgang.com> wrote:
>
>> I think you need some non-blocking code in there.  The use of delay sits
>> and waits and does not check the status of the motion detector while it is
>> waiting.  So any time it is triggered, it should turn on the light and then
>> "hang" before turning off the light and starting to check for motion again.
>>
>> What I would do is use millis function.  Look at the blink without delay
>> (non blocking code) for an example.  then anytime I saw montion I would
>> change the counter to current millis + "on timer length"
>>
>> Hope this helps.
>>
>> On Fri, Mar 2, 2018 at 12:50 PM, Bruce Harding <bsharding at rogers.com>
>> wrote:
>>
>>> I'm trying to get the code for this project to work.  I want the LEDs to
>>> stay on
>>> for 10 minute or continue on if more motion is detected. right now the
>>> LED strip
>>> turns off 2 seconds after no motion.
>>>
>>> the code:
>>> https://gist.github.com/anonymous/8f0be0ffb6366ebe814c6d9b0ef7cd49
>>>
>>> pics of the project:
>>> https://photos.app.goo.gl/QrgMDG1KMnpNUjdy2
>>>
>>> I'm using the Grove Shield and Relay
>>> http://wiki.seeed.cc/Grove-Relay/
>>>
>>> PS. I've not coding experience
>>>
>>> ====
>>> bruce
>>> faintfuzzies.ca
>>>
>>> _______________________________________________
>>> Lab mailing list
>>> 1. subscribe https://artengine.ca/mailman/listinfo/lab
>>> 2. then email Lab at artengine.ca to send your message to the list
>>>
>>
>>
>
>
> --
> ====
> bruce
> faintfuzzies.ca
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://artengine.ca/pipermail/lab/attachments/20180302/b9c60bfd/attachment.html>


More information about the Lab mailing list