Continued implementation of the code without resetting

Hello all,

I have a question regarding Arduino Uno.

Can Arduino continue executing a code at some point after it's turned off and back on again? So that it doesn't reset and start from beginning.

For example:

Arduino ON - Servo horn goes from 90° to 0°
Arduino OFF - Servo horn stays at 0°
Arduino ON - Servo horn goes from 0° to 90°
Arduino OFF - Servo horn stays at 90°

And the cycle repeats. Switching on and off will be done by a relay, triggered by a sensor.

There are no delays in the code.

So basically it's a loop that doesn't reset when switching the Arduino on and off.

Thank you all for any help about this issue.

You might be able to save a variable that records the current state of the program and regularly writes the value of that variable to EEPROM. Then, in setup(), you would read the EEPROM value at startup, and use a switch/case block to determine which function(s) get called. Once the startup code finishes, control goes to loop() which records the new state in EEPROM and the program continues. One fly in the ointment, however, is that EEPROM has a finite number of write cycles before it gets flaky. Therefore, you probably can't update the state stored in EEPROM on every pass through the loop. You'd need to compromise on that.

Thank you both for your answer and guidance.

Does your servo always turn to 0° to 90° and viceversa? In case you can setup a couple of limit switch on both position, read it with your arduino at start so you'll know the position where the servo is left and acting according.

Cheers, Ale.

Barko:
Can Arduino continue executing a code at some point after it's turned off and back on again?

Perhaps you could just leave the Arduino running constantly?

...R

@Robin2

I can't because it doesn't apply to my project.