Running a setup loop before the main loop

Hi and first off thanks for taking the time to read my post. So im working on a project using a few servos to move a 3D printed robotic arm. I have the problem that my 3D printed joystick may not be centered when the system boots up and do not want the arm destroying itself trying to go to a position it might not be able to. The way I am think this should work is to have the program start with the servos in a "parked" position then have the user move the joysticks to the position to match the parked position before the arm will start taking inputs from the joystick box. I think the way to do this would be to have a loop run in the void setup section and once it can match the position of the joysticks and the arm it will allow the main loop to run. I know the void setup is only suposto run once on start up so it should keep it from locking back up almost emeditly when running thru the main loop. I just don't know exactly how i could do something like this or if its even the right way to do it. any help that can be provided would help a lot thanks!!

Your long text is very hard to understand because all the ideas are jumbled together. See how much easier it is read when paragraphs are used.

Hi and first off thanks for taking the time to read my post.

So im working on a project using a few servos to move a 3D printed robotic arm.

I have the problem that my 3D printed joystick may not be centered when the system boots up and do not want the arm destroying itself trying to go to a position it might not be able to.

The way I am think this should work is to have the program start with the servos in a "parked" position then have the user move the joysticks to the position to match the parked position before the arm will start taking inputs from the joystick box.

I think the way to do this would be to have a loop run in the void setup section and once it can match the position of the joysticks and the arm it will allow the main loop to run.

I know the void setup is only suposto run once on start up so it should keep it from locking back up almost emeditly when running thru the main loop.

I just don't know exactly how i could do something like this or if its even the right way to do it. any help that can be provided would help a lot thanks!!

...R

You can run any code you want in setup(). If it is something that you will also want to run from loop() then put the code into a function and then you can call the function whenever it is needed.

When the Arduino starts it has no means to know where the servos were when the Arduino was switched off. You could use code in setup() to make them all go to their mid position - but even that can put a large force on the servos.

It probably would be wise to adopt a formal shut-down procedure in which the operator moves all the servos to their mid position before turning the Arduino off. But people do forget to do things like that - for example when the girl-friend arrives :slight_smile:

You should also ensure that no movement of the joystick can make a servo go beyond its fixed limits.

...R

Any code that just moves the arm on powerup is bad code.

Using a setup loop to calibrate the joypad would be best. Add code to set xy(z?) variables based on moving the joystick around before the arm goes active.

Maybe require full up command for 2 seconds or something after it’s calibated to break out of the loop.

Most RC models nowadays - particularly the drones - use a protocol where the throttle must be moved through its full range and back to zero before the model will accept any actual commands. This would appear to be the same concept.