Using code to power off arduino?

(Note: I am a novice at arduino and I am trying to expand my experience with personal projects)

So I'm creating this night-light thing that uses a rotary encoder to cycle through all of the rgb values in the rgb color spectrum. One of the features I want it to have is that if I hold down the built-in button on the encoder, then it will power off the arduino. I'm sure I can figure out how to detect when the button is held down, but I am wondering if there is a way to write code so that the arduino powers off. Is there a simple way to do this or is the solution more complicated than I think?

Thanks,
Carson

(I am also posting this in project guidance because I'm not sure what category this question belongs in)

but I am wondering if there is a way to write code so that the arduino powers off.

No.

You would probably need a relay/transistor/MOSFET of some sort for that: Arduino Auto Shutdown (with relay) - YouTube

You could have the Arduino go into sleep mode on a button push, which dramatically reduces the power consumption. See this tutorial.

Or use an electronic power switch.

(Note: I am a novice at arduino and I am trying to expand my experience with personal projects)

So I'm creating this night-light thing that uses a rotary encoder to cycle through all of the rgb values in the rgb color spectrum. One of the features I want it to have is that if I hold down the built-in button on the encoder, then it will power off the arduino. I'm sure I can figure out how to detect when the button is held down, but I am wondering if there is a way to write code so that the arduino powers off. Is there a simple way to do this or is the solution more complicated than I think?

Thanks,
Carson

(I am also posting this in programming because I'm not sure what category this question belongs in)

Do not cross post. It wastes peoples time. Please see the "how to use this forum" thread.

There is no way to power off the board entirely in software (you need external hardware for that) - you can put the chip into sleep mode, though.

Or if it's plugged into mains, you can just have it turn off the external lights.

Sleep mode can get the power consumption of the processor itself down to negligible levels. Unfortunately, in an Arduino board, the power consumption will be much higher (too high to treat as "off") due to the power LED and the regulator's quiescent current. The LED can be removed easily (rest soldering iron on it, or use the tip of a small screwdriver or similar on it until it stops emitting light). Reg is a bit harder (and then you need input of appropriate voltage - I try to run it straight off the battery when possible). Or you build a barebones arduino on perfboard (or breadboard) without those components in the first place.

Threads merged.