Arduino write to memory and self shut down (over MOSFET) - An idea help i would like to ask you for help with design of an circuit.

I have and atmega328p, with detection for voltage on its Vin (Vcc).
I have made and voltage divider and its output i connected to Analog Pin. And i would like to, if an voltage decrease under an certain level (4.5V), to shutdown itself (atmega328p). 

As i said the voltage detection is no problem (there is an voltage divider), programing it is as well no problem, but i though that i program it like this:


  Detect voltage on analog pin A0, if the voltage is less than certain
  level (4.5V), write to memory and send to digital pin 3 an "LOW" (0)
  logical state.


This will turn the mosfet off and the atmega will loose its ground (GND) and its shuts down, but i have there an pull-up resistor which when the atmega shutsdown takes its lead an set Gate at MOSFET to high (transistor will turn on) and atmega turns on, then it shuts down and this will do the infinite cycling until there's no power to turn on atmega. 

I want to use an Supercapacitor (2.2F 5.5V) to power it when the main input is lost and to give an atmega a time to write to memory and steadily turns off. 

I am adding an schematics to better look at the problem. 



I know that there is no crystal and capacitors and the other things to make it reliable and fully functional, but this is not a subject of this question.Its an first sketch, which is not completed.

I know some ICs that can detect Voltage and turn its output to some logical state (1 or 0), like an MAX809 (803 or 810) and so on and of course this is my backup plan, but i want to ask you, if you have any great idea to make this without any special ICs and more easily? I also thought about some OPamp or thyristor maybe, i don't know......

Can you help me with an idea or better with an schematics how to do it ? Thanks.
You should be using a P-channel MOSFET (which is set to be normally OFF with a pull-up resistor) and the Arduino is responsible for keeping it on:







simulate this circuit &ndash; Schematic created using CircuitLab

In this schematic M1 is normally off.  You press and hold SW1 and the Arduino gets power. The first thing it then does is turn on the GPIO. That turns M2 on, which in turn pulls the gate of M1 LOW turning it on. You can now release M1 and the Arduino will stay on.  When you want to turn off, just switch off the GPIO, turning off M2, which then allows R1 to pull the gate of M1 up again turning off the power.

This arrangement means:


You can control an incoming voltage higher than 5V (thanks to M2 doing the actual switching of the power MOSFET)
You get a soft-power switch to turn the power on
Current draw of the control circuit is basically the incoming voltage divided by the resistance of R1 (you can safely increase this if you want to reduce current draw - 1MΩ is fine).
You can have all the rest of the circuitry on the Arduino side of M1 so it has power removed when switched off (to prevent over-discharge of your batteries)
You are switching VCC, not GND (switching GND can cause all sorts of problems).