Help with using SleepyPi2 and Raspberry Pi

This is my first forum post and I was hoping that someone could help me with a problem that I've been trying to solve but can't seem to get past.

Project description: I'm using a SleepyPi2 as a power management board for a Raspberry Pi. I have a PIR sensor connected to the SleepyPi2 that when triggered will turn on the Raspberry Pi. Once the Raspberry Pi turns on it will take a series of video and pictures. The SleepyPi2 will send notice of any further motion through the serial port of the Raspberry Pi to take further images if detected. The Raspberry Pi will shut it's self down once no motion is detected. The SleepyPi2 will detect that the Raspberry Pi has been shutdown and then cut the power to the Raspberry Pi and wait for motion or a button press on the SleepyPi2 to turn back on. The Sleepy2 also has a button which can be help down to power off the Raspberry Pi.

Problem: I have most of this working how I would expect however when I issue a shutdown command from the Raspberry Pi the code that I'm running on the SleepyPi2 turns the Raspberry Pi back on again straight away. What am I doing wrong in my code? I know it's probably some logic that I'm not doing right but with my limited coding experience and not being able to run the code real time and put print statements in I'm having a hard time debugging it.

Additional Info: With the code below it will shutdown the Raspberry Pi and also wake it up using the PIR on the SleepyPi2.

Resources I'm using:

ps. Any other pointers would be appreciated.

I've had to create a Gist because it was too long to add in the forum post. It can be found here

This is an Arduino forum, not a Raspberry Pi forum.

pylon:
This is an Arduino forum, not a Raspberry Pi forum.

The SleepyPi seems to be running on Arduino hardware; see https://spellfoundry.com/docs/sleepy-pi-2-getting-started/#1-toc-title.

Sorry I should have mentioned that it's essentially just an Arduino underneath.

The Sleepy Pi 2 contains an Arduino processor (ATMEGA328P) and its functionality is governed by the code that the Arduino contains.