I've noticed that when I open and close the com port my Arduino is connected to, the device resets. This makes the program run the setup profile again and it loses the previous variable states. Is there any way to prevent this?
If you review the Uno schematic, you can see the circuitry that enables an automatic reset when serial communication starts. The circuitry is intended to allow easy program downloading.
There are two or three ways to prevent that reset: add a 10 μF capacitor from ground to reset; use a much-lower resistance for the pullup on reset; cut a trace from the serial-interface pin (eg CTS) that pulls reset.
The first method is relatively easy to implement, and easy to remove whenever you want to install a new program.