Help with code for a Halloween prop

I don’t know if there’s an “ideal” value for a pulldown resistor for this. If it was me, I’d probably use a 4.7K or 10K? Mainly, because those are values I frequently see mentioned for this purpose. :smile:

I did spend some time yesterday looking over the code. The only thing I really spotted was that in the relayControl() function, passing ‘r1’ or ‘r2’ will toggle pins 0 and 1, so probably not the correct pins. The state logic seems okay, though there’s some weirdness with the code blocks for state == 2. It won’t break anything, but it looks like the product of code that was rewritten a few times, but never cleaned up? I’d probably clean it up like so:

else if (state == 2)
{
    if ((lastMotionTime + propOnTime) < millis())
    {
        state = 0;
    }
    else
    {
        digitalWrite(RELAY1, HIGH);
        delay(75);
        digitalWrite(RELAY2, HIGH);

        delay(100);

            digitalWrite(RELAY1, LOW);
            delay(200);
            digitalWrite(RELAY2, LOW);

            delay(75);

            digitalWrite(RELAY1, HIGH);
            delay(400);    
            digitalWrite(RELAY2, HIGH);

            delay(100);

            digitalWrite(RELAY1, LOW);
            delay(100);    
            digitalWrite(RELAY2, LOW);
        }
    }

Basically, just putting all of the relay control together in the else block, and getting rid of the other unneeded block markers.

Thanks for the code editing help. I removed the extra braces from state == 2. Not sure about what to do for the r1 and r2 in the relayControl() function, so I left it alone.

received the new photon 1/2 hr ago, flashed the code, and everything is behaving as intended. Perhaps the last photon was dropped or otherwise damaged? We may never know. I am hopeful that when I assemble the prop later everything will still work well.

Thank you

I think at the end of relayControl(), I’d do this:

  // write to the appropriate relay
  int relayPin;
  if (relayNumber == 1) {
    relayPin = RELAY1;
  } else if (relayNumber == 2) {
    relayPin = RELAY2;
  }
  digitalWrite(relayPin, relayState);
  return 1;

And if you added more relays to the system later, it would be easy to just add new conditions for relayNumber == 3, 4, 5, etc.

There are probably more elegant ways to do it, but with a small number of possible values, this should be fine.

dougal,

Thanks for the coding help. I assembled the prop with the new photon and everything works great. I sincerely appreciate all of the help. After all the troubleshooting, it appears to have been a bad photon.

Maybe you zapped a pin?

That is quite possible. I was using an older computer power supply to power the 12 vdc actuators and the 5vdc motion sensor. The power supply fan stopped working, and is dead now. I think that when it died, it sent the 5v to ground and made my photon a very light paperweight.

All is well now with a new photon and a quality 5v power supply. The prop is actually pretty cool, already scared the neighbor when he walked up to it. I’ll post a video when we do our dry run next weekend.

I posted the project to YouTube, may still tinker with the timing of the shaking boards.

Thanks Again!

1 Like