# Help with code for a Halloween prop

**URL:** <https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473>\
**Category:** General\
**Created:** [October 14, 2014, 7:16pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473 "2014-10-14T19:16:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 14, 2014, 7:16pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/1 "2014-10-14T19:16:38Z")

</div>

Hello Everyone,  
I am quite new to Spark and time is running out before my Halloween prop has to be ready. I am using a Spark Relay Shield and would like to control 3 different props. Using Tinker, all three props are wired in and work fine, however, now is the time to automate it. I need help with the code to make this happen:

1. Event will be triggered by my motion sensor (drops to 0VDC when triggered). Sensor input needs to be ignored while the below tasks happen.
2. Turn on D0, D1, D2 (relays) 10 seconds after sensor trigger.
3. Keep relays on for 20 seconds
4. Reset and wait for another motion sensor trigger.

Any help would be greatly appreciated. Have to scare some people this year, all in good fun.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 14, 2014, 8:30pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/2 "2014-10-14T20:30:38Z")

</div>

Alright, sounds very do-able.

A couple of questions.

1. What is the logic level of your motion sensor? Is HIGH 3.3V or 5V?
2. Are all your props wired to the relay’s “normally open” side?

Answer those and the code should be pretty straight forward

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 14, 2014, 9:03pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/3 "2014-10-14T21:03:19Z")

</div>

The motion sensor is high @ 3.3V, and yes the props are wired normally open.

We’re very excited now, I sincerely appreciate the help.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 15, 2014, 3:09am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/4 "2014-10-15T03:09:35Z")

</div>

Ok. I haven’t tested the following code but it compiles. Hook the motion sensor up to D3 and flash the following code and let me know what happens.

Code: [https://gist.github.com/harrisonhjones/403398d12fe8f018bbad](https://gist.github.com/harrisonhjones/403398d12fe8f018bbad)

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [October 15, 2014, 3:38am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/5 "2014-10-15T03:38:06Z")

</div>

Code looks good, thanks for offering a hand 🙂

Skimming the code I see one timer area that could cause a problem, albeit very rarely (like every 50 - 70 days or something like that if I remember)

Instead of:  
`if((lastMotionTime + propDelay) < millis())`

Use subtraction… the math with unsigned numbers always works out correctly even when the millis() count wraps:  
`if((millis() - lastMotionTime) > propDelay)`

* * *

Also there is a missing `digitalWrite(RELAY3, LOW);` and `digitalWrite(RELAY3, HIGH);`

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 15, 2014, 4:50am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/6 "2014-10-15T04:50:41Z")

</div>

@BDub  
Do you have any documentation on it “always works out”? I’m just curious on why some math works out better than other math. Is there something special going on behind the scenes with the subtraction?

Also, the gist has been updated to include the missing digitalWrite(RELAY3, HIGH/LOW) lines

---

<div class="post-metadata">

**Author:** ![zach](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zach/32/20714_2.png) [@zach](https://community.particle.io/u/zach)\
**Post date:** [October 15, 2014, 6:03am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/7 "2014-10-15T06:03:17Z")

</div>

@harrisonhjones the issue is that `millis()` increments by 1 every millisecond, but it’s a statically typed 32-bit variable, so every 4294967296 milliseconds it will “wrap” and end up back at zero. This happens roughly every 50 days.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [October 15, 2014, 6:22am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/8 "2014-10-15T06:22:17Z")

</div>

I guess it’s not exactly just because it’s subtraction, but also the way in which you are subtracting.

Since `millis()` returns an unsigned long (32bit) value, we want to specifically make sure all math is done with uint32\_t aka unsigned long.

If you imagine `millis()` counting up from 0 in milliseconds and one day in the distant future wrapping around back to 0 because it’s always counting up and overflowing eventually…

If you save `lastMotionTime = millis()` 2 seconds before it wraps and then the following code executes: `if((lastMotionTime + propDelay) < millis())` before millis() wraps, it will immediate execute because lastMotionTime + 20000 will wrap around to a small number which will be less than the current millis() value.

If you instead use subtraction, then 2 seconds before millis() wraps if we immediately execute this code: `if((millis() - lastMotionTime) > propDelay)` you’ll find that a large millis() value minus lastMotionTime (which we just saved 2 seconds ago) will basically be 2000, which is not greater than propDelay until it’s 20000. But what happens after millis() wraps? surely that will be bad… nope. This is where the subtraction helps. Let’s say we are 3 seconds from when we saved lastMotionTime, so millis() equals 1000 now. With unsigned integer math, 1000 minus (that really large value representing millis() 2 seconds before it wrapped… something like 4294965295 or so) is a negative number… but since we are using unsigned it’s always positive, so it wraps around and the answer is 3000. Thus you never have a wrap error case. Actually you’re probably thinking 1000 - 4294965295 should be some really large negative number, but if it’s always positive then it’s just some really large positive number, not 3000. Well…

The really low level explanation involves how computers do math. Subtraction of two integers is really subtraction of two binary numbers. Subtraction is done by adding the 2’s compliment of the number to be subtracted, to the number being subtracted from. In the case of unsigned math, the answer is always positive. Kind of a walkthrough with signed binary math [https://www.youtube.com/watch?v=vfY7bN\_3VKw](https://www.youtube.com/watch?v=vfY7bN_3VKw)

Subtraction “just always works” if you subtract the previous count from the current higher count (even if that higher count has a chance of wrapping). You always get the absolute difference between the two numbers. Notice this doesn’t work the same way if you subtract the currently incrementing value from the previous count, and it also won’t work if you try to subtract the previous count from the current higher count if it has wrapped around past the previous count 😉

I hope that makes sense! Otherwise I defer to it “just always works” because it does 😄

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 15, 2014, 11:37am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/9 "2014-10-15T11:37:00Z")

</div>

Thank you so much for the help! I will load the core this afternoon and test the setup.  
Harrison, I will make BDub’s math changes if you don’t have time to change the code online.  
BDub & Zach, thanks for the coding input. All of this reinforces the fact that I have some substantial learning to do. Do any of you know of a great place to start my coding voyage?

I will complete the project very soon thanks to all of your help.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 15, 2014, 6:32pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/10 "2014-10-15T18:32:11Z")

</div>

@zach & @BDub: I appreciate the response but I’m still a little confused. I actually knew (and thought I understood) about the millis() wrapping and the 2s compliment subtraction which is why I’m so confused.

**Let’s say we have the following situation:**  
lastMotionTime = 2 seconds before overflow  
4 seconds goes by. Millis() is now 2,000  
the “if((lastMotionTime + propDelay) \< millis())” check occurs  
lastMotionTime + 10,000 should equal 8,000 correct? 8,000 is \> millis() so the statement would NOT be true and the IF would not fire. It sounds like you are saying that this is not the case. I just don’t understand

Note: see [http://ideone.com/iafX6u](http://ideone.com/iafX6u). It appears both methods work so I continue to be a little confused

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [October 16, 2014, 1:01am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/11 "2014-10-16T01:01:42Z")

</div>

Yeah you’re thinking about it right… but the problem case with your code is when you are within 10 - 20 seconds of rollover (depending on propDelay vs propOnTime) and you save lastMotionTime and then the `if` statement is evaluated immediately, i.e., _before_ millis() rolls over.

I’m going to steal your text 😄

**Let’s say we have the following situation:**  
lastMotionTime = 9 seconds before overflow  
1 second goes by. `millis()` is now (4294967296 - 8000)  
the `if((lastMotionTime + propDelay) < millis())` check occurs  
lastMotionTime + 10,000 should equal (4294967296 - 9000 + 10000) which rolls over to 1000.  
1,000 is \< `millis()` which is (4294967296 - 8000) so the statement WOULD BE true.

That’s the problem… super tiny problem albeit every 50 days. I only point it out so that when you are building rockets some day… you know! 🙂

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 16, 2014, 1:39am UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/12 "2014-10-16T01:39:36Z")

</div>

Nicely done. You are correct! (obviously). I will make the change to the gist and my own libraries. Good catch.

Note, for those of you who want to “play along from home” the earlier [ideone.com](http://ideone.com) link I posted should go to the version in which millis() is 8 seconds before rollover and lastMotionTime is 9 seconds before which illustrates the error in my code

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 16, 2014, 11:10pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/13 "2014-10-16T23:10:41Z")

</div>

Thanks everyone, the code works quite well. All I need to do now is the cosmetics.

Thanks again.

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 19, 2014, 1:19pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/14 "2014-10-19T13:19:06Z")

</div>

Again, thanks for the help. Here is a link to the youtube video.

Still need to put the electronics in an enclosure and route the EL wire for better visibility.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 19, 2014, 7:24pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/15 "2014-10-19T19:24:44Z")

</div>

That’s awesome… and terrifying!

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 14, 2015, 4:35pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/16 "2015-10-14T16:35:37Z")

</div>

Help me Harrisonhjones, you’re my only hope…

This years project was working this morning. I added to your code from last year and it worked! Now it is inconsistent. As soon as the core comes on line, it immediately starts toggling the relays. The motion sensor seems fine, 0vdc with no movement, strong 5vdc with motion. I hooked it up to D7 and the led comes on momentarily with motion. Could someone tell me what is going wrong?

```
https://github.com/glockhaus/Halloween/blob/master/Boardshaker
```

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [October 14, 2015, 4:51pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/17 "2015-10-14T16:51:14Z")

</div>

I shall try! Could you setup your reply like this:

What happens:

- List of things that happen
- Another item
- Yet another item

What’s supposed to happen / what’s expected

- Different stuff here
- Different stuff here
- Different stuff here

That’ll make it much easier for me to get a good understanding of the issue!

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 14, 2015, 5:06pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/18 "2015-10-14T17:06:08Z")

</div>

using a photon on a v2 (newer) relay board.

what happens...  
upon photon startup without any motion sensor input, relays D3 & D4 come on and toggle like they are supposed to. But that action is supposed to be triggered by motion. The toggling action occurs for the specified amount of time (10 secs), then immediately start toggling again. This goes on until I unplug the photon.

Expected behavior...  
Once the photon is powered up, it should await motion sensor input, wait for a 1/4 second then toggle the relays on and off for 10 seconds. After that, it should return to a state awaiting motion sensor input again.

I had another post going,

> [@Code help needed for relay shield for Halloween](https://community.particle.io/t/code-help-needed-for-relay-shield-for-halloween/15723):
>
> Well, it’s that time of year again… Halloween Prop building time! This years prop requires help with the code that will enable several boards to shake that are covering a window. I have a photon in a v2 relay shield powering 2 separate 12VDC automotive door lock actuators. Each actuator is connected to 2 wooden boards. I flashed the Photon with one of the library relay shield programs to test it out, and it works fine. Now it needs a little coding magic that I cannot provide. I ordered a motio…

but didn't hear much from anyone after that, so I thought I would charge ahead with your code from last year (modified), with some success.

---

<div class="post-metadata">

**Author:** ![dougal](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dougal/32/20713_2.png) [@dougal](https://community.particle.io/u/dougal)\
**Post date:** [October 14, 2015, 7:53pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/19 "2015-10-14T19:53:17Z")

</div>

When the Photon first powers up, I think all the GPIO pins are floating. So you might want to add some pulldown resistors to your relay output pins.

Not sure why it’s re-triggering after startup, though…

---

<div class="post-metadata">

**Author:** ![dura4570](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dura4570](https://community.particle.io/u/dura4570)\
**Post date:** [October 15, 2015, 1:14pm UTC](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473/20 "2015-10-15T13:14:10Z")

</div>

Well, further troubleshooting reveals that any wire (open, no sensor or load) inserted into the pin designated for the motion input will trigger a never ending run cycle. I suspect that something has happened to either the v2 shield or the photon? I have a new photon arriving tomorrow. I love that they are only $20.

Has anyone reviewed my code to rule it out as the issue. I know it is probably not as efficient as it could be, but I am learning slowly.

dougal, I will research the pulldown resistor idea. Is there a magic value or is it dependent on the load? In this case the relay is switching 12vdc.

A sincere thanks for harrisonhjones and dougal!

[Next page](https://community.particle.io/t/help-with-code-for-a-halloween-prop/7473.md?page=2)
