Two(or more) servo functions in one

Hi,
I wrote a code for driving multible servos. Now I want to make it more compact. I am sure it is possible but I have no idea how...
I want to be able to change the variables into an other Variable so that I need just one funktion istead of (in this case) two. Later I want to have four servos, each servo will get a diffrent max-min-position and speed over I²C .

This is the main part of the code I have right now ( it is not fuinished yet,more something like a test trial):

#include <Servo.h>

Servo servo_1;                         // create servo object to control a servo 
Servo servo_2;
Servo servo_3;
Servo servo_4;

int pos_1;                             // variable to store the servo position 
int pos_2;
int pos_3;
int pos_4;


unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0;      // the variables is a long because the time, measured in miliseconds,
unsigned long previousMicros_2 = 0;      // will quickly become a bigger number than can be stored in an int.
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0;      // will store last time servo was updated




long interval = 1000; 
const int ledPin =  13;      // the number of the LED pin                    
int ledState = LOW; 




void Servo_01(int Min_1, int Max_1, int servospeed_1)
{

  if ((micros()-previousMicros_1) >=servospeed_1)  {
    previousMicros_1 = micros();
    
      
    if (pos_1 > servo_1.readMicroseconds())  { 
      servo_1.writeMicroseconds(servo_1.readMicroseconds() + 1);
   }
    
    
    if (pos_1 < servo_1.readMicroseconds())  {
      servo_1.writeMicroseconds(servo_1.readMicroseconds() - 1);
    }

    if(servo_1.readMicroseconds() == Max_1) {
      pos_1 = Min_1;
    }
    
    if(servo_1.readMicroseconds() <= Min_1)  {
      pos_1 = Max_1;
    }
  }  
}

void Servo_02(int Min_2, int Max_2, int servospeed_2)
{

  if ((micros()-previousMicros_2) >=servospeed_2)  {
    previousMicros_2 = micros();
    
      
    if (pos_2 > servo_2.readMicroseconds())  { 
      servo_2.writeMicroseconds(servo_2.readMicroseconds() + 1);
   }
    
    
    if (pos_2 < servo_2.readMicroseconds())  {
      servo_2.writeMicroseconds(servo_2.readMicroseconds() - 1);
    }

    if(servo_2.readMicroseconds() == Max_2)  {
      pos_2 = Min_2;
    }
    
    if(servo_2.readMicroseconds() <= Min_2)  {
      pos_2 = Max_2;
    }
  }  
}




void setup() 
{
  servo_1.attach(9, 554, 2400);                   // attaches the servo on pin 9 to the servo object
  servo_2.attach(10);
  servo_3.attach(11);
  servo_4.attach(12);

  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}

void loop()
{

  Servo_01(554, 2400, 500);
  Servo_02(800, 2000, 1000);
 
  
//-------------------------------------------------------------------------------- not important
  
 if( (millis() % 1000) == 0 )  {
   Serial.println(millis());
   Serial.println("A second has passed");
 }
 
 //-------------------------------------------------------------------------------
  
  if (millis() - previousMillis_1 > interval)  {
    // save the last time you blinked the LED 
    previousMillis_1 = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)  {
      ledState = HIGH;
    }
    else  {
      ledState = LOW;
    }
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    Serial.println(millis());
    Serial.println();
  }
}

I want something like that:

void Servo(int Servo_X, int Min_X, int Max_X, int servospeed_X)
{........}

void loop()
{
Servo(1,Min_X,Max_X,servospeed_X);  // these will get their values from the I²C network
Servo(2,Min_X,Max_X,servospeed_X);
....

}

To make that work servo_1 needs to change into servo_2, pos_1 into pos_2 and so on.

Is there a easy way to do that or is it to complicate?

I hope I explained my problem well enouth.

best regards,
Florian

Find out about arrays.

okay I looked at arrays and got totally confused...
At the beginning I thought I would understand something. later i noticed it to be totally false...

I tried somthing similar to this:

void servo(servo[] = {0,1,2,3},.......)

but had absolutely no luck with it.

Please help me.

If you look at your functions "Servo_01" and "Servo_02", you'll see that they're identical in structure and they only differ by the names of the variables (and the names of the functions themselves).

The trick is to parameterise the functions so that the structure remains the same, but you pass parameters to the function to change which servo you operate on.

void Servo_x(Servo* thisServo, int Min_1, int Max_1, int servospeed_1)
{

  if ((micros()-previousMicros_1) >=servospeed_1)  {
    previousMicros_1 = micros();
    

    if (pos_1 > thisServo->readMicroseconds())  {
      thisServo->writeMicroseconds(thisServo->readMicroseconds() + 1);
   }
    
    
    if (pos_1 < thisServo->readMicroseconds())  {
      thisServo->writeMicroseconds(thisServo->readMicroseconds() - 1);
    }

    if(thisServo->readMicroseconds() == Max_1) {
      pos_1 = Min_1;
    }
    
    if(thisServo->readMicroseconds() <= Min_1)  {
      pos_1 = Max_1;
    }
  }  
}

would be just one way of doing this (but won't work without a little more work, mainly on the previous values)
Play with it, think about it,maybe look at arrays of structures.

I tried something but got an error code I don't understand:

#include <Servo.h>

Servo servo_1;                         // create servo object to control a servo 
Servo servo_2;
Servo servo_3;
Servo servo_4;

int pos_1;                             // variable to store the servo position 
int pos_2;
int pos_3;
int pos_4;


unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0;      // the variables is a long because the time, measured in miliseconds,
      // will quickly become a bigger number than can be stored in an int.
      // will store last time servo was updated

int Speed = 900;



long interval = 1000; 
const int ledPin =  13;      // the number of the LED pin                    
int ledState = LOW; 




void servo(int Servo_num)
{

  int servo[]                     = {0,1,2,3,4};
  int Speed_[]                     = {0,1,2,3,4};
  int pos[]                       = {0,1,2,3,4};
  unsigned long previousMicros[]  = {0,1,2,3,4};
  
  
  if ((micros()-previousMicros[Servo_num]) >=Speed)  {
    previousMicros[Servo_num] = micros();
    
      
    if (pos[Servo_num] > (servo[Servo_num]).readMicroseconds())  { 
      servo[Servo_num].writeMicroseconds(servo[Servo_num].readMicroseconds() + 1);
   }
    
    
    if (pos[Servo_num] < servo[Servo_num].readMicroseconds())  {
      servo[Servo_num].writeMicroseconds(servo[Servo_num].readMicroseconds() - 1);
    }

    if(servo[Servo_num].readMicroseconds() == 2400)  {
      pos[Servo_num] = 554;
    }
    
    if(servo[Servo_num].readMicroseconds() <= 554)  {
      pos[Servo_num] = 2400;
    }
     
  }
  
}










void setup() 
{
  servo_1.attach(9, 554, 2400);                   // attaches the servo on pin 9 to the servo object
  servo_2.attach(10);
  servo_3.attach(11);
  servo_4.attach(12);

  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}

void loop()
{

  servo(1);
  
 
  
//-------------------------------------------------------------------------------- not important
  
 if( (millis() % 1000) == 0 )  {
   Serial.println(millis());
   Serial.println("A second has passed");
 }
 
 //-------------------------------------------------------------------------------
  
  if (millis() - previousMillis_1 > interval)  {
    // save the last time you blinked the LED 
    previousMillis_1 = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)  {
      ledState = HIGH;
    }
    else  {
      ledState = LOW;
    }
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    Serial.println(millis());
    Serial.println();
  }
}

In function 'void servo(int)':
error: request for member 'readMicroseconds' in 'servo[Servo_num]', which is of non-class type 'int'

does someone understand it?

Yes.

You need to slow down, start with some of the tutorials, work through them and modify them, and build on your learning.

You're confusing constructs like functions, objects and arrays.

If you find yourself writing something so self-similar like

int servo[]                     = {0,1,2,3,4};
  int Speed_[]                     = {0,1,2,3,4};
  int pos[]                       = {0,1,2,3,4};
  unsigned long previousMicros[]  = {0,1,2,3,4};

you've probably got the problem the wrong way around.

Cut your problem down, start with just two servos, and work from that.

int servo[]                     = {0,1,2,3,4};
  int Speed_[]                     = {0,1,2,3,4};
  int pos[]                       = {0,1,2,3,4};
  unsigned long previousMicros[]  = {0,1,2,3,4};


    if (pos[Servo_num] > (servo[Servo_num]).readMicroseconds())  {
                                        ^^^^^
                                         Here!

An "int" can't have a "readMicroseconds" method.

I tried to use this array method from the KnightRider tutorial.

is there a way (or some other way) to combine an array simillar to this?

(servo[Servo_num]).readMicroseconds())

is there a way (or some other way) to combine an array simillar to this?

(servo[Servo_num]).readMicroseconds())

Yes, just make the type of the array "servo" "Servo", or "Servo*".

Seriously, slow down, work through some examples.