Power down sequence?

Hello all,

i am very new to arduino, and i am currently waiting for my board to arrive so i can start tinkering away with it, and i am quite happy with the versatility of it after looking at example programs and projects, because i completed a few computer science courses in C/C++.

Im sorry if some of this may be redundant but i haven’t had time to test these answers and i can’t find anything about them.

From what i can tell the Setup function is executed once and that is done upon the arduino receiving power? Is there a way to do a power down sequence?

Example that im talking about:
Im looking to create a cover that opens up in my car, upon it starting (this is where the setup portion comes in). So the arduino would automatically execute the code to raise the cover, once power gets to the arduino

and just as the opposite (which is where my main question is) i would like to make it so that when the power to the arduino is cut (by the car turning off)… to be able to close the panel, and then the arduino shut down. Here is where im looking to take suggestions on how i could achieve this “power down” type of sequence.

One thing i do know.

There is a steady power from the battery(although i heard this depends on the car, but i checked for my car and it does have it :slight_smile: ), and there is a power that is only on when car is on.

You could use the non-permanent power to drive a transistor/relay to let you know when it’s cut off. Just check for digitalRead(THAT_PIN) == HIGH at the end of every loop() iteration.

Alternative method that just came into my head and may or may not be feasible: if the two power sources are different voltages you could feed one into an analog input (through a voltage divider, because at least one of them is 12V) and check for that going past a certain threshold.

If you actually need to switch between the two power supplies, I’m fairly sure you’d need some external circuitry for that (however, I’ll probably be proven wrong.)

You can use the brown out detector to trigger an interrupt service routine. This can be triggered when the voltage drops below say 4V and there is probably enough time to shut things down. You can extend the time slightly by having a bigger capacitor across the power supply.

I sense a use for a Super cap :). Although…That much capacitance probably isn’t required for the milliseconds needed to run a sequence :P.

One of the problems with a super cap across the supply is that it is likely to make the supply rise time too slow to trigger the internal reset circuits in the arduino chip.