Pan and Tilt

Hi
I am making a pan and tilt gimble that will make sweeping scans left to right and moving down a notch after each left to right movement.
Accuracy is not important and all movement is stopped when a button is pressed hence the use of delay while motors are running
I have hall effect and incremental rotary encoders to hand but that would be over kill for this project .
I have come up with the following code which seems rather repetitive and looking at making it shorter and cutting it down to a couple of lines.
Can anyone please advise.

#include <AFMotor.h>
#include <Button.h>
Button button = Button(12,PULLUP);
AF_DCMotor motor(4);
AF_DCMotor motor2(2);
void setup() {

}

void loop() {
 if(button.isPressed()){
   motor.run(RELEASE);
 }else{
   motor.run(FORWARD);
   delay(5000);
   motor.run(BACKWARD);
   delay(5000);
   motor.run(RELEASE);
   motor2.run(FORWARD);
   delay(1000);
   motor2.run(RELEASE);
   motor.run(FORWARD);
   delay(5000);
   motor.run(BACKWARD);
   delay(5000);
   motor.run(RELEASE);
   motor2.run(FORWARD);
   delay(1000);
   motor2.run(RELEASE);
   motor.run(FORWARD);
   delay(5000);
   motor.run(BACKWARD);
   delay(5000);
   motor.run(RELEASE);
   motor2.run(FORWARD);
   delay(1000);
   motor2.run(RELEASE);
   motor.run(FORWARD);
   delay(5000);
   motor.run(BACKWARD);
   delay(5000);
   motor.run(RELEASE);
   motor2.run(FORWARD);
   delay(1000);
   motor2.run(RELEASE);
  
 }
}

get the code under hash "#" tags please!

First get rid of ALL the delay()s. See the Blink Without Delay example sketch for the proper way to do timing.

If you want to perform a preset series of movements you could define the series as an array of characters such as

char movements[] = {'f', 'b', 'r', 'f', 'r', 'f'}

etc - where f = fwd, b = bkwd and r = release

Then in loop() you would have something like this (assuming there are 6 steps in the series)

for (int n = 0; n < 6; n++) {
   motorRun(movements[n]);
}

The code I posted in Reply #15 of this Thread may be of interest Switching between LED sequences - #20 by Robin2 - Programming Questions - Arduino Forum may be of interest.

...R

Many thanks for that and reference to the other post.
How would the sketch look in total ( I am still learning Arduino) would you mind showing me a bit more

Very like the sketch in the other Thread except that you would control servos instead of leds.

You have two different motors and I don't understand how their movements are coordinated. Maybe you need to use (for example) 'f' to mean one motor and 'F' to mean the other.

...R

motor is for left to right axis and motor2 is for up and down
I have tried with simple servo and continuos servo but they can not supply the torque required hence adafruit motor shield and motors with gearbox.