giant confused noob

Hi everyone. I'll try my best to keep this concise as i don't want to be a newby who's asking overtly dumb questions. Programming and cold is very far out of my comfort zone. But I am building a giant display model for a game and want automated turrets that scan left to right and up to down.
There is AN ABSOLUTELY PERFECT sketch in the examples for this. I know how to adjust the variables and load it up. And switch out which pin the info will go to.
However for 4 turrets with two servos a piece I need to repeat the code 8 times.
Which I have absolutely no f'n clue how to do. I've watched about 4 youtube videos....and I do get a little. But by and large those guys are speaking gibberish.
Can someone point me in the right direction? Or explain it to me like i'm a five year old?
thanks in advance for any help you provide.

Post the code that you think is perfect. I suspect that it is the Servo Sweep example, but let's be sure

Do all of the servos need to move at the same time and will they be synchronised or moving in different patterns or at different times ?

YUP!!! Servo Sweep. Perfect for my uses is maybe more accurate.

/* Sweep
by BARRAGAN http://barraganstudio.com
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

And the answers to my questions ?

generic noob post:

Read “How To Use This Forum”

in particular, 7. If you are posting code or error messages, use “code” tags

This is what happens when you do not

if you have a module that is not working, do not say “A GPS” or “the fingerprint sensor”. Show us a link to the particular sensor, and a link to the datasheet if available

You can make an array of servos and for loops to iterate through the arrays.

Here is example code with 4 servos to illustrate what I mean. Merely increase numservos to 8 and fill the servoPins array with the pin numbers for your use. You will absolutely need external power for the servos.

#include <Servo.h>

const byte numServo = 4;

Servo servo[numServo];

const byte servoPins[numServo] = {4, 5, 6, 7};

void setup()
{
   Serial.begin(115200);
   for (int n = 0; n < numServo; n++)
   {
      servo[n].attach(servoPins[n]);
      servo[n].write(0);
   }
}

void loop()
{
   for (int pos = 0; pos <= 180; pos += 1)
   {
      for (int n = 0; n < numServo; n++)  // iterate through the servo array to write new pos
      {
         servo[n].write(pos);
      }
      delay(15);
   }

   for (int pos = 180; pos >= 0; pos -= 1)
   {
      for (int n = 0; n < numServo; n++)  // iterate through the servo array to write new pos
      {
         servo[n].write(pos);
      }
      delay(15);
   }
}

UKHeliBob:
And the answers to my questions ?

So sorry. I read your post quickly before running out to work. The servo's will not be in sync. As I am trying to mimic different people using the "gun turrets" to scan the skys for enemy's. I was planning on adjusting the numbers as needed.

Okay i think i see what you did there. It helps. I see where to add the pins. But not to where/how to differentiate the other parts as to which pin its sending instructions to. Unless…as each addition section is entered it is instructing the next pin in the sequence.
Mark

The servo's will not be in sync

That has a big influence on how the sketch is written. You need to move one servo a small amount, then the next one (maybe a different amount), then the next one and so on. After each move, if a servo has reached one of its end positions (which do not all have to be the same) then next time it moves it must be in the other direction. For extra complication each servo does not have to move at the same speed nor even over the same range each time.

How complicated do you want to make this ?

TBH if no coordination or communication is required between turrets, and space or weight are not issues, it might be easier just to use multiple Arduinos...
Unless of course you are in it for the learning exercise. In which case learn to code, then combine the sketches?

so you can't combine multiples of the same sketch?

so you can't combine multiples of the same sketch?

Sure you can. But you must define what you want to do first.

The code that I wrote was to give you ideas, not solve the problem for you. I could not do that, anyway, because I have not seen a complete set of requirements.