Servo resetting limits function

Hi folks, (noob alert)

I've tried searching for this, I'm assuming it's very basic but I can't figure it out and can't find the answer probably 'cause I can't work out what I need to search for.

In short, two parts that I'm stuck on;
(i) how to I get a separate function to operate, essentially pausing the void loop and ignoring any other inputs then ending with a return function back to void loop with updated values to then be used in the main loop?
(ii) how can I write a script which only progresses when a button is pressed?

In Long: :slight_smile:

Here's the set up:

I will have a Mega with 12 servo's and 12 corresponding toggle switches. (This is to control points/turnouts on a model railway/railroad). I've got the basic programme set up, using if functions to detect if the switch is high/low and writing to the servo one of two variables; "point##Max" and "point##Min". That's all fine, I've got that working with a prototype with 2 servos.

The problem I can't sort is that after I've written/uploaded the script I want to be able to alter the two positions that each of the servo's go to through the script. Sure, I can guess at the positions now and write it into the void setup, but I want to be able to change in-situ in case I get it wrong and to save all the agro of uncoupling the board, guessing at the correct position, updating the script and reattaching everything, repeat until it works. Being used in a position away from the PC, in a non-temperature controlled environment, repeated operation, I need to allow that over time I may need to alter the "max" and "min" positions. Hence the need for a separate piece of script to reset the variables.

How I imagine this working is in the main loop when a push button, "Programme", is pressed it starts "Programme Mode", which is a sequence to adjust the max and min of each servo in turn. Starting with the first variable on the first servo, I will have two buttons which will adjust the arm up and down. When the desired position is achieved I want to press the Programme button to then progress to the next value. The same two buttons will then be used again to adjust the servo to the next position, with the programme button to save the final position and progress to the next variable. This progresses through each of the 12servos x 2 positions= 24variables and ends with an option to repeat the cycle or return to the void loop. Throughout this I want to ignore any of the switch inputs as I can't be reconfiguring the position while the switch trying to move the servo to a different position. I also want to avoid the possibility of any user mistaking switching a point while in programme mode to effect what is happening.

So how to I pause the void loop, silence any other inputs and carry out such a programme?
And how to I write a script that will only progress with the press of a button?

I have considered that this is a very long winded approach. If I have to change just one variable I will still need to cycle through all other variables to complete. I may consider adding an abort/save and close button, or some way of fast tracking to the desired variable, but for now i'm content with how I have set out above.
Further thoughts around the wider setup on this board: When coming out of the return() from the "Programme" function I will write/update the new values to the EEPROM, these will be read once in each void setup to reduce needing to repeatedly read/write to the EEPROM. I'm not expecting to have to adjust the values too much after the initial few runs of adjustment so not too worried on the limit on number of read/writes to the eeprom. And I've seen by using "update" this should further reduce any "ware"
To aid the Programme mode to the user I will probably couple the whole script with writing to an LCD display or more basically indicator LEDs to show progression.
I'm thinking of also using a while() option on a separate toggle switch to swap the whole control system into a sort of stacking changes mode. To basically work like the manual F9 "calculate now" does in excel. So on the control board I can make a series of changes which are only implemented when an "activate" button is pressed. This will allow you to get prepared for several changes to be implemented all at once. This is a little unnecessary for the day to day running, but I thought of it so want to make it happen.

Happy to take any comment whether the rest is feasible or suggestions how you might to it. If anyone's interested I'm doing all the power and control of the trains from a separate mega regulating power to the track with MOSFETs and will have a 3rd MEGA which is controlling any scenic lighting and a few other moving parts around the layout..

Wak101:
In short, two parts that I'm stuck on;
(i) how to I get a separate function to operate, essentially pausing the void loop and ignoring any other inputs then ending with a return function back to void loop with updated values to then be used in the main loop?
(ii) how can I write a script which only progresses when a button is pressed?

You should put the idea of "pausing the void loop()" firmly out of your mind. Your goal should be to have loop() repeat as often as possible - hundreds or thousands of times per second.

You control what happens by means of values stored in variables that record the state of the system. For example your code might have a section like this (buttonPressed is a variable).

if (buttonPressed == false) {
  // do nothing
}
else {
   // do whatever
}

Have a look at planning and implementing a program and / or several things at a time

...R

Many thanks Robin for taking the time to respond.

Thanks for pointing me in the right direction, I can see the logic and how this could work with a variable holding what mode the board should be in and writing if functions to check the mode an operate accordingly

Andy